javascript - 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
);
})
解决方案
这显然是一个列表,您应该可以在消费者中添加多个主题。
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
}
);
推荐阅读
- javascript - ImagePicker 设置数据状态
- google-apps-script - 将徽标嵌入电子邮件
- ibm-cloud - IBM 沃森问题
- image - 如何在 eps 文件中将 type 3 字体转换为 type 1 字体?
- python - 'function' 和 'int' 的实例之间不支持 '<'
- single-sign-on - ADFS 作为服务提供商
- amazon-web-services - 如何在 cdk deploy 命令中使用 aws 角色 arn
- c++ - C ++小函数不内联
- c++ - 为什么我不能从返回指针的函数中直接返回对指针的引用?
- ajax - 我们如何以快速而充分的方式将我们的表情符号 png 文件从服务器获取到浏览器?