问题描述
我有一个必须从kafka服务器读取的主题,因此,我只需要创建可以从kafka主题读取数据的使用者,我总是会得到错误主题.
I have topic that i have to read from kafka server so for that i just need to create consumer that can read data from kafka topic, I always get error topic does not exist.
1-如何确定kafka连接已建立?
1- How can i make sure kafka connection is established ?
2-如何从kafka中的特定主题获取数据?
2- How to get the data from specific topic in kafka ?
main.js
var kafka = require('kafka-node');
var config = require('./config.js');
var kafkaConn = config.kafkaCon.dit;
var HighLevelConsumer = kafka.HighLevelConsumer;
//var HighLevelProducer = kafka.HighLevelProducer;
var Client = kafka.Client;
var Offset = kafka.Offset;
var topics = [{topic: 'UEQ'}];
var client = new Client(kafkaConn);
var payloads = [ { topic: topics, partition : 0}];
var options = {
groupId: 'kafka-node-group',
// Auto commit config
autoCommit: true,
autoCommitMsgCount: 100,
autoCommitIntervalMs: 5000,
// Fetch message config
fetchMaxWaitMs: 100,
fetchMinBytes: 1,
fetchMaxBytes: 1024 * 10,
};
var consumer = new HighLevelConsumer(client, payloads, options);
consumer.on('message', function (message) {
console.log('TEST',this.id, message);
});
错误
events.js:141
throw er; // Unhandled 'error' event
^
TopicsNotExistError: The topic(s) [object Object] do not exist
at new TopicsNotExistError (C:\uilogging\node_modules\kafka-node\lib\errors\
TopicsNotExistError.js:11:11)
推荐答案
我正在做一个类似的项目,其中我在自己的服务器上有一个Kafka生产者,并且正在使用Kafka-Node作为我的应用程序的使用者.我对Kafka-Node相当陌生,并且没有太多经验,但是我可以尝试分享一些我发现的见解.
I am doing a similar project where i have a Kafka producer on its own server and am using Kafka-Node as a consumer for my application. I am fairly new to Kafka-Node, and don't have much experience with it, but i can try to share some of the insights i have found.
我相信您的问题实际上是您的主题不存在.
I believe your problem is literally that your topic doesn't exist.
如果您的连接没有建立,我认为它不会继续说该主题不存在.当我键入一个不存在的主题并且为我的Kafka生产者键入一个随机IP时,没有任何错误.但是,当我指向正确的ip,并且仍然有不正确的主题时,就会收到与您看到的相同的错误.
If your connection wasn't established, i don't think it would move on to say the topic doesn't exist. When i type in a topic that doesn't exist and i type a random ip for my Kafka producer, nothing errors out. But when i point to the correct ip, and an still have incorrect topic, i get the same error you see.
var kafka = require('kafka-node');
var Consumer = kafka.Consumer,
// The client specifies the ip of the Kafka producer and uses
// the zookeeper port 2181
client = new kafka.Client("<ip to producer>:2181"),
// The consumer object specifies the client and topic(s) it subscribes to
consumer = new Consumer(
client, [ { topic: 'myTopic', partition: 0 } ], { autoCommit: false });
consumer.on('message', function (message) {
// grab the main content from the Kafka message
var data = JSON.parse(message.value);
console.log(data);
});
希望这不会太晚.
这篇关于如何使用kafka-node从主题读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!