简体   繁体   English

Kafka 消费者获得特定主题的分配分区

[英]Kafka Consumer get assigned partitions for a specific topic

I'm working with a Kafka Consumer, and only subscribe to one topic.我正在与 Kafka 消费者合作,并且只订阅一个主题。 I want to return only the assigned partitions from the topic for each consumer.我只想为每个消费者返回主题中分配的分区。 I am running four instances of the consumer in the same group, and reading a topic with 8 partitions.我在同一个组中运行四个消费者实例,并阅读一个有 8 个分区的主题。

I know I can use the assignment() method, but it looks like that returns the topic name and partition in the format of - (ie topic1-0, topic1-1, etc).我知道我可以使用 assignment() 方法,但它看起来以 - (即 topic1-0、topic1-1 等)的格式返回主题名称和分区。 Is there a better way of getting the partition only without having to parse the value returned from the assignment() method?有没有更好的方法来获取分区,而不必解析从 assignment() 方法返回的值?

public Set<TopicPartition> assignment() public Set<TopicPartition> assignment()

assignment() will return the set of TopicPartition assignment()将返回TopicPartition的集合

public int partition() .公共 int partition() here 这里

In the TopicPartition you have method partition() that gives the partition of topicTopicPartition您有方法partition()给出主题的分区

Example例子

Set<TopicPartition> partitions = consumer.assignment();
partitions.forEach(part->System.out.println(part.partition()));

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

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