Kafka Console Producer and Consumer Example
Kafka Console Producer and Consumer Example – In this Kafka Tutorial, we shall learn to create a Kafka Producer and Kafka Consumer using console interface of Kafka.
bin/kafka-console-producer.sh and bin/kafka-console-consumer.sh in the Kafka directory are the tools that help to create a Kafka Producer and Kafka Consumer respectively.
We shall start with a basic example to write messages to a Kafka Topic read from the console with the help of Kafka Producer and read the messages from the topic using Kafka Consumer.
Start Zookeeper and Kafka Cluster
Navigate to the root of Kafka directory and run each of the following commands in separate terminals to start Zookeeper and Kafka Cluster respectively.1$ bin/zookeeper-server-start.sh config/zookeeper.properties1$ bin/kafka-server-start.sh config/server.properties
Create a Kafka Topic
Create a topic named sampleTopic by running the following command.123$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic sampleTopic
Create a Kafka Console Producer.
Run the following command to start a Kafka Producer, using console interface, writing to sampleTopic.123$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic sampleTopic
Create a Kafka Console Consumer.
Run the following command to start a Kafka Producer, using console interface, subscribed to sampleTopic.123$ bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic sampleTopic --from-beginning
- Start typing messages in the producer. Consumer would get the messages via Kafka Topic.
In this Apache Kafka Tutorial – Kafka Console Producer and Consumer Example, we have learnt to start a Kafka Producer and Kafka Consumer using console interface.