Kafka Console Producer and Consumer Example

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.

Kafka Console Producer and Consumer

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.

  2. Create a Kafka Topic

    Create a topic named sampleTopic by running the following command.

  3. Create a Kafka Console Producer.

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

  4. Create a Kafka Console Consumer.

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

  5. Start typing messages in the producer. Consumer would get the messages via Kafka Topic.
    Kafka Console Producer and Consumer Example

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.