【README】
本文po出了 topic not present in metadata 的解决方法;
很多博文说是 因为 jackson-databind 没有引入,但是我重新引入后,还是没有解决问题;
最后,原因在于我要发送消息的分区,topic 没有对应分区会报这个错误;
【1】场景重现
1)新建topic hello02, 分为3个分区,每个分区2个副本;
[root@centos201 kafka3.0.0]# kafka-topics.sh --bootstrap-server centos201:9092 --describe --topic hello02
Topic: hello02 TopicId: xbNGvz9uTDu77qpkw7D3Ug PartitionCount: 3 ReplicationFactor: 2 Configs: segment.bytes=1024
Topic: hello02 Partition: 0 Leader: 3 Replicas: 2,3 Isr: 3,2
Topic: hello02 Partition: 1 Leader: 3 Replicas: 3,1 Isr: 3,1
Topic: hello02 Partition: 2 Leader: 2 Replicas: 1,2 Isr: 2,1
2)java 生产者发送数据
设置目的分区为3;
/* 10.发送数据 */
String now = DateUtils.getNowDate();
for (int i = 0; i < 10; i++) {
Future<RecordMetadata> future = producer.send(new ProducerRecord<String, String>("hello02", 3, "", DataFactory.INSTANCE.genOneHundred()));
try {
System.out.println("[生产者] " + future.get().partition() + "-" + future.get().offset());
} catch (Exception e) {
e.printStackTrace();
}
}
报错如下:
[kafka-producer-network-thread | producer-1] INFO org.apache.kafka.clients.Metadata - [Producer clientId=producer-1] Cluster ID: Sbvo8XzuQxeoikbg-Oo8ZQ
java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.TimeoutException: Topic hello02 not present in metadata after 3000 ms.
at org.apache.kafka.clients.producer.KafkaProducer$FutureFailure.<init>(KafkaProducer.java:1320)
at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:989)
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:889)
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:775)
at kafka.produer.MyProducer.main(MyProducer.java:51)
3)把 分区设置为2 ,我们再试下;
Future<RecordMetadata> future =
producer.send(new ProducerRecord<String, String>("hello02", 2, "", DataFactory.INSTANCE.genOneHundred()));
public ProducerRecord(String topic, Integer partition, K key, V value) {
this(topic, partition, null, key, value, null);
}
成功发送;
【2】小结
1)问题根因,应该都看到了:
就是因为 生产者设置的分区号,在kafka集群中不存在导致的; 然后报错信息提示 topic 不存在,而不是说 partition不存在; 其实在发送消息时,不设置分区也不会出现这个问题;