Commands | Kafka Produce Messages

Use the Produce(topic, value, key, partition) method to publish a record to a Kafka topic. The key and partition arguments are optional; when no partition is set, the broker selects one (for example by hashing the key). Call ProduceBytes to publish a binary payload.

Configure the delivery guarantee with KafkaOptions.Producer.Acks: kafkaAcksNone (fire and forget), kafkaAcksLeader (wait for the partition leader) or kafkaAcksAll (wait for all in-sync replicas). You can also set Producer.TimeoutMs and Producer.Compression (kafkaCompressionNone or kafkaCompressionGzip).

After a record is sent, the OnKafkaProduce event reports the topic, partition, offset and error code of the produced record.

Basic Usage


oKafka.KafkaOptions.Producer.Acks := kafkaAcksAll;
oKafka.KafkaOptions.Producer.TimeoutMs := 5000;
oKafka.KafkaOptions.Producer.Compression := kafkaCompressionGzip;

oKafka.Produce('my-topic', 'hello world', 'key1');

procedure OnKafkaProduce(Sender: TObject; const Topic: string; Partition: Integer; Offset: Int64; ErrorCode: Integer);
begin
  if ErrorCode = 0 then
    ShowMessage(Format('Produced to %s [%d] at offset %d', [Topic, Partition, Offset]))
  else
    ShowMessage(Format('Produce error %d', [ErrorCode]));
end;