Suppose the following schema has grown too big, and you want to split it into smaller, reusable pieces. It may even have hit a size limit in Schema Registry.
{
"type": "record",
"name": "Message",
"namespace": "dev.adebuisson",
"fields": [
{
"name": "content",
"type": "string"
},
{
"name": "address",
"type": {
"type": "record",
"name": "Address",
"namespace": "dev.adebuisson.shared",
"fields": [
{
"name": "street",
"type": "string"
},
{
"name": "city",
"type": "string"
}
]
}
}
]
}
I will first extract Address into its own schema (address.avsc):
{
"type": "record",
"name": "Address",
"namespace": "dev.adebuisson.shared",
"fields": [
{
"name": "street",
"type": "string"
},
{
"name": "city",
"type": "string"
}
]
}
Then reference it from Message using its fully qualified name (namespace + name):
{
"type": "record",
"name": "Message",
"namespace": "dev.adebuisson",
"fields": [
{
"name": "content",
"type": "string"
},
{
"name": "address",
"type": "dev.adebuisson.shared.Address"
}
]
}
Deployment to Schema Registry during CI/CD#
The configuration below deploys both schemas in the correct order with a reference from avro-demo-value to avro-demo-address:
<plugin>
<groupId>io.confluent</groupId>
<artifactId>kafka-schema-registry-maven-plugin</artifactId>
<version>7.9.9</version>
<configuration>
<schemaRegistryUrls>
<param>http://localhost:62281</param>
</schemaRegistryUrls>
<subjects>
<avro-demo-address>src/main/resources/avro/address.avsc</avro-demo-address>
<avro-demo-value>src/main/resources/avro/message.avsc</avro-demo-value>
</subjects>
<schemaTypes>
<avro-demo-address>AVRO</avro-demo-address>
<avro-demo-value>AVRO</avro-demo-value>
</schemaTypes>
<references>
<avro-demo-value>
<reference>
<name>dev.adebuisson.shared.Address</name>
<subject>avro-demo-address</subject>
</reference>
</avro-demo-value>
</references>
</configuration>
</plugin>
Each schema is registered under a “subject”:
$ mvn schema-registry:register
[INFO] Registered subject(avro-demo-address) with id 52 version 1
[INFO] Registered subject(avro-demo-value) with id 53 version 1
Generating Avro-backed classes#
If a type such as dev.adebuisson.shared.Address isn’t already known, the Avro Maven plugin can look into the files listed under <imports/> to resolve it.
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>1.11.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/avro</sourceDirectory>
<imports>
<import>${project.basedir}/src/main/resources/avro/address.avsc</import>
</imports>
<enableDecimalLogicalType>true</enableDecimalLogicalType>
</configuration>
</execution>
</executions>
</plugin>
mvn clean generate-sources generates the Avro-backed Java classes used for building and serializing your messages.
Message message = Message.newBuilder()
.setContent("Hello")
.setAddress(Address.newBuilder()
.setCity("Paris")
.setStreet("Rue St Jacques")
.build())
.build();
If you inspect the generated Message class closely, you’ll notice that the Address schema is fully inlined into the SCHEMA$ private field.
This is the actual schema that will be verified against Schema Registry by your application at runtime.
Checking compatibility#
Before publishing an Avro message to, for example, a Kafka topic, applications typically check compatibility with Schema Registry using SCHEMA$ as the request
body:
$ curl -X POST http://localhost:62281/compatibility/subjects/avro-demo-value/versions \
-H "Content-Type: application/json" \
-d '{
"schema": "{\"type\":\"record\",\"name\":\"Message\",\"namespace\":\"dev.adebuisson\",\"fields\":[{\"name\":\"content\",\"type\":\"string\"},{\"name\":\"address\",\"type\":{\"type\":\"record\",\"name\":\"Address\",\"namespace\":\"dev.adebuisson.shared\",\"fields\":[{\"name\":\"street\",\"type\":\"string\"},{\"name\":\"city\",\"type\":\"string\"}]}}]}"
}'
{"is_compatible":true}
Schema Registry successfully resolves the references within avro-demo-value and returns {"is_compatible":true}.
Alternatively, Avro-producing applications can retrieve the schema ID and bundle it alongside the Avro payload, allowing the consumer to fetch the writer’s schema it needs to deserialize the message.
$ curl --request POST \
--url http://localhost:62281/subjects/avro-demo-value \
--header 'Content-Type: application/json' \
--data '{
"schema": "{\"type\":\"record\",\"name\":\"Message\",\"namespace\":\"dev.adebuisson\",\"fields\":[{\"name\":\"content\",\"type\":\"string\"},{\"name\":\"address\",\"type\":{\"type\":\"record\",\"name\":\"Address\",\"namespace\":\"dev.adebuisson.shared\",\"fields\":[{\"name\":\"street\",\"type\":\"string\"},{\"name\":\"city\",\"type\":\"string\"}]}}]}"
}'
As before, Schema Registry resolves the references and returns the schema ID:
{
"subject": "avro-demo-value",
"version": 1,
"id": 57,
"guid": "ed750bde-60a8-6409-2673-82adb5fda2fd",
"schemaType": "AVRO",
"references": [
{
"name": "dev.adebuisson.shared.Address",
"subject": "avro-demo-address",
"version": 1
}
],
"schema": "{\"type\":\"record\",\"name\":\"Message\",\"namespace\":\"dev.adebuisson\",\"fields\":[{\"name\":\"content\",\"type\":\"string\"},{\"name\":\"address\",\"type\":\"dev.adebuisson.shared.Address\"}]}",
"ts": 1810554477013,
"deleted": false
}