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"
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.
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 .
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With