如何使用 kafka-node 库从 kafka 服务器获取主题列表? [英] How can i get list of topics from kafka server using kafka-node library?
本文介绍了如何使用 kafka-node 库从 kafka 服务器获取主题列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 kafka 服务器上创建新主题,但在此之前我想从我的服务器检索主题列表,一旦建立连接,我该如何实现?
I wanted to create new topic on kafka server but before that i want to retrieve topic list from my server , How can i achieve that once connection is established ?
main.js
var groupId = 'test1';
var clientId = "consumer-" + Math.floor(Math.random() * 10000);
var options = {autoCommit: true, fetchMaxWaitMs: 100, fetchMaxBytes: 10 * 1024 * 1024, groupId: groupId};
console.log("Started consumer: ", clientId);
var consumer_client = new Client(kafkaConn,clientId);
var client = new Client(consumer_client.connectionString,clientId);
var consumer = new HighLevelConsumer(client, topics, options);
推荐答案
没有真正直接的方法可以用 kafka-node
做到这一点,但你可以用 node-zookeeper-client 做到这一点代码>.如果您已经实例化了
kafka.Client
,则您已经可以访问 zookeeper
客户端.你可以这样做:
No real direct way to do this with kafka-node
, but you can do it with node-zookeeper-client
. If you have a kafka.Client
instantiated, you already have access to a zookeeper
client. You can just do:
client.zk.client.getChildren("/brokers/topics", (err, children, stats) => {
children.forEach(child => console.log(child));
});
这篇关于如何使用 kafka-node 库从 kafka 服务器获取主题列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文