简体   繁体   English

Kafka节点:消费缓慢

[英]Kafka node: slow consumer

I am writing a kafka-node consumer and compared to the consumer in python, it is very slow and seems not to recieve all messages. 我正在编写一个kafka节点使用者,与python中的使用者相比,它非常慢,而且似乎无法接收所有消息。 I tried to adjust all possible parameters, but nothing works. 我试图调整所有可能的参数,但是没有任何效果。

I use this API: https://github.com/SOHU-Co/kafka-node#consumer 我使用此API: https : //github.com/SOHU-Co/kafka-node#consumer

and this is my code: 这是我的代码:

//--------------------Create a consumer---------------------------------------------------------------------------
var kafka = require('kafka-node');
var Consumer = kafka.Consumer;
var client = new kafka.Client('192.168.2.2:2181');
var consumer = new Consumer(client,
        [{ topic: 'clusterTest1', offset: 0}],
        {
            autoCommit: false,
            fromOffset: 'latest',
            outOfRangeOffset: 'latest',
            fetchMinBytes: 1,
            fetchMaxWaitMs: 50,
            fetchMaxBytes: 52428800

        }
    );

offset = new kafka.Offset(client);
    offset.fetch([
        { topic: 'clusterTest1', partition: 0, time: Date.now(), maxNum: 1 }
    ], function (err, data) {
        // data
        // { 't': { '0': [999] } }
    });


consumer.on('message', function (message) {
    console.log(message.value);
}); 

});

consumer.on('error', function (err) {
    console.log('Error:',err);
});

 consumer.on('offsetOutOfRange', function (topic) {
     console.log('---------Offset out of range--------');
  topic.maxNum = 2;
  offset.fetch([topic], function (err, offsets) {
    if (err) {
      return console.error(err);
    }
    var max= Math.max.apply(null, offsets[topic.topic][topic.partition]);
    consumer.setOffset(topic.topic, topic.partition, max);
  });
}); 

However, this python code works fine: 但是,此python代码可以正常工作:

from kafka import KafkaConsumer

consumer = KafkaConsumer(bootstrap_servers=['192.168.2.2:9092','192.168.2.3:9092','192.168.2.4:9092','192.168.2.5:9092'])
print('Erfolgreich verbunden')
print(consumer.subscribe('clusterTest1'))
print(consumer.subscription())
#print(consumer.DEFAULT_CONFIG)
for msg in consumer:
    print(msg.value)

Thanks for any help in advance. 感谢您的任何帮助。

Solved. 解决了。 I wasn't aware that there are multiple partitions. 我不知道有多个分区。 Now, I am using a consumerGroup and I can receive all messages. 现在,我正在使用一个consumerGroup,并且可以接收所有消息。

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

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