我跟着this tutorial在Ubuntu 14.04服务器上安装Kafka。为生产者和消费者提供的例子都很好。
这是生产者命令:
echo "Hello, World" | ~/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic TutorialTopic > /dev/null
这是消费者命令:
~/kafka/bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic TutorialTopic --from-beginning
但是,我正在尝试在Node.js中编写类似的使用者,并且在创建使用者之前它不会显示主题中存在的消息。这是我的代码。完成我正在尝试的内容需要什么附加配置?
var kafka = require('kafka-node')
var Consumer = kafka.Consumer
var client = new kafka.Client("localhost:2181/")
var consumer = new Consumer(
client,
[
{ topic: 'TutorialTopic', partition: 0, offset: 0}
],
{
fromOffset: true
}
);
consumer.on('message', function (message) {
console.log("received message", message);
});
答案 0 :(得分:3)
使用节点v6.11.0和kafka-node 1.6.2
,上述功能对我无效以下代码:
var kafka = require('kafka-node'),
Consumer = kafka.Consumer,
client = new kafka.Client("localhost:2181/"),
consumer = new Consumer(
client,
[
{ topic: 'TutorialTopic', partition: 0, offset: 0 }
],
{ fromOffset: true }
);
consumer.on('message', function (message)
{
console.log(message);
});
consumer.on('error', function (err)
{
console.log('ERROR: ' + err.toString());
});
答案 1 :(得分:2)
我认为发生的事情是<div><!-- This is the wrapper -->
<input type="text" class="userInput">
<button class="submitter">Submit</button>
<div class="output" style="white-space: pre-wrap; display: inline;"></div>
</div>
<div><!-- This is the wrapper -->
<input type="text" class="userInput">
<button class="submitter">Submit</button>
<div class="output" style="white-space: pre-wrap; display: inline;"></div>
</div>
被调用之前,consumer.on(...)
已经从主题中读过。相反,试试这个:
kafka-node
答案 2 :(得分:2)
{{1}}