Schema Registry 中的向后兼容性问题和不确定性 [英] Backward Comaptibility issue and uncertainity in Schema Registry
问题描述
我有一个用例,我有一个 JSON,我想从 JSON 中生成模式和记录并发布记录.我已经配置了值序列化器并且架构设置向后兼容.
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 架构.
Version 1 schema registered.
在 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 生成的模式是如何注册的,以及第三个被拒绝了?虽然我没有任何默认键删除字段?是不是 Schema Registry 总是接受第一个进化?(第 1 个架构之上的第 2 个架构)
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 架构
{字段":[
{
"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:
{字段":[
{
"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" }
推荐答案
让我们回顾一下向后兼容性规则... https://docs.confluent.io/current/schema-registry/avro.html#compatibility-types
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.
这篇关于Schema Registry 中的向后兼容性问题和不确定性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!