Tuesday, 5 November 2019

Kafka Cli: Consume pre-exist messages from topic


By default, a consumer reads the messages from the time when it is created, if you want to read all the messages in the topic (Which are there before consumer creation), you need to use ‘--from-beginning’ option. I will explain about this in my next post.

Step 1: Create a topic.
kafka-topics.sh --bootstrap-server localhost:9092 --topic myFirstTopic --create --partitions 3 --replication-factor 1

$kafka-topics.sh --bootstrap-server localhost:9092 --topic myFirstTopic --create --partitions 3 --replication-factor 1
$
$kafka-topics.sh --bootstrap-server localhost:9092 --list
__consumer_offsets
myFirstTopic

Step 2: Produce some messages to the topic.


kafka-console-producer.sh --broker-list localhost:9092 --topic myFirstTopic --producer-property acks=1
$kafka-console-producer.sh --broker-list localhost:9092 --topic myFirstTopic --producer-property acks=1
>first message
>second message

Step 3: Let’s start the consumer using ‘--from-beginning’ option.

kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic myFirstTopic --from-beginning
$kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic myFirstTopic --from-beginning
first message
second message




Previous                                                    Next                                                    Home

No comments:

Post a Comment