Node.js kafka-node分区器类型用法

时间:2016-05-13 16:20:00

标签: node.js apache-kafka kafka-producer-api

我正在尝试使用kafka-node通过节点js将数据插入到kafka主题中。

如果我尝试使用partitionerType,则不会插入数据。如果我删除PartitionerType(即不使用选项),代码可以工作,但在这种情况下,所有数据只会转到一个分区。

请你帮我解决这个问题。提前谢谢。

var kafka = require('kafka-node'),
    Producer = kafka.Producer,
    KeyedMessage = kafka.KeyedMessage,
    client = new kafka.Client("zookeeper-host:2181","node-id"),
    options = {
    requireAcks: 1,
    ackTimeoutMs: 100,
    partitionerType: 3
        },
    producer = new Producer(client, options);
    //producer = new Producer(client);  -- This works

    var km = new KeyedMessage('key1', 'message6'),
    kn = new KeyedMessage('key2', 'message5'),
    kv = new KeyedMessage('key3', 'message4'),
    kx = new KeyedMessage('key4', 'message3'),
    ky = new KeyedMessage('key5', 'message2'),
    kz = new KeyedMessage('key6', 'message1'),
    payloads = [
        { topic: 'test-topic3', messages: [km,kn,kv,kx,ky,kz] }
    ];
    console.log("Ready to send data");
    producer.on('ready', function () {
    producer.send(payloads, function (err, data) {
        console.log(data);
    });
    });

console.log("DATA SENT");

非常感谢

帕里

2 个答案:

答案 0 :(得分:0)

上面的代码可以正常使用HighLevelConsumer。

由于 彩

答案 1 :(得分:0)

使用refreshMetadata方法...

就我而言,它有效。

请参阅以下链接......

https://github.com/SOHU-Co/kafka-node/issues/436

public FileSystemResource get(String file) { try { // First try to resolve as URL (file:...) Path path = Paths.get(new URL(file).toURI()); FileSystemResource resource = new FileSystemResource(path.toFile()); return resource; } catch (URISyntaxException | MalformedURLException e) { // If given file string isn't an URL, fall back to using a normal file return new FileSystemResource(file); } }