3
votes

I'm working with a Kafka Consumer, and only subscribe to one topic. 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.

I know I can use the assignment() method, but it looks like that returns the topic name and partition in the format of - (i.e. topic1-0, topic1-1, etc). Is there a better way of getting the partition only without having to parse the value returned from the assignment() method?

1

1 Answers

4
votes

public Set<TopicPartition> assignment()

assignment() will return the set of TopicPartition

public int partition() . here

In the TopicPartition you have method partition() that gives the partition of topic

Example

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