How to get Consumer Lag for a consumer group in kafka in java

Saurabh :

I want to know the consumer lag for a consumer group using java. I have tried using

kafka-consumer-groups --describe --bootstrap-server localhost:9092 --group MyGroupName

and the lag is visible.

How do i do this in Java ?

I have tried using the org.apache.kafka.clients.admin.AdminClient, but could not get the lag per consumer group.

i am using confluent 5.0.1 which has kafka 2.0.1 org.apache.kafka - kafka-clients - 2.0.1

Katya Gorshkova :

Lag is approximately endOffset-currentOffset. You can use

AdminClient.listConsumerGroupOffsets("MyGroupName").partitionsToOffsetAndMetadata()

to get the current offset for the group. To get the end offsets you need to create KafkaConsumer for the topic and use

KafkaConsumer.endOffsets(partitions)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=104526&siteId=1