简体   繁体   English

卡夫卡消费者的多个话题

[英]Kafka consumer for multiple topic

I have a list of topics (for now it's 10) whose size can increase in future. 我有一个主题列表(现在是10个),其大小可以在未来增加。 I know we can spawn multiple threads (per topic) to consume from each topic, but in my case if the number of topics increases, then the number of threads consuming from the topics increases, which I do not want, since the topics are not going to get data too frequently, so the threads will sit ideal. 我知道我们可以生成多个线程(每个主题)来从每个主题中使用,但在我的情况下,如果主题数量增加,那么从主题中消耗的线程数量会增加,这是我不想要的,因为主题不是过于频繁地获取数据,因此线程将是理想的。

Is there any way to have a single consumer to consume from all topics? 有没有办法让一个消费者从所有主题中消费? If yes, then how can we achieve it? 如果是,那我们怎样才能实现呢? Also how will the offset be maintained by Kafka? 卡夫卡还将如何维持这种抵消? Please suggest answers. 请提出答案。

We can subscribe for multiple topic using following API : consumer.subscribe(Arrays.asList(topic1,topic2), ConsumerRebalanceListener obj) 我们可以使用以下API订阅多个主题:consumer.subscribe(Arrays.asList(topic1,topic2),ConsumerRebalanceListener obj)

Consumer has the topic info and we can comit using consumer.commitAsync or consumer.commitSync() by creating OffsetAndMetadata object as follows. 消费者有主题信息,我们可以通过创建OffsetAndMetadata对象使用consumer.commitAsync或consumer.commitSync()进行如下操作。

ConsumerRecords<String, String> records = consumer.poll(long value);
for (TopicPartition partition : records.partitions()) {
    List<ConsumerRecord<String, String>> partitionRecords = records.records(partition);
    for (ConsumerRecord<String, String> record : partitionRecords) {
        System.out.println(record.offset() + ": " + record.value());
    }
    long lastOffset = partitionRecords.get(partitionRecords.size() - 1).offset();
    consumer.commitSync(Collections.singletonMap(partition, new OffsetAndMetadata(lastOffset + 1)));
}

There is no need for multiple threads, you can have one consumer, consuming from multiple topics. 不需要多个线程,您可以拥有一个消费者,从多个主题中消费。 Offsets are maintained by zookeeper, as kafka-server itself is stateless. 由zooaf维护偏移量,因为kafka-server本身是无状态的。 Whenever a consumer consumes a message,its offset is commited with zookeeper to keep a future track to process each message only once. 每当消费者使用消息时,其偏移量将与zookeeper一起提交,以保持未来的跟踪仅处理每条消息一次。 So even in case of kafka failure, consumer will start consuming from the next of last commited offset. 因此,即使在kafka失败的情况下,消费者也会从最后一次提交的偏移中开始消费。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM