import ballerinax/kafka;

kafka:Producer kafkaProducer = check new (kafka:DEFAULT_URL);

public function main() returns error? {
    string message = "Hello World, Ballerina";
    // Sends the message to the Kafka topic.
    check kafkaProducer->send({
                                topic: "test-kafka-topic",
                                value: message.toBytes() });

    // Flushes the sent messages.
    check kafkaProducer->'flush();
}

Producer

This is an example on how to send messages to a Kafka topic using a kafka:Producer object. The producer is configured to send string values and int keys. For this example to work properly, an active Kafka broker should be present.

For more information on the underlying module, see the Kafka module.

import ballerinax/kafka;
kafka:Producer kafkaProducer = check new (kafka:DEFAULT_URL);
public function main() returns error? {
    string message = "Hello World, Ballerina";
    check kafkaProducer->send({
                                topic: "test-kafka-topic",
                                value: message.toBytes() });

Sends the message to the Kafka topic.

    check kafkaProducer->'flush();
}

Flushes the sent messages.

bal run kafka_producer.bal