To create a Kafka producer and send messages to a Kafka topic

Category : Apache Kafka | Sub Category : Apache Kafka | By Prasad Bonam Last updated: 2023-08-05 14:44:00 Viewed : 54

To create a Kafka producer and send messages to a Kafka topic:

To create a Kafka producer and send messages to a Kafka topic, you can use the script (or kafka-console-producer.bat on Windows) provided by Apache Kafka. This script allows you to produce and publish messages to a specific Kafka topic from the command-line.

The basic command to start a Kafka producer is as follows:

css --broker-list <BROKER_LIST> --topic <TOPIC_NAME>
  • --broker-list: Specifies the Kafka broker(s) to connect to in the format host:port. You need to provide at least one broker to connect the producer.

  • --topic: Specifies the name of the Kafka topic to which you want to send messages.

Example: Lets say you have a Kafka cluster running with a broker on localhost:9092, and you want to produce messages to a topic named "my_topic."

Using the command-line tool on Unix/Linux/Mac:

bin/ --broker-list localhost:9092 --topic my_topic

Using the command-line tool on Windows:

binwindowskafka-console-producer.bat --broker-list localhost:9092 --topic my_topic

Explanation: In the above example, we are using the (or kafka-console-producer.bat) script to create a Kafka producer. We specify the Kafka broker to connect to using the --broker-list option with the broker address localhost:9092.

We also specify the --topic option with the value "my_topic" to indicate that we want to produce and send messages to the "my_topic" Kafka topic.

Once you run the producer command, it will start waiting for you to enter messages from the console. Each line of text you enter in the console will be treated as a separate message and will be published to the specified Kafka topic. To send messages, type the text and press Enter. The messages will be sent to Kafka, and you can see the output in the console.

Example of sending messages:

Hello Kafka This is a test message Welcome to Kafka messaging

After you press Enter after each message, the producer will publish them to the "my_topic" topic in the Kafka cluster. The messages can then be consumed by Kafka consumers subscribed to the same topic.

Related Articles

Leave a Comment: