简体   繁体   English

如何在nodejs中创建带分区的kafka主题?

[英]How to create kafka topic with partitions in nodejs?

I am using kafka-node link api for creating kafka topics. 我正在使用kafka-node link api来创建kafka主题。 I did not find how to create a kafka topic with partitions. 我没有找到如何使用分区创建kafka主题。

var kafka = require('kafka-node'),
    Producer = kafka.Producer,
    client = new kafka.Client(),
    producer = new Producer(client);
// Create topics sync
producer.createTopics(['t','t1'], false, function (err, data) {
    console.log(data);
});
// Create topics async
producer.createTopics(['t'], true, function (err, data) {});

producer.createTopics(['t'], function (err, data) {});// Simply omit 2nd arg

how to create kafka topic with partitions in nodejs. 如何使用nodejs中的分区创建kafka主题。

From your node.js app execute the shell script $KAFKA_HOME/bin/kafka-topics.sh —create —topic topicname —partitions 8 —replication-factor 1 —zookeeper localhost:2181 从您的node.js应用程序执行shell脚本$ KAFKA_HOME / bin / kafka-topics.sh -create -t​​opic topicname -partitions 8 -replication-factor 1 -zookeeper localhost:2181

Where $KAFKA_HOME is the location where you installed Kafka 其中$ KAFKA_HOME是您安装Kafka的位置

As documentation describes, this method works only when auto.create.topics.enable is set to true : 如文档所述,此方法仅在auto.create.topics.enable设置为true

This method is used to create topics on the Kafka server. 此方法用于在Kafka服务器上创建主题。 It only works when auto.create.topics.enable, on the Kafka server, is set to true. 它仅在Kafka服务器上的auto.create.topics.enable设置为true时有效。 Our client simply sends a metadata request to the server which will auto create topics. 我们的客户只需向服务器发送元数据请求即可自动创建主题。 When async is set to false, this method does not return until all topics are created, otherwise it returns immediately. 当async设置为false时,此方法在创建所有主题之前不会返回,否则会立即返回。

This means that any operation on unknown topic will lead to its creation with default number of partitions configured by num.partitions parameter. 这意味着对未知主题的任何操作都将导致使用num.partitions参数配置的默认分区数创建它。

I'm not sure, but maybe one of the node-rdkafka implementations could allow you to call corresponding librdkafka method to create topic? 我不确定,但也许其中一个node-rdkafka实现可以允许你调用相应的librdkafka方法来创建主题?

I am not that sure but I think as per your requirement the code has updated here:- https://github.com/SOHU-Co/kafka-node#createtopicstopics-cb , adding a parameter "replicaAssignment". 我不确定,但我认为根据您的要求,代码已在此更新: - https://github.com/SOHU-Co/kafka-node#createtopicstopics-cb ,添加参数“replicaAssignment”。

  // Optional explicit partition / replica assignment
  // When this property exists, partitions and replicationFactor properties are ignored
  replicaAssignment: [
    {
      partition: 0,
      replicas: [3, 4]
    },
    {
      partition: 1,
      replicas: [2, 1]
    }
  ]

The Producer.createTopics takes a partitons option. Producer.createTopics采用partitons选项。 See https://www.npmjs.com/package/kafka-node#createtopicstopics-cb 请参阅https://www.npmjs.com/package/kafka-node#createtopicstopics-cb

Pass an object, rather than a string 传递一个对象,而不是一个字符串

producer.createTopics(['t', 't1'], true, function (err, data) {});

becomes

producer.createTopics(
  [
    { topic: 't', paritions: 5 },
    { topic: 't1', partitions: 23 },
  ], 
  true,
  function (err, data) {}
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM