如何使用kafka-node库从kafka服务器获取主题列表?

时间:2016-06-22 17:51:14

标签: javascript node.js apache-kafka

我想在kafka服务器上创建新主题但在此之前我想从我的服务器检索主题列表,一旦建立连接,我怎样才能实现?

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);

1 个答案:

答案 0 :(得分:1)

使用kafka-node无法直接实现此目的,但您可以使用node-zookeeper-client执行此操作。如果您已实例kafka.Client,则您已有权访问zookeeper客户端。你可以这样做:

client.zk.client.getChildren("/brokers/topics", (err, children, stats) => {
  children.forEach(child => console.log(child));
});