Reputation: 475
Suppose the number of partitions is fixed for a topic. And suppose I provide a key --- an array of bytes --- when publishing a message. Kafka will map the key onto a topic partition 0,1,2,3,...N-1.
I'd like to make a function that does the same thing: given N, and the key return the same partition number as would Kafka.
Is this this just (in pseudo-code) murmur3(key) % N?
TIA
Upvotes: 3
Views: 6435
Reputation: 6583
DefaultPartitioner
uses murmur3
function to calculate partition for messages with not null keys.
You can just extract algorithm and added to your code:
import org.apache.kafka.common.utils.Utils;
Utils.toPositive(Utils.murmur2(keyBytes)) % numPartitions;
Second option is to just use DefaultPartitioner
:
DefaultPartitioner partitioner = new DefaultPartitioner();
int partition = partitioner.partition(???, ???, ???, ???, ???, ???);
Upvotes: 4
Reputation: 3512
As far as I know, Kafka hashes a key and partition according to that hash value. Here is a helpful diagram and an example on how.
Utils.abs(java.util.Arrays.hashCode(key.asInstanceOf[Array[Byte])) % numPartitions
Just found another good example of partitioning!
Upvotes: 3