简体   繁体   English

nodejs和kafka键分区程序

[英]nodejs and kafka keyed partitioner

I am trying to use keyed partitioning with kafka Highlevelproducer, on a 4 partitions topic I have the code below: 我正在尝试使用kafka Highlevelproducer的键分区,在4个分区主题中,我有以下代码:

   var kafka = require('kafka-node'),
      HighLevelProducer = kafka.HighLevelProducer,
      client = new kafka.Client(Host+":"+Port,client_id),
      producer = new HighLevelProducer(client,{partitionerType: 3});
.
.
   theKey = theKey+1;
   if ( theKey > Nb_key ) {
      theKey = 0;
   }
   var payloads = [
        { topic: Topic, key: theKey, messages: JSON.stringify({"hello": "world", "Timestamp": +timestamp}) }
      ];
      producer.send(payloads, function (err, data) {
      });

I checked that messages have the correct key value, but still, all messages are sent to partition 1. Do you see anything wrong with what I am doing ? 我检查了消息是否具有正确的键值,但仍然将所有消息发送到分区1。您发现我的操作有问题吗?

Thanks 谢谢

Looking at the code, I think I understood the problem. 查看代码,我想我理解了问题。 The baseProducer is doing a hash on the key as a string, and the result is always the same, so always same partition. baseProducer将键作为字符串对键进行哈希处理,结果始终相同,因此分区始终相同。 I wrote my own custom partitioner, doing a simple modulo on the key with the nb of partitions, it works perfectly. 我编写了自己的自定义分区程序,对分区nb的键进行了简单的模运算,它可以完美地工作。 See code below: 参见下面的代码:

   var MyPartitioner = function (partitions, key) {
   key = key || '0';
   var index = parseInt(key) % partitions.length;
   return partitions[index];
   };
   var kafka = require('kafka-node'),
      HighLevelProducer = kafka.HighLevelProducer,
      client = new kafka.Client(Host+":"+Port,client_id),
      producer = new HighLevelProducer(client,{requireAcks: 0,partitionerType: 4},MyPartitioner );
      producer.on('ready', function () {
      timer.setInterval(send_data, '',SendIntervalInUsecs+'u');
   });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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