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.

1 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.

$ bin/zookeeper-server-start.sh config/zookeeper.properties
$ bin/kafka-server-start.sh config/server.properties

2 Create a Kafka Topic

Create a topic named sampleTopic by running the following command.

$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic sampleTopic

3 Create a Kafka Console Producer

Run the following command to start a Kafka Producer, using console interface, writing to sampleTopic.

$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic sampleTopic

4 Create a Kafka Console Consumer

Run the following command to start a Kafka Producer, using console interface, subscribed to sampleTopic.

$ bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic sampleTopic --from-beginning

5 Send Messages

Start sending messages from the producer. Consumer would get the messages via Kafka Topic.

Conclusion

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.