简体   繁体   English

使用 Kafka 模式注册表 API 注册新的 avro 模式

[英]Register a new avro schema using Kafka schema-registry API

I am trying to create a new schema using the kafka-schema-registery api.我正在尝试使用kafka-schema-registery api 创建一个新schema below is the implementation:下面是实现:

let value = JSON.stringify(avroSchema);

let type= {"schema" : value}; 

 fetch(`${process.env.SCHEMA_REGISTRY_URL}/subjects/${topic}/versions`,
        { 
          body : type,  
          method : 'POST', 
          headers :{ 'Content-Type': 'application/vnd.schemaregistry.v1+json,
                                      application/vnd.schemaregistry+json, application/json',
                     'Accept' : 'application/vnd.schemaregistry.v1+json,
                                 application/vnd.schemaregistry+json, application/json'            
        }
        })
    .then(res=>res.json())
    .then((result)=>{
        console.log('result is ', result);   
         resolve(result);    
     })
    .catch((err)=>{
        console.log('err',err);
        reject(err);
    })

Here is how avroSchema looks:这是avroSchema外观:

const avroSchema = {
      "type": "record",
      "name": "test",
      "fields" : [
            {"name": "field", "type": "long"},
    ]
  };

When I am executing this code I am getting 500 - Internal server error .当我执行此代码时,我收到500 - Internal server error

Can anyone help me to understand where I am going wrong?谁能帮助我了解我哪里出错了?

The error exists on the server, and your code looks okay, but I suggest using NPM packages rather than re-invent the wheel - https://www.npmjs.com/package/avro-schema-registry服务器上存在错误,您的代码看起来不错,但我建议使用 NPM 包而不是重新发明轮子 - https://www.npmjs.com/package/avro-schema-registry

For future users:对于未来的用户:

Here is how I was able to solve this:这是我能够解决这个问题的方法:

 const payload = {
                   "schema": JSON.stringify(avroSchema)
                 }; 

Finally after setting the payload, make a POST request options as below:最后在设置有效载荷后,发出一个POST请求options ,如下所示:

const options = {
  method: 'POST',
  url: `${process.env.SCHEMA_REGISTRY_URL}/subjects/${topicName}-value/versions`,
  headers: { 'Content-Type': 'application/vnd.schemaregistry.v1+json' },
  body: payload,
  json: true
}

then make the request:然后提出请求:

request(options, function (error, response, body) {
    if (error) {
        reject(error);
    }                   
    resolve(body)
});

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

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