首页 > 解决方案 > Kafka Connect JSON 模式似乎不支持“$ref”标签

问题描述

我正在使用带有 JSONSchema 的 Kafka Connect,并且需要在 Kafka Connect 插件中手动转换 JSON 模式(转换为“模式”)。我可以成功地从模式注册表中检索 JSON 模式,并成功地使用简单的 JSON 模式进行转换,但是对于那些复杂且具有引用单个 JSON 模式定义中组件的有效“$ref”标签的模式,我遇到了困难。

我有几个问题:

  1. JsonConverter.java 似乎无法处理“$ref”。我是正确的,还是它在其他地方以其他方式处理它?
  2. Schema Registry 是否处理子定义的引用?如果是,是否有代码显示如何处理取消引用?
  3. JSON Schema 是否应该在提交给 Schema Registry 之前解析为没有引用的字符串(即内联引用),从而消除“$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?\"}}}}}

标签: apache-kafkaapache-kafka-connectjsonschemaconfluent-platform

解决方案


同样,JsonConverterApache Kafka 源代码中没有 JSONSchema 的概念,因此,不,$ref它不起作用,它也没有与注册表集成。

你似乎在寻找io.confluent.connect.json.JsonSchemaConverter类+逻辑


推荐阅读