Below
statement is used to receive messages from a topic.
kafka-console-consumer
--bootstrap-server {serverDetails}--topic {topicName}
Let’s see
it with an example.
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
myFirstTopic
$
Step 2:
Start a consumer.
Open
terminal and execute command.
kafka-console-consumer.sh
--bootstrap-server localhost:9092 --topic myFirstTopic
Step 3:
Open other terminal
and start sending messages to the topic.
kafka-console-producer.sh
--broker-list localhost:9092 --topic myFirstTopic
Whenever
you send a message to topic, you can observe that the message is received at
consumer end.
At
Producer End
$kafka-console-producer.sh --broker-list localhost:9092 --topic myFirstTopic
>My First Message
>My Second Message
>
At
Consumer End
$kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic myFirstTopic
My First Message
My Second Message
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.
No comments:
Post a Comment