我想获得卡夫卡消费者的进步,即滞后。我知道以下命令给了我滞后和其他有价值的描述。
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库找到滞后和其他细节?
答案 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));