Saurabh
Saurabh

Reputation: 115

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

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

Upvotes: 3

Views: 7555

Answers (1)

Katya Gorshkova
Katya Gorshkova

Reputation: 1561

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)

Upvotes: 7

Related Questions