简体   繁体   English

获取分配给Kafka分区的使用者或客户端ID

[英]Get the consumer or client id assigned to Kafka partitions

We have a code that gets some details of the consumers of kafka topic. 我们有一个代码,获取有关kafka主题使用者的一些详细信息。 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. 我们需要的缺失信息是使用者组中分区的客户机ID /使用者。 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());

        }
    }

You probably mean group.id property of the consumer, as offsets are per group, nor per client.id . 您可能是指group.id属性,因为偏移量是每个组,也不是每个client.id This discussion stackoverflow.com/questions/55937806/apache-kafka-get-list-of-consumers-on-a-specific-topic/55938325 此讨论stackoverflow.com/questions/55937806/apache-kafka-get-list-of-consumers-on-specific-topic/55938325

can answer you question. 可以回答你的问题。

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

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