Skip to content
Advertisement

Specifying a null partition in a Kafka Producer

The Kafka ProducerRecord api has a few different constructors.

I want to be able to specify the topic, timestamp, key and value; but not the partition.

The closest option I can see is the following, but it forces me to specify a partition number:

public ProducerRecord(String topic, Integer partition, Long timestamp, K key, V value)

Is it possible to just put null there, to then use the standard partitioning algorithm based on the key; or will this have unwanted side-effects?

Advertisement

Answer

The Kafka Documentation states:

If a valid partition number is specified that partition will be used when sending the record. If no partition is specified but a key is present a partition will be chosen using a hash of the key. If neither key nor partition is present a partition will be assigned in a round-robin fashion.

The constructors that don’t allow a partition to be specified also pass null into that parameter internally; so using null will give the expected behaviour

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement