Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the last/end offset of a kafka topic partition?

I'm writing a kafka consumer using Java. I want to keep the real time of the message, so if there are too many messages waiting for consuming, such as 1000 or more, I should abandon the unconsumed messages and start consuming from the last offset.

For this problem, I try to compare the last committed offset and the end offset of a topic(only 1 partition), if the difference between these two offsets is larger than a certain amount, I will set the last committed offset of the topic as next offset so that I can abandon those redundant messages.

Now my problem is how to get the end offset of a topic, some people say I can use old consumer, but it's too complicated, do new consumer has this function?

like image 534
Neptune Avatar asked Jul 18 '16 03:07

Neptune


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.

How do you check last committed offset in Kafka?

To get the last committed offset of a topic partitions you can use the KafkaConsumer. committed(TopicPartition partition) function.

How do you check the offset of a topic in Kafka?

Short Answer. If your Kafka topic is in Confluent Cloud, use the kafka-console-consumer command with the --partition and --offset flags to read from a specific partition and offset. You can also read messages from a specified partition and offset using the Confluent Cloud Console: Run it.

What is last committed offset in Kafka?

It commits the offset, indicating that all the previous records from that partition have been processed. So, if a consumer stops and comes back later, it restarts from the last committed position (if assigned to that partition again). Note that this behavior is configurable.


2 Answers

The new consumer is also complicated.

//assign the topic consumer.assign();

//seek to end of the topic consumer.seekToEnd();

//the position is the latest offset consumer.position();

like image 101
lynn Avatar answered Sep 23 '22 12:09

lynn


You can also use the kafka server command line tools:

./bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic topic-name 

Output is of the form <topicName>:<partitionID>:<offset>, e.g. t1:0:0, see https://jaceklaskowski.gitbooks.io/apache-kafka/kafka-tools-GetOffsetShell.html for further details.

like image 34
Steven Avatar answered Sep 26 '22 12:09

Steven