Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Getting kafka consumer offsets of a consumer group for a given topic in java?

avatar
Explorer

Hi,

I am trying to write a java program that monitors the kafka consumer and end offset values and send alerts if it does not update for a given period of time. So i was using the consumer.getCommitedOffsets() method in the Kafka consumer API in java. But this does not seem to be the correct consumer offset value or maybe not the consumer offset for this consumer group. Can anyone tell me how to get the value of consumer offsets in kafka for a consumer group and particular topic?

4 REPLIES 4

avatar
Super Guru

@ROHIT AILA

Have you tried using consumer.position(TopicPartition partition)? or run the following script from your java program. You can also run the script with zookeeper instead of bootsrap-server.

kafka-consumer-groups.sh --bootstrap-server <broker-host>:9092 --new-consumer --group groupname --describe

avatar
Explorer

@mqureshi I tried using the consumer.position(TopicPartition partition) method. But this does not seem to give the correct value either. I know how to get them in the command line. So i am comparing the values in zookeeper to the one in java method. My goal is to get those values in Java.

avatar
Super Guru

@ROHIT AILA

I understand. That's why, I think you need to call kafka-consumer-group.sh script from your java program to get what you are looking for.

avatar
Master Collaborator

kafka-consumer-groups.sh calls ConsumerGroupCommand

Please take a look at the following method:

ConsumerGroupCommand#getPartitionOffsets