How to check which partition is a key assign to in kafka?

谁说我不能喝 提交于 2019-12-02 05:52:28

As explained here or also in the source code

You need the byte[] keyBytes assuming it isn't null, then using org.apache.kafka.common.utils.Utils, you can run the following.

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

For strings or JSON, it's UTF8 encoded, and the Utils class has helper functions to get that.
For Avro, such as Confluent serialized values, it's a bit more complicated (a magic byte, then a schema ID, then the data). See Wire format

only goes to 1 partition

This isn't a guarantee. Hashes can collide.

It makes more sense to say that a given key isn't in more than one partition.

if the cluster is not rebalancing

Rebalancing will still preserve a partition value.

when you send message, Partitions are determined by the following classes

https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/clients/producer/internals/DefaultPartitioner.java

If you want change logics, implement org.apache.kafka.clients.producer.Partitioner interface and, set ProduceConfig's 'partitioner.class'

reference docuement : https://kafka.apache.org/documentation/#producerconfigs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!