Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get last committed offset from read_committed Kafka Consumer

I am using the transactional KafkaProducer to send messages to a topic. This works fine. I use a KafkaConsumer with read_committed isolation level and I have an issue with the seek and seekToEnd methods. According to the documentation, the seek and seekToEnd methods give me the LSO (Last Stable Offset). But this is a bit confusing. As it gives me always the same value, the END of the topic. No matter if the last entry is committed (by the Producer) or part of an aborted transaction. Example, after I abort the last 5 tries to insert 20_000 messages, the last 100_000 records should not be read by the Consumer. But during a seekToEnd it moves to the end of the Topic (including the 100_000 messages). But the poll() does not return them.

I am looking for a way to retrieve the Last Committed Offset (so the last successful committed message by the Producer). There seems to be no proper API method for this. So do I need to roll my own?

Option would be to move back and poll until no more records are retrieved, this would result in the last committed message. But I would assume that Kafka provides this method.

We use Kafka 1.0.0.

like image 423
Coen Damen Avatar asked Jan 23 '18 07:01

Coen Damen


People also ask

How do I get latest offset in Kafka?

Using kafka-python You can use end_offsets : Get the last offset for the given partitions. The last offset of a partition is the offset of the upcoming message, i.e. the offset of the last available message + 1. This method does not change the current consumer position of the partitions.

What is the last committed offset in Kafka?

Kafka - Understanding Offset Commits Committed offsets is the last committed offset for the given partition. Committing an offset for a partition is the action of saying that the offset has been processed so that Kafka cluster won't send the committed records for the same partition.

How do I get Kafka consumer offset?

How to find the current consumer offset? Use the kafka-consumer-groups along with the consumer group id followed by a describe. You will see 2 entries related to offsets – CURRENT-OFFSET and LOG-END-OFFSET for the partitions in the topic for that consumer group.


1 Answers

The class KafkaConsumer has some nice methods like: partitionFor, begginingOffsets and endOffsets also commited and position.

Check which one fits to your needs. Especially carefully consider all 4 offset-related methods. The method partitionFor returns complete metadata object with other information, but can be useful for enriching the logging.

like image 157
Seweryn Habdank-Wojewódzki Avatar answered Oct 18 '22 21:10

Seweryn Habdank-Wojewódzki