When working with Kafka you might find yourself using the kafka-console-producer (kafka-console-producer.sh). The kafka-console-producer is a program included with Kafka that creates messages from command line input (STDIN).

However, simply sending lines of text will result in messages with null keys. In order to send messages with both keys and values you must set the parse.key and key.separator properties on the command line when running the producer.

The below example sets the parse.key property to true, and specifies the key.separator as “:“. The keys in the sample messages below are “key1”, “key2”, “key3”, with the values being “value1”, “value2”, “value3”.

$KAFKA_HOME/bin/kafka-console-producer.sh \
  --broker-list localhost:9092 \
  --topic my-topic \
  --property "parse.key=true" \
  --property "key.separator=:"
key1:value1
key2:value2
key3:value3

If you haven’t installed Kafka yet, see our Kafka Quickstart Tutorial to get up and running quickly.

Leave a Reply

Sending Key Value Messages with the Kafka Console Producer