Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last message from kafka consumer console script

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"

like image 431
Paul Leclercq Avatar asked Oct 16 '15 12:10

Paul Leclercq


People also ask

How do I get the last message in Kafka?

Use -o -5 for last 5 messages, etc. Show activity on this post. With KafkaCat (https://docs.confluent.io/platform/current/app-development/kafkacat-usage.html) last N messages of an Apache Kafka Topic can be read.

How do I fetch specific messages in Apache Kafka?

Assuming that we want to consume the messages between offsets 11 and 21 , we need to run the following kafkacat command: -o 11 denotes that the consumer should start fetching messages from offset 11 , and -c 10 specifies that we only need to consume the first ten messages starting from offset 11 .

How do I query a message in Kafka topic?

The only fast way to search for a record in Kafka (to oversimplify) is by partition and offset. The new producer class can return, via futures, the partition and offset into which a message was written. You can use these two values to very quickly retrieve the message.

Which command is used in Kafka to retrieve messages from a topic?

Kafka provides the utility kafka-console-consumer.sh which is located at ~/kafka-training/kafka/bin/kafka-console-producer.sh to receive messages from a topic on the command line.


1 Answers

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).

like image 85
Aydin K. Avatar answered Sep 25 '22 03:09

Aydin K.