apache-kafkakafka-topic

kafka producer - how to know which key will go to what partition?


I am aware of kafka console producer command and it is quite handy. I want to know is there a way to know for sure which key will go to which partition when we have key as well? Assuming we have 10 partitions in kafka topic, how will producer decide to which partition the key will go?

I thought it might use key.toString.hashCode() % (num_of_partitons), but i don't think this is the way kafka console producer employs.

Can we check to which partition producer will send data to?


Solution

  • If the default partitioner is used, it will not hash code the keys string value, instead it uses a Murmur2 hashing algorithm as seen in the org.apache.kafka.clients.producer.internals.DefaultPartitioner code:

    Utils.toPositive(Utils.murmur2(keyBytes)) % numPartitions;
    

    The implementation of the algorithm can be found here.