According to the Kafka documentation:
The producer is responsible for choosing which message to assign to which partition within the topic.
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 , 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.