我一直在关注Kafka消费者类。我可以将主题作为列表对象传递。我指的是下面的文章https://docs.confluent.io/current/clients/java.html,但是我需要知道使用者类订阅了主题之后,如何知道哪个主题在其中包含记录。有什么办法找出来吗?这是代码:
public abstract class ConsumeLoop implements Runnable {
private final KafkaConsumer<K, V> consumer;
private final List<String> topics;
private final CountDownLatch shutdownLatch;
public BasicConsumeLoop(KafkaConsumer<K, V> consumer, List<String> topics) {
this.consumer = consumer;
this.topics = topics;
this.shutdownLatch = new CountDownLatch(1);
}
public abstract void process(ConsumerRecord<K, V> record);
public void run() {
try {
consumer.subscribe(topics); --> Consuming list of topics
while (true) {
ConsumerRecords<K, V> records = consumer.poll(Long.MAX_VALUE); --> Which topic is returning the records?
records.forEach(record -> process(record));
}
} catch (WakeupException e) {
// ignore, we're closing
} catch (Exception e) {
log.error("Unexpected error", e);
} finally {
consumer.close();
shutdownLatch.countDown();
}
}
}
答案 0 :(得分:2)
ConsumerRecords
中的 partitions()方法将返回一组TopicPartition
s:
分区-获取在此记录集中包含记录的分区。
然后,您可以根据需要在该集合上进行迭代以获得topic()
名称和partition()
数字。例如:
for (TopicPartition tp : records.partitions()) {
System.out.println("Got " + records.records(tp).size() + " records "
+ "from topic:partition " + tp.topic() + ":" + tp.partition());
}