使用kafka库查找消费者偏移量滞后的代码?

时间:2016-09-29 14:32:51

标签: apache-kafka kafka-consumer-api

我想获得卡夫卡消费者的进步,即滞后。我知道以下命令给了我滞后和其他有价值的描述。

bin/kafka-run-class.sh kafka.admin.ConsumerGroupCommand --zookeeper localhost:2182 --describe --group DemoConsumer

bin/kafka-consumer-groups.sh --zookeeper localhost:2182 --describe --group DemoConsumer

我还可以在kafka-client的帮助下使用以下代码片段获取当前的消费者偏移量

ConsumerRecords<Integer, String> records = consumer.poll(100);
for (ConsumerRecord<Integer, String> record : records) {

             System.out.println("Received message: (" + record.topic()+ ",
             " + record.partition()+ ", " + record.key() + ", " +
             record.value() + ") at offset " + record.offset());
}

但我找不到代码来获取上述两个命令的详细信息。是否有任何代码可以使用kafka库找到滞后和其他细节?

1 个答案:

答案 0 :(得分:0)

根据this topic,您可以获得消费者滞后。但是,maven依赖在该主题中是错误的,它应该是

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka_2.12</artifactId>
    <version>0.10.2.0</version>
</dependency>

代码是:

AdminClient client = AdminClient.createSimplePlaintext("localhost:9092");
Map<TopicPartition, Object> offsets = JavaConversions.asJavaMap(
client.listGroupOffsets("groupID"));
Long offset = (Long) offsets.get(new TopicPartition("topic", 0));