com.azure.data.schemaregistry.apacheavro
Microsoft Azure client library for Schema Registry Apache Avro Serializer provides support for serializing and deserializing Apache Avro objects with schemas stored in Azure Schema Registry.
Key Concepts
- Schema: Text describing the how to deserialize and serialize an object.
- Schema Registry: Centralized location for event producers and consumers to fetch schemas used to serialize and deserialized structured data.
- Apache Avro: Serialization format for data.
Getting Started
The starting point for creating clients is via builders. The examples shown in this document use a credential object named DefaultAzureCredential for authentication, which is appropriate for most scenarios, including local development and production environments. Additionally, we recommend using managed identity for authentication in production environments. You can find more information on different ways of authenticating and their corresponding credential types in the Azure Identity documentation".
Sample: Construct the serializer
The following code demonstrates the creation of SchemaRegistryApacheAvroSerializer. The credential used is DefaultAzureCredential because it combines commonly used credentials in deployment and development and chooses the credential to used based on its running environment.
TokenCredential tokenCredential = new DefaultAzureCredentialBuilder().build();
SchemaRegistryAsyncClient schemaRegistryAsyncClient = new SchemaRegistryClientBuilder()
.credential(tokenCredential)
.fullyQualifiedNamespace("{schema-registry-endpoint}")
.buildAsyncClient();
SchemaRegistryApacheAvroSerializer serializer = new SchemaRegistryApacheAvroSerializerBuilder()
.schemaRegistryClient(schemaRegistryAsyncClient)
.schemaGroup("{schema-group}")
.buildSerializer();
Sample: Serialize an object
The serializer can serialize objects into any class extending from MessageContent. EventData extends from MessageContent, so the object can be serialized seamlessly.
The serializer assumes there is a no argument constructor used to instantiate the MessageContent type. If there is a different way to instantiate the concrete type, use the overload which takes a message factory function, com.azure.data.schemaregistry.apacheavro.SchemaRegistryApacheAvroSerializer#serialize(java.lang.Object, com.azure.core.util.serializer.TypeReference, java.util.function.Function).
// The object to encode. The Avro schema is:
// {
// "namespace": "com.azure.data.schemaregistry.apacheavro.generatedtestsources",
// "type": "record",
// "name": "Person",
// "fields": [
// {"name":"name", "type": "string"},
// {"name":"favourite_number", "type": ["int", "null"]},
// {"name":"favourite_colour", "type": ["string", "null"]}
// ]
// }
Person person = Person.newBuilder()
.setName("Chase")
.setFavouriteColour("Turquoise")
.setFavouriteNumber(3)
.build();
EventData eventData = serializer.serialize(person, TypeReference.createInstance(EventData.class));
Sample: Deserialize an object
The serializer can deserialize messages that were created using any of the serialize methods. In the sample, EventData is created by serializing the Avro-generated object, Person.
// EventData created from the Avro generated object, person.
EventData eventData = serializer.serialize(person, TypeReference.createInstance(EventData.class));
Person deserialized = serializer.deserialize(eventData, TypeReference.createInstance(Person.class));
System.out.printf("Name: %s, Number: %s%n", deserialized.getName(), deserialized.getFavouriteNumber());
Classes
| SchemaRegistryApacheAvroException |
Represents an exception that is thrown when Avro serialization or deserialization fails. |
| SchemaRegistryApacheAvroSerializer |
Schema Registry-based serializer implementation for Avro data format using Apache Avro. |
| SchemaRegistryApacheAvroSerializerBuilder |
The builder for instantiating a SchemaRegistryApacheAvroSerializer. |