0
votes

We have a code that gets some details of the consumers of kafka topic. The code below shows how to get the partitions and corresponding offsets. The missing information that we need is the client id/consumers of the partition within the consumer group. Do we have a way to get the consumers of each topic partition?

    ArrayList<TopicPartition> partitions = new ArrayList<TopicPartition>();
    ArrayList<OffsetAndMetadata> offsets = new ArrayList<OffsetAndMetadata>();

    for (int i=0;i<consumer.partitionsFor(topic).size();i++)
    {
        TopicPartition partitiontemp = new TopicPartition(topic, i);
        partitions.add(partitiontemp);
        OffsetAndMetadata offsettemp = consumer.committed(partitiontemp);
        offsets.add(offsettemp);
    }

    consumer.assign(partitions);
    consumer.seekToEnd(partitions);

    for (int i=0;i<consumer.partitionsFor(topic).size();i++)
    {
        try {

            long cur_offset = offsets.get(partitions.get(i).partition()).offset();
            long log_offset = consumer.position(partitions.get(partitions.get(i).partition()));

            System.out.printf("Topic: %s partitionID: %d current offset: %d log offset: %d uncommitted: %d\n",
                    topic, partitions.get(i).partition(),cur_offset , log_offset , log_offset - cur_offset);


        }catch (Exception ex){
            System.out.printf("Topic: %s partitionID: %d current offset: - log offset: - uncommitted: -\n", topic, partitions.get(i).partition());

        }
    }
1

1 Answers

0
votes

You probably mean group.id property of the consumer, as offsets are per group, nor per client.id. This discussion stackoverflow.com/questions/55937806/apache-kafka-get-list-of-consumers-on-a-specific-topic/55938325

can answer you question.