I have two consumers in a consumer group which have assigned same kafka topic partitions. I wish to get last read offset of say, consumer A from inside consumer B. Any Idea, how to implement this?
Kafka store offsets by (consumer-group-id, topic, partition) so the first thing to note is that from Kafka point of view there is no such thing like "last read offset of consumer A". All information that you can get with the Kafka consumer API is for a given (group, topic, partition). You have two methods in consumer API that may be useful.
commited(): Get the last committed offset for the given partition (whether the commit happened by this process or another).
position(): Get the offset of the next record that will be fetched (if a record with that offset exists).
If that is not what you need, then you will have to implement something yourself. Assuming you already know how to get the last offset read from consumer A, then consumer A should store that value in some location that is available to consumer B. This location could be
A single partition will never be assigned to two consumer instances in the same group.
You can use the below script to know the last consumed offset
sh kafka-consumer-groups.sh --bootstrap-server localhost:9092 --new-consumer --group groupname --describe
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With