Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kafka console consumer: How to get only the last N messages from a topic instead of everything from the beginning?

Tags:

apache-kafka

I can do this:

./bin/kafka-avro-console-consumer --zookeeper 10.0.0.225:2181/kafka --topic myTopic --property schema.registry.url=http://10.0.0.100:8081  --from-beginning 

But I have too many messages. I would rather only get the last N ones. How can I do that with kafka console consumer?

like image 536
TakeSoUp Avatar asked Aug 16 '16 19:08

TakeSoUp


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 you read a Kafka topic from the beginning?

If you want to process a topic from its beginning, you can simple start a new consumer group (i.e., choose an unused group.id ) and set auto. offset. reset = earliest . Because there are no committed offsets for a new group, auto offset reset will trigger and the topic will be consumed from its beginning.

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 .


1 Answers

If you want to stick with the bundled binaries, you need to use the simple consumer shell:

bin/kafka-simple-consumer-shell.sh --broker-list mybroker:9092 --topic mytopic --partition mypartition --offset myoffset 

I recommend kt; it's much faster, more lightweight and has better options.

like image 168
mlg Avatar answered Sep 21 '22 09:09

mlg