kafka producer - 如何知道哪个键将转到哪个分区?

kafka producer - how to know which key will go to what partition?

我知道 kafka console producer 命令,它非常方便。我想知道有没有办法确定当我们也有密钥时哪个密钥将转到哪个分区?假设我们在 kafka 主题中有 10 个分区,producer 将如何决定将密钥分配给哪个分区?

我认为它可能会使用 key.toString.hashCode() % (num_of_partitons),但我认为这不是 kafka console producer 使用的方式。

我们可以检查将数据发送到哪个分区生产者吗?

如果使用默认的分区程序,它不会对键字符串值进行哈希编码,而是使用 Murmur2 hashing algorithm as seen in the org.apache.kafka.clients.producer.internals.DefaultPartitioner 代码:

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

算法的实现可以参考here.