Clients - kafka : Producer

Represents a Kafka producer endpoint.

Constructor

__init

(ProducerConfiguration config)

Remote Methods

close Closes the producer connection to the external Kafka broker.
 kafka:ProducerError? result = producer->close();
commitConsumer Commits the offsets consumed by the provided consumer.
commitConsumerOffsets Commits the consumer offsets in a given transaction.
flushRecords Flushes the batch of records already sent to the broker by the producer.
 kafka:ProducerError? result = producer->flushRecords();
getTopicPartitions Retrieves the topic partition information for the provided topic.
 kafka:TopicPartition[]|kafka:ProducerError result = producer->getTopicPartitions("kafka-topic");
send Produces records to the Kafka server.
 kafka:ProducerError? result = producer->send("Hello World, Ballerina", "kafka-topic");

Fields

  • producerConfig ProducerConfiguration? (default ())
  • Used to store configurations related to a Kafka connection

  • connectorId string (default system:uuid())
  • Unique ID for a particular connector

close

()

returns ProducerError?
Closes the producer connection to the external Kafka broker.
 kafka:ProducerError? result = producer->close();
  • Return Type

    (ProducerError?)
  • A kafka:ProducerError if closing the producer is failed or else ()

commitConsumer

(Consumer consumer)

returns ProducerError?
Commits the offsets consumed by the provided consumer.

Parameters

  • consumer Consumer
  • Consumer, which needs offsets to be committed

  • Return Type

    (ProducerError?)
  • Akafka:ProducerError if committing the consumer failed or else ()

commitConsumerOffsets

(PartitionOffset[] offsets, string groupID)

returns ProducerError?
Commits the consumer offsets in a given transaction.

Parameters

  • offsets PartitionOffset[]
  • Consumer offsets to commit for a given transaction

  • groupID string
  • Consumer group ID

  • Return Type

    (ProducerError?)
  • A kafka:ProducerError if committing consumer offsets failed or else ()

flushRecords

()

returns ProducerError?
Flushes the batch of records already sent to the broker by the producer.
 kafka:ProducerError? result = producer->flushRecords();
  • Return Type

    (ProducerError?)
  • A kafka:ProducerError if records couldn't be flushed or else ()

getTopicPartitions

(string topic)

returns TopicPartition[] | ProducerError
Retrieves the topic partition information for the provided topic.
 kafka:TopicPartition[]|kafka:ProducerError result = producer->getTopicPartitions("kafka-topic");

Parameters

  • topic string
  • Topic of which the partition information is given

  • Return Type

    (TopicPartition[] | ProducerError)
  • A kafka:TopicPartition array for the given topic or else a kafka:ProducerError if the operation fails

send

(anydata value, string topic, anydata? key, int? partition, int? timestamp)

returns ProducerError?
Produces records to the Kafka server.
 kafka:ProducerError? result = producer->send("Hello World, Ballerina", "kafka-topic");

Parameters

  • value anydata
  • Record contents

  • topic string
  • Topic to which the record will be appended

  • key anydata? (default <anydata?> ())
  • Key that will be included in the record

  • partition int? (default <int?> ())
  • Partition to which the record should be sent

  • timestamp int? (default <int?> ())
  • Timestamp of the record in milliseconds since epoch

  • Return Type

    (ProducerError?)
  • A kafka:ProducerError if send action fails to send data or else ()