ballerinax/kafka Ballerina library

2.1.0-alpha8

Package Overview

This package is used to interact with Kafka Brokers via Kafka Consumer and Kafka Producer clients. This package supports Kafka 1.x.x and 2.0.0 versions.

For information on the operations, which you can perform with this package, see the below Functions. For examples on the usage of the operations, see the following.

Basic Usages

Publishing Messages
  1. Initialize the Kafka message producer.
Copy
kafka:ProducerConfiguration producerConfiguration = {
    clientId: "basic-producer",
    acks: "all",
    retryCount: 3
};

kafka:Producer kafkaProducer = check new (kafka:DEFAULT_URL, producerConfiguration);
  1. Use the kafka:Producer to publish messages.
Copy
string message = "Hello World, Ballerina";
check kafkaProducer->send({ topic: "test-kafka-topic",
                            value: message.toBytes() });
Consuming Messages
  1. Initializing the Kafka message consumer.
Copy
kafka:ConsumerConfiguration consumerConfiguration = {
    groupId: "group-id",
    offsetReset: "earliest",
    topics: ["kafka-topic"]
};

kafka:Consumer consumer = check new (kafka:DEFAULT_URL, consumerConfiguration);
  1. Use the kafka:Consumer as a simple record consumer.
Copy
kafka:ConsumerRecord[]|kafka:Error result = consumer->poll(1);
  1. Use the kafka:Listener as a listener.
Copy
listener kafka:Listener kafkaListener = new (kafka:DEFAULT_URL, consumerConfiguration);

service kafka:Service on kafkaListener {
    remote function onConsumerRecord(kafka:Caller caller,
                                kafka:ConsumerRecord[] records) {
    }
}

Import

import ballerinax/kafka;Copy

Metadata

Released date: almost 3 years ago

Version: 2.1.0-alpha8


Compatibility

Platform: java11

Ballerina version: slalpha5

GraalVM compatible: Yes


Pull count

Total: 15518

Current verison: 1


Weekly downloads


Other versions

See more...