send key value messages to kafka from console producer

January 20, 2017    kafka bash blog

Kafka provides kafka-console-producer.sh to send messages from stdin

> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
message1
message2

or file.

> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test < messages.txt

messages send from above methods are parsed as values. If you want to send 10 messages from console producer with explicitly defined key.

	for (( i=1; i<=10; i++ )); do echo "key$$i:value$$i" | bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test --property "parse.key=true" --property "key.separator=:"; done;