Apache Kafka Producer Example Using CLI
Before we dive in, you need to ensure that you have an Apache Kafka cluster running on your machine. You can quickly spin up a Kafka container using the docker-compose which is provided in the YAML file as shown in the following:
Run the following command to start the container as shown in the following:
This should start the container and map the zookeeper and Kafka services to ports 2181 and 9092, respectively.
Produce Messages from CLI
To produce a message to a Kafka topic, you must ensure that the Kafka topic exists and use the Kafka-console-producer.sh utility.
Start by creating an example topic for the cluster with the “kafka-topics.sh” command. An example is shown in the following:
The previous command creates a Kafka topic called “sample_topic” with one partition and one replica.
To start producing messages to the topic that we previously created, we can use the kafka-console-producer.sh
The command is as shown in the following:
Once you run the previous command, Kafka opens an input buffer where you can produce messages to the Kafka topic. Each message takes up one line.
To end the message input, you can terminate it using the CTRL+ C input.
Keep in mind that the messages are sent with a null key by default.
We explored the basics of working with the Kafka Producer CLI tool to write the messages to an existing Kafka topic.
Leave a Reply
You must be logged in to post a comment.