Sending Messages to Kafka Topics

Apache Kafka is a popular distributed streaming platform that allows you to build real-time data streaming applications. One of the key features of Kafka is its ability to handle large volumes of data efficiently. In this article, we'll explore how to send messages to Kafka topics, which is the first step in the Kafka messaging pipeline.


Before we begin, make sure you have the following prerequisites in place:

  • Apache Kafka installed and running.
  • Basic understanding of Kafka concepts like brokers, topics, and partitions.

Kafka Producers

In Kafka, producers are responsible for sending messages to Kafka topics. A producer can be written in any programming language, as Kafka provides client libraries for various languages like Java, Python, and more.

To send messages to Kafka topics, follow these steps:

  1. Create a Kafka producer instance by configuring the necessary properties like the Kafka broker address, serializer class, and more.
  2. Define the topic to which you want to send the messages.
  3. Call the send() method of the producer instance and provide the topic name and message as parameters.

Here's an example of sending a message using the Java client library:

import org.apache.kafka.clients.producer.*;

public class KafkaProducerExample {
    private static final String TOPIC_NAME = "my_topic";
    private static final String BOOTSTRAP_SERVERS = "localhost:9092";

    public static void main(String[] args) {
        Properties props = new Properties();
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");

        Producer<String, String> producer = new KafkaProducer<>(props);

        String message = "Hello, Kafka!";
        ProducerRecord<String, String> record = new ProducerRecord<>(TOPIC_NAME, message);


In this example, we create a Kafka producer instance with the necessary configurations. Then, we define the topic name and the message we want to send. Finally, we create a ProducerRecord object with the topic name and message, and call the send() method of the producer instance to send the message to the topic.

Sending Messages with Kafka Command Line Tools

Kafka also provides command line tools that allow you to interact with Kafka topics without writing any code. One such tool is kafka-console-producer, which lets you send messages from the command line.

To use kafka-console-producer, open a terminal and run the following command: --broker-list localhost:9092 --topic my_topic

Replace localhost:9092 with the address of your Kafka broker, and my_topic with the name of the Kafka topic to which you want to send messages.

Once the command starts, you can enter messages line by line. Press Ctrl + D to exit the producer.


Sending messages to Kafka topics forms the foundation of building real-time data streaming applications. Whether you prefer writing code using Kafka client libraries or using the convenient command line tools, Kafka provides multiple ways to send messages efficiently. By leveraging these capabilities, you can unlock the full potential of Kafka in your data streaming projects.

noob to master © copyleft