简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM