How to produce ad-hoc Kafka messages on Windows?
November 25, 2017Navigate to your Kafka bin/windows directory and start your power shell console. Use one of following commands.
Manually add messages one by one
./kafka-console-producer.bat --broker-list kafka:9092 --topic test
>value1
>value2
Manually add messages with keys one by one
> ./kafka-console-producer.bat --broker-list kafka:9092 --topic test --property "parse.key=true" --property "key.separator=;"
>key1;value1
>key2;value2
Just add parse.key and key.separator properties. The example uses ; but there is nothing special about it, one can use any other character.
Automatically add multiple messages with keys
1..1000 | %{"Key$_;Message$_"} | Write-Output | ./kafka-console-producer.bat --property "parse.key=true" --property "key.separator=;" --broker-list kafka:9092 --topic test
The command adds 1000 messages with keys using simple PowerShell script.
1..1000generates numbers from 1 to 1000, adjust numbers according to your needs.%{"Key$_;Message$_"}for each number string is prepared with key and value separated by;.$_is PowerShell context variable that is equal to a currently processed number.Write-Outputsends all the generated strings to standard input ofkafka-console-producer.
