0
votes

I want to register an Avro schema which references another Avro schema on the schema registry.

First I've registered the following base Avro schema:

{
  "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"]}
  ]
}

Them if I try to register the following Avro schema which refers to the base one within 'client' attribute the operation fails with error 422


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

It seems issue is related to specifying custom type fields.

Any idea how to add custom type while registering related schemas in schema registry?

1
Ideally, you'd nest the complete object, but I know references were recently added to the APIOneCricketeer

1 Answers

1
votes

You can use schema references to let a schema to refer to other schemas on the Confluent Schema Registry.

Note that support for this notation has been introduced with Confluent Platform 5.5.

Basing on your example ( according to the documentation available at https://docs.confluent.io/platform/current/schema-registry/develop/api.html#subjects) you should register the AnotherObject using the following payload on the POST request for new schemas registration:

{
  "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
    }
  ]
}

Where clientSubject is the subject name you specified on registering the com.test.client.Client schema.

As you can see basically with this approach you can specify directly the version of the reference schema to use. This is very useful because otherwise inconsistency could occurs.

Check the documentation for further informations about schema references at the following link: https://docs.confluent.io/platform/current/schema-registry/serdes-develop/serdes-avro.html#referenced-schemas-avro