Bros0912
Bros0912

Reputation: 75

Kafka Consumer get assigned partitions for a specific topic

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?

Upvotes: 6

Views: 11474

Answers (1)

Ryuzaki L
Ryuzaki L

Reputation: 40078

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()));

Upvotes: 8

Related Questions