Get last message from kafka consumer console script

Paul Leclercq picture Paul Leclercq · Oct 16, 2015 · Viewed 53.3k times · Source

We can get every messages from Kafka by doing:

 bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning

Is there a way to get only the last message ?

EDIT:

If you just want to monitor some messsages (--max-messages 10) in your stream, a convenient command is :

watch -n5 "./bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic auction --max-messages 10"

Answer

Aydin K. picture Aydin K. · Apr 26, 2018

I'm not aware of any automatism, but using this simple two step approach, it should work. Note that in my case it was a partitioned topic, you can leave the params for it out in case you have a unpartitioned one:

1) Get max offset for your topic (+ their partitions):

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

mytopic:2:11
mytopic:1:7
mytopic:0:15
mytopic:3:8

2) Choose a topic (+ partition) and provide the offset - n as parameter:

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic mytopic --offset 10 --partition 0  

The last n messages of the topic will be printed to the console. In my example, it will show 5 messages (= 15-10).