According to the current state of things (Kafka>=0.10.0.1), the kafka-console-producer.sh script and the underlying ConsoleProducer java class support sending data with a partition key but such support is disabled by default and has to be enabled from the CLI.
Note that the partition key is not necessarily the same as the "partition id". By default, the partition is calculated from the key using kafka.producer.DefaultPartitioner
, and you would either need to change that (for that you'd need to add the new Partitioner implementation to the classpath that kafka-console-producer.sh uses), or figure out which is the partition id from the partition key, depending on your use case and what your readers expect.
In any case, to enable parsing keys for the messages you'll need to set the property parse.key
to true (by default it's false). Also, if you want to use something different than a tab character, use key.separator
as specified in Cedric's answer.
In the end, the command line would be:
kafka-console.producer.sh --broker-list kafka:9092,kafka2:9092 \
--topic $TOPIC --property parse.key=true --property key.separator=|