schema registry-注册自定义对象类型

6pp0gazn  于 2021-06-04  发布在  Kafka
关注(0)|答案(1)|浏览(900)

我想注册一个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模式,该模式引用“client”属性中的基模式,操作将失败,并出现错误422

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

这个问题似乎与指定自定义类型字段有关。
知道如何在schema registry中注册相关模式时添加自定义类型吗?

7dl7o3gd

7dl7o3gd1#

您可以使用模式引用让一个模式引用合流模式注册表上的其他模式。
请注意,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
    }
  ]
}

其中clientsubject是您在注册 com.test.client.Client 架构。
基本上可以看到,使用这种方法,可以直接指定要使用的引用模式的版本。这是非常有用的,因为否则可能会发生不一致。
有关架构引用的更多信息,请查看以下链接中的文档:https://docs.confluent.io/platform/current/schema-registry/serdes-develop/serdes-avro.html#referenced-架构avro

相关问题