繁体   English   中英

将 avro 消息推送到 kafka 主题

[英]push avro message to kafka topic

我正在尝试使用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)
});

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

const kafkaSettings  = {
    "kafka" : {
      "kafkaHost" : config.KafkaHost
    },
    "schema": {
      "registry" : config.KafkaRegistry,
      "topics": [
        {"name": "classifier-response-test" }
    ]
    }
  };

该库尚未准备好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

暂无
暂无

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

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