How can I get the last/end offset of a kafka topic partition?
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.
For Kafka version : 0.10.1.1
// Get the diff of current position and latest offset
Set<TopicPartition> partitions = new HashSet<TopicPartition>();
TopicPartition actualTopicPartition = new TopicPartition(record.topic(), record.partition());
partitions.add(actualTopicPartition);
Long actualEndOffset = this.consumer.endOffsets(partitions).get(actualTopicPartition);
long actualPosition = consumer.position(actualTopicPartition);
System.out.println(String.format("diff: %s (actualEndOffset:%s; actualPosition=%s)", actualEndOffset -actualPosition ,actualEndOffset, actualPosition));
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();