How to produce messages to selected partition using kafka-console-producer?

Here is your starting point:
partitioner.class setting in your Properties instance. In Kafka, the default implementation is kafka.producer.DefaultPartitioner.

The goal of that setting is:

The partitioner class for partitioning messages amongst sub-topics. The default partitioner is based on the hash of the key.

This means that if you want to change the behaviour of the default partitioner , such as targeting a specific partition, then you need to create your own implementation of kafka.producer.Partitioner interface.

I would suggest to be really careful when creating your own strategy and really, test it a lot and monitor your topics and their partitions.

When it is built as a JAR, you can set the path to the JAR in CLI CLASSPATH variable, then kafka-console-producer will be able to detect and use it.


Targeting a specicic partition is not possible, but the ConsoleProducer does support writing keyed messages to the topic.

Kafka will use the hash of the key to distribute the message into partitions, at least with the default behaviour.

Currently, the default separator is \t, so entering key[\t]message will distribute it amongst partitions:

key1    a-message

The separator can be changed by providing the key.separator configuration, for example:

kafka-console-producer --broker-list localhost:9092,localhost:9093 \
  --topic mytopic --property key.separator=,

Send messages like this:

key2,another-message

I have tested this with the default tab and a custom separator successfully. The messages were distributed to two separate partitions.


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=|

Tags:

Apache Kafka