架构注册表中的向后兼容性问题和不确定性 [英] Backward Comaptibility issue and uncertainity in Schema Registry

查看:114
本文介绍了架构注册表中的向后兼容性问题和不确定性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用例,其中有一个JSON,我想生成模式并从JSON中记录并发布记录. 我已经配置了值序列化程序,并且Schema设置是向后兼容的.

I have a use case where I have a JSON and I want to generate schema and record out of the JSON and publish a record. I have configured the value serializer and Schema setting is Backward compatible.

第一个JSON

字符串json ="{\ n" +

String json = "{\n" +

     "    \"id\": 1,\n" +

     "    \"name\": \"Headphones\",\n" +

     "    \"price\": 1250.0,\n" +

     "    \"tags\": [\"home\", \"green\"]\n" +

     "}\n"
     ;

已注册版本1模式.

在Avro控制台使用者中收到的消息.

Received message in avro console consumer.

第二个JSON.

字符串json ="{\ n" +

String json = "{\n" +

    "    \"id\": 1,\n" +

    "    \"price\": 1250.0,\n" +
    "    \"tags\": [\"home\", \"green\"]\n" +
    "}\n"
    ;

成功注册架构. 已发送邮件.

Registered schema Successfully. Sent message.

现在尝试发送成功发送的JSON 1

Now tried sending the JSON 1 sent successfully

模式3:

字符串json ="{\ n" +

String json = "{\n" +

    "    \"id\": 1,\n" +
    "    \"name\": \"Headphones\",\n" +

    "    \"tags\": [\"home\", \"green\"]\n" +
    "}\n"
    ;

在这种情况下出现错误. 由以下原因引起:io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException:正在注册的架构与早期的架构不兼容.错误代码:409

Got error for this case. Caused by: io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException: Schema being registered is incompatible with an earlier schema; error code: 409

如何注册从第二个JSON生成的架构,并且 第三个被拒绝了?虽然我没有任何默认键 删除字段?是架构注册表始终接受第一个 进化? (第2个模式超过第1个)

How is that schema generated from 2nd JSON was registered and the third one was rejected? Although I didn't have any Default key for the deleted field? Is it that the Schema Registry always accepts the 1st evolution? (2nd schema over 1st)

架构注册表中的架构

版本1模式

{ 字段":[

{ "fields": [

{

  "doc": "Type inferred from '1'",

  "name": "id",

  "type": "int"

},

{

  "doc": "Type inferred from '\"Headphones\"'",

  "name": "name",

  "type": "string"

},

{

  "doc": "Type inferred from '1250.0'",

  "name": "price",

  "type": "double"

},

{

  "doc": "Type inferred from '[\"home\",\"green\"]'",

  "name": "tags",

  "type": {

    "items": "string",

    "type": "array"

  }

}
],
"name": "myschema",
"type": "record"   }

版本2:

{ 字段":[

{ "fields": [

{

  "doc": "Type inferred from '1'",

  "name": "id",

  "type": "int"

},

{

  "doc": "Type inferred from '1250.0'",

  "name": "price",

  "type": "double"

},

{

  "doc": "Type inferred from '[\"home\",\"green\"]'",

  "name": "tags",

  "type": {

    "items": "string",

    "type": "array"

  }

}
],
"name": "myschema",
"type": "record"   }

推荐答案

让我们回顾一下向后兼容规则...

Let's go over the backwards compatibility rules... https://docs.confluent.io/current/schema-registry/avro.html#compatibility-types

首先,默认设置不是可传递的,因此版本3仅查看版本2.

First, the default isn't transitive, so version 3 only will look at version 2.

向后规则指出您可以删除字段或添加可选字段(默认情况下).我假设您的模式生成器工具不知道如何使用可选选项,因此只允许您删除而不允许添加.

The backwards rule states you can delete fields or add optional fields (those with a default). I assume your schema generator tool doesn't know how to use optionals, so you're only allowed to delete, not add.

在版本1和版本2之间,您已经删除了有效的名称字段.

Between version 1 and 2, you've deleted the name field, which is valid.

在版本2和传入版本3之间,它认为您正在尝试发布一个新的架构,该架构会取消价格(可以这样做),但是会添加一个必填名称字段,这是不允许的.

Between version 2 and the incoming 3, it thinks you're trying to post a new schema which removes price (this is okay}, but adds a required name field, which is not allowed.

这篇关于架构注册表中的向后兼容性问题和不确定性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆