我一直在努力用kafka经纪人和在localhost上运行的kafka制作人做一个非常简单的应用程序几天,我已经阅读了谷歌类似问题的所有可能答案,但我仍然无法和#39; t使它工作。
这是我得到的错误:
INFO kafka.client.ClientUtils$ - Fetching metadata from broker id:0,host:localhost,port:9092 with correlation id 0 for 1 topic(s) Set(clicks)
[ProducerSendThread-] INFO kafka.producer.SyncProducer - Connected to localhost:9092 for producing
[ProducerSendThread-] INFO kafka.producer.SyncProducer - Disconnecting from localhost:9092
[ProducerSendThread-] WARN kafka.producer.BrokerPartitionInfo - Error while fetching metadata [{TopicMetadata for topic clicks ->
No partition metadata for topic clicks due to kafka.common.LeaderNotAvailableException}] for topic [clicks]: class kafka.common.LeaderNotAvailableException
[ProducerSendThread-] INFO kafka.client.ClientUtils$ - Fetching metadata from broker id:0,host:localhost,port:9092 with correlation id 1 for 1 topic(s) Set(clicks)
[ProducerSendThread-] INFO kafka.producer.SyncProducer - Connected to localhost:9092 for producing
[ProducerSendThread-] INFO kafka.producer.SyncProducer - Disconnecting from localhost:9092
[ProducerSendThread-] WARN kafka.producer.BrokerPartitionInfo - Error while fetching metadata [{TopicMetadata for topic clicks ->
No partition metadata for topic clicks due to kafka.common.LeaderNotAvailableException}] for topic [clicks]: class kafka.common.LeaderNotAvailableException
[ProducerSendThread-] ERROR kafka.producer.async.DefaultEventHandler - Failed to collate messages by topic, partition due to: Failed to fetch topic metadata for topic: clicks
[ProducerSendThread-] INFO kafka.producer.async.DefaultEventHandler - Back off for 100 ms before retrying send. Remaining retries = 3
这是我的代码:
Properties properties= new Properties();
properties.put( "broker.id", "1");
properties.put( "advertised.host.name", "localhost");
properties.put( "advertised.port", "9092");
properties.put( "host.name", "localhost");
properties.put( "auto.create.topics.enable","true");
properties.put("zookeeper.connect", zookeeperConnectString);
properties.put("port","9092");
properties.setProperty("num.partitions", "1");
properties.setProperty("log.dirs", newPath(KAFKA_LOG_DIR).toString());
KafkaConfig kafkaConfig = new KafkaConfig(properties);
KafkaServerStartable kafkaServer = new KafkaServerStartable(kafkaConfig);
kafkaServer.startup();
String topic = clicks;
ZkClient zookeeper = new ZkClient(zookeeperConnectString, 30000, 30000, ZKStringSerializer$.MODULE$);
if (!AdminUtils.topicExists(zookeeper, topic)) {
AdminUtils.createTopic(new ZkClient(zookeeperConnectString), topic, 1, 1, new Properties());
}
zookeeper.close();
Properties producerProps = new Properties();
producerProps.put("serializer.class", "kafka.serializer.StringEncoder");
producerProps.put("key.serializer.class", "kafka.serializer.StringEncoder");
producerProps.setProperty("producer.type", "async");
producerProps.put("metadata.broker.list", "localhost:9092");
producerProps.put("request.required.acks","0");
Producer producer = new Producer(new ProducerConfig(producerProps));
String click = "exampleMessage";
producer.send(ImmutableList.of(new KeyedMessage(topic, click)));
producer.close();
我已经在localhost上正确运行了zookeeper实例:2181。
我使用以下版本的kafka和zookeeper:
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.10</artifactId>
<version>0.8.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.8.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
感谢您提供任何帮助或评论:)
答案 0 :(得分:0)
如果有人遇到同样的问题,我会在创建主题后添加Thread.sleep(500);
。我不确切知道为什么,这可能是由于在localhost上运行的Zookeeper实例需要一些时间来初始化。