【发布时间】:2021-12-05 17:49:41
【问题描述】:
我正在使用带有 JSONSchema 的 Kafka Connect,并且我需要在 Kafka Connect 插件中手动转换 JSON 模式(转换为“模式”)。我可以成功地从架构注册表中检索 JSON 架构,并且可以成功地使用简单的 JSON 架构进行转换,但是对于那些复杂且在单个 JSON 架构定义中引用组件的有效“$ref”标签存在困难。
我有几个问题:
- JsonConverter.java 似乎无法处理“$ref”。我是正确的,还是它在其他地方以其他方式处理它?
- 模式注册表是否处理子定义的引用?如果是,是否有代码显示如何处理取消引用?
- 在提交到 Schema Registry 之前,是否应该将 JSON Schema 解析为没有引用的字符串(即内联引用),从而消除“$ref”问题?
我在看下面的Kafka源码模块JsonConverter.java:
https://github.com/apache/kafka/blob/trunk/connect/json/src/main/java/org/apache/kafka/connect/json/JsonConverter.java#L428
下面显示了一个复杂模式的示例(取自 JSON Schema 站点)(注意 "$ref": "#/$defs/veggie" 标记引用后面的子定义)
{
"$id": "https://example.com/arrays.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "A representation of a person, company, organization, or place",
"title": "complex-schema",
"type": "object",
"properties": {
"fruits": {
"type": "array",
"items": {
"type": "string"
}
},
"vegetables": {
"type": "array",
"items": { "$ref": "#/$defs/veggie" }
}
},
"$defs": {
"veggie": {
"type": "object",
"required": [ "veggieName", "veggieLike" ],
"properties": {
"veggieName": {
"type": "string",
"description": "The name of the vegetable."
},
"veggieLike": {
"type": "boolean",
"description": "Do I like this vegetable?"
}
}
}
}
}
以下是模式注册成功后从模式注册表返回的实际模式:
[
{
"subject": "complex-schema",
"version": 1,
"id": 1,
"schemaType": "JSON",
"schema": "{\"$id\":\"https://example.com/arrays.schema.json\",\"$schema\":\"https://json-schema.org/draft/2020-12/schema\",\"description\":\"A representation of a person, company, organization, or place\",\"title\":\"complex-schema\",\"type\":\"object\",\"properties\":{\"fruits\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}},\"vegetables\":{\"type\":\"array\",\"items\":{\"$ref\":\"#/$defs/veggie\"}}},\"$defs\":{\"veggie\":{\"type\":\"object\",\"required\":[\"veggieName\",\"veggieLike\"],\"properties\":{\"veggieName\":{\"type\":\"string\",\"description\":\"The name of the vegetable.\"},\"veggieLike\":{\"type\":\"boolean\",\"description\":\"Do I like this vegetable?\"}}}}}"
}
]
实际的模式嵌入在上面返回的字符串中(“模式”字段的内容)并包含 $ref 引用:
{\"$id\":\"https://example.com/arrays.schema.json\",\"$schema\":\"https://json-schema.org/draft/2020-12/schema\",\"description\":\"A representation of a person, company, organization, or place\",\"title\":\"complex-schema\",\"type\":\"object\",\"properties\":{\"fruits\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}},\"vegetables\":{\"type\":\"array\",\"items\":{\"$ref\":\"#/$defs/veggie\"}}},\"$defs\":{\"veggie\":{\"type\":\"object\",\"required\":[\"veggieName\",\"veggieLike\"],\"properties\":{\"veggieName\":{\"type\":\"string\",\"description\":\"The name of the vegetable.\"},\"veggieLike\":{\"type\":\"boolean\",\"description\":\"Do I like this vegetable?\"}}}}}
【问题讨论】:
-
根据您提交的架构,您为什么希望响应不包括
$ref? -
是的,“$ref”是预期的,但问题实际上是关于如何/在哪里处理和解决“$ref”标签。
-
我不希望您对它们进行预处理。它们是 JSON Schema 标准的一部分,因此 JSON Schema 实现应该理解它们。
$ref不像某种模板命令。解决方案应由在验证数据时使用架构的实现来完成。
标签: apache-kafka apache-kafka-connect jsonschema confluent-platform