首页 > 解决方案 > Consume multiple topics in a single consumer on kafka

问题描述

Is there any way for kafka to cosume on a single consumer with multiple topics?

Here is my consumer code, it works fine with single topic

let consumer = new Consumer(
        client,
        [{ topic: '1234', offset: 0, partition: 0 }],
        {
            autoCommit: true,
            fetchMaxWaitMs: 1000,
            fetchMaxBytes: 1024 * 1024,
            encoding: 'utf8',
            fromOffset: false
        }
    );
    consumer.on('message', async function (message: any) {
        console.log(
            'kafka-> ',
            message.value
        );
    })

标签: javascriptnode.jsapache-kafkakafka-consumer-api

解决方案


这显然是一个列表,您应该可以在消费者中添加多个主题。

https://www.npmjs.com/package/kafka-node#consumer说你可以做如下。(最好不要分配分区,让动物园管理员处理)

var kafka = require('kafka-node'),
Consumer = kafka.Consumer,
client = new kafka.KafkaClient(),
consumer = new Consumer(
    client,
    [
        { topic: 't', partition: 0 }, { topic: 't1', partition: 1 }
    ],
    {
        autoCommit: false
    }
);

推荐阅读