首页 > 解决方案 > Schema Registry - 注册自定义对象类型

问题描述

我想注册一个 Avro 模式,它在模式注册表上引用另一个 Avro 模式。

首先,我注册了以下基本 Avro 架构:

{
  "type":"record",
  "name":"Client",
  "namespace":"com.test.client",
  "doc": "Client Property specific information is specified in this object",
  "fields" : [
      {"name" : "name", "type" : [ "null", "string" ]},
      {"name" : "address", "type" :  [ "null", "string"]}
  ]
}

如果我尝试注册以下 Avro 架构,该架构引用“客户端”属性中的基本架构,则操作失败并出现错误 422


{
  "type":"record",
  "name":"AnotherObject",
  "namespace":"com.test.client",
  "fields":
  [
    {"name":"tenant", "type": [ "null", "long" ]},
    {"name":"client", "type" : [ "null","com.test.client.Client"]}
  ]
}

似乎问题与指定自定义类型字段有关。

知道如何在模式注册表中注册相关模式时添加自定义类型吗?

标签: apache-kafkaavroconfluent-schema-registry

解决方案


您可以使用模式引用让模式引用 Confluent Schema Registry 上的其他模式。

请注意,在 Confluent Platform 5.5中引入了对这种表示法的支持。

根据您的示例(根据https://docs.confluent.io/platform/current/schema-registry/develop/api.html#subjects提供的文档),您应该AnotherObject在 POST 请求中使用以下有效负载注册新模式注册:

{
  "schema":"{
     \"type\":\"record\",
     \"name\":\"AnotherObject\",
     \"namespace\":\"com.test.client\",
     \"fields\":[
        {
           \"name\":\"tenant\",
           \"type\":[\"null\",\"long\"]
        },
        {
           \"name\":\"client\",
           \"type\":[\"null\",\"com.test.client.Client\"]
        }
     ]
   }",
  "schemaType":"AVRO",
  "references":[
    {
      "name":"com.test.client.Client",
      "subject":"clientSubject",
      "version":1
    }
  ]
}

其中clientSubjectcom.test.client.Client是您在注册架构时指定的主题名称。

正如您所看到的,使用这种方法基本上可以直接指定要使用的参考模式的版本。这非常有用,因为否则可能会出现不一致。

在以下链接中查看有关架构引用的更多信息的文档:https ://docs.confluent.io/platform/current/schema-registry/serdes-develop/serdes-avro.html#referenced-schemas-avro


推荐阅读