How To Use Kafka - Basic Usage

In this guide, you will learn the very basics of how to run Kafka with Docker containers locally and how to produce and consume Kafka messages with CLI commands.
For more information, you can visit Kafka documentation https://kafka.apache.org/documentation/

Prerequisite:

  • Docker + Docker account.

 

Step one - Run Kafka

  1. Save the following Docker-compose.yml into a folder named “Kafka” on your PC.

2. Open CMD in the “Kafka” folder location and run docker-compose up -d.

Note: It might take some time for the first time.

3. When all of the containers are up, you are ready to go.

You can also download it from Apache Kafka and install into your PC
https://kafka.apache.org/downloads


Step two - Produce/ consume Kafka messages:

  1. Open CMD.

2. The command for producing messages to a topic:

docker-compose exec broker kafka-console-producer --broker-list localhost:9092 --topic <topic>

For example:

docker-compose exec broker kafka-console-producer --broker-list localhost:9092 --topic myTopic
> {enter your message} >

Note Kafka messages should be sent in JSON format.

3. The command for consuming messages from a topic:

For example: