Describe Kafka consumer groups


What's inside this article ⌄
  • Kafka describe consumer group Java
  • How to list Kafka consumer groups
  • Kafka consumer group details Java
  • Java Kafka group description

If you want to describe a Kafka Consumer group in Java, this is how you can do it:

 1// returns list of consumers in given consumer group mapped on distributed partitions
 2public String consumerGroupDescription(KafkaAdmin kafkaAdmin, String consumerGroup) {
 3    DescribeConsumerGroupsResult result = kafkaAdmin.describeConsumerGroups(Collections.singleton(consumerGroup));
 4    ConsumerGroupDescription groupDescription;
 5    try {
 6        groupDescription = result.describedGroups().get(consumerGroup).get();
 7    } catch (InterruptedException | ExecutionException e) {
 8        throw new RuntimeException(e);
 9    }
10    Collection<MemberDescription> membersDescription = groupDescription.members();
11    Map<String, Integer> consumerPartitions = new HashMap<>();
12    for (MemberDescription md : membersDescription) {
13        consumerPartitions.put(md.consumerId(), md.assignment().topicPartitions().size());
14    }
15    StringBuilder sb = new StringBuilder();
16    for (Map.Entry<String, Integer> entry : consumerPartitions.entrySet()) {
17        sb.append("\n[").append(entry.getKey()).append("]:[").append(entry.getValue()).append("]");
18    }
19    if (sb.toString().equals("")) {
20        sb.append("<no active members found>");
21    }
22    return String.format("----- Group %s description: %s", consumerGroup, sb.toString());
23}