KafKa partitioner class, assign message to partition within topic using key

This is what I’ve found so far ..

Define our own custom partitioner class by implementing the kafka Partitioner interface. The implemented method will have two arguments, first the key that we provide from the producer and next the number of partition available. So we can define our own logic to set which key of message goes to what partition.

Now while creating the producer we can specify our own partitioner class using the “partitioner.class” attribute

    props.put("partitioner.class", "path.to.custom.partitioner.class");

If we don’t mention it then Kafka will use its default class and try to distribute message evenly among the partitions available.

Also inform Kafka how to serialize the key

    props.put("key.serializer.class", "kafka.serializer.StringEncoder");

Now if we send some message using a key in the producer the message will be delivered to a specific partition (based on our logic written on the custom partitioner class), and in the consumer (SimpleConsumer) level we can specify the partition to retrieve the specific messages.

In case we need to pass a String as a key, the same should be handled in the custom partitioner class ( take hash value of the key and then take first two digit etc )

Leave a Comment