0

我正在尝试使用kafka-node-avro

库将数据推送到现有主题。我已经使用 curl 和主题将模式添加到 SchemaRegistry 中。我收到以下错误:

Error: Topic name or id is needed to build Schema
    at new Shema (/work/node_modules/kafka-node-avro/lib/schema.js:7:41)
    at Object.Pool.getByName (/work/node_modules/kafka-node-avro/lib/schemaPool.js:70:10)
    at new Promise (<anonymous>)

我的代码片段如下:

const Settings = {
    "kafka" : {
        "kafkaHost" : config.KafkaHost
    },
    "schema": {
        "registry" : config.KafkaRegistry
    }
};

console.log("settings registry: ", config.KafkaRegistry);
console.log("settings kafkaHost: ", config.KafkaHost)
KafkaAvro.init(Settings).then( kafka => {
const producer = kafka.addProducer();
let payloads = [
    {
        topic: 'classifier-response-test',
        messages: JSON.stringify(kafkaData)
    }
];
producer.send(payloads).then( success => {
// Message was sent encoded with Avro Schema
    console.log("message sent ! Awesome ! :) ")
}, error => {
        // Something wrong happen
        console.log("There seems that there is a mistake ! try Again ;) ")
        console.log(error)
    });
} , error => {
    // something wrong happen
    console.log("There seems that there is a global mistake ! try Again ;) ")
    console.log(error)
});
4

2 回答 2

0

该库尚未准备好send消息列表,这就是为什么它在尝试在发送机制中动态地将 Schema 添加到 Schema Pool 时抱怨的原因。最简单的解决方案是一次发送 1 条消息,在您的代码示例中可能类似于

const Settings = {
  "kafka" : {
    "kafkaHost" : config.KafkaHost
  },
  "schema": {
    "registry" : config.KafkaRegistry
  }
};

KafkaAvro.init(Settings).then( kafka => {
  kafka.send({
    topic: 'classifier-response-test',
    messages: JSON.stringify(kafkaData)
  }).then( success => {
    // Message was sent encoded with Avro Schema
    console.log("message sent ! Awesome ! :) ")
  }, error => {
    // Something wrong happen
    console.log("There seems that there is a mistake ! try Again ;) ")
    console.log(error)
  });
} , error => {
  // something wrong happen
  console.log("There seems that there is a global mistake ! try Again ;) ")
  console.log(error)
});

感谢您使用kafka-node-avro

于 2020-10-01T22:27:05.893 回答
-1

问题是我们需要将主题列表放在模式设置中,以建立主题和模式注册表之间的链接。我们可以输入模式 id 或主题名称。

const kafkaSettings  = {
    "kafka" : {
      "kafkaHost" : config.KafkaHost
    },
    "schema": {
      "registry" : config.KafkaRegistry,
      "topics": [
        {"name": "classifier-response-test" }
    ]
    }
  };
于 2020-08-25T14:22:11.267 回答