In this article, I will download and install Apache Kafka. Then I create a Kafka topic, produce messages to it, and consume them. I use the terminal to perform all the operations.
Kafka needs Java to run. If you don't have Java installed, now is the time to do so.
The example commands are in Mac/Linux systems. For Windows, use the
Navigate to Apache Kafka download page and download the Kafka binary.
At the time of this article, the latest release was
That's it. Now I have Kafka installed on my machine.
Navigate to the folder where Kafka resides. In my case, that folder is
Zookeeper monitors brokers belonging to the Kafka cluster.
It appoints and manages leaders for partitions in case of resource failures and a lot of housekeeping.
Note that the zookeeper is being sunset from the new versions of Kafka, but those changes are still not production ready.
Here is how to start zookeeper. Make sure to navigate to the kafka folder.
bin/zookeeper-server-start.sh config/zookeeper.properties
That will start the zookeeper on port 2181. I can change that port by editing the
That is also called the broker. Before starting the Kafka server, I must specify the hostname for listeners to listen. Open the
listeners=PLAINTEXT://localhost:9092
Then start the server.
bin/kafka-server-start.sh config/server.properties
That will start the Kafka broker on port 9092. I can change that port by editing the
I need a topic to publish messages on the broker. The below command creates the topic
bin/kafka-topics.sh --create --topic TEST-TOPIC --bootstrap-server localhost:9092
Here is how to verify the topic creation.
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
Producers publish messages to topics. I can create a simple producer by using Kafka's built-in producer script.
bin/kafka-console-producer.sh --topic TEST-TOPIC --broker-list localhost:9092
The above command enables me to publish messages to the topic
Kafka has a built-in script to create consumers to consume messages through the terminal.
bin/kafka-console-consumer.sh --topic TEST-TOPIC --bootstrap-server localhost:9092
If you want to consume all the messages in the topic, append
bin/kafka-console-consumer.sh --topic TEST-TOPIC --bootstrap-server localhost:9092 --from-beginning