Class SchemaRegistryApacheAvroSerializer

java.lang.Object
com.azure.data.schemaregistry.apacheavro.SchemaRegistryApacheAvroSerializer

public final class SchemaRegistryApacheAvroSerializer extends Object
Schema Registry-based serializer implementation for Avro data format using Apache Avro.

Creating a SchemaRegistryApacheAvroSerializer

 TokenCredential tokenCredential = new DefaultAzureCredentialBuilder().build();
 SchemaRegistryAsyncClient schemaRegistryAsyncClient = new SchemaRegistryClientBuilder()
     .credential(tokenCredential)
     .fullyQualifiedNamespace("{schema-registry-endpoint}")
     .buildAsyncClient();

 // By setting autoRegisterSchema to true, if the schema does not exist in the Schema Registry instance, it is
 // added to the instance. By default, this is false, so it will error if the schema is not found.
 SchemaRegistryApacheAvroSerializer serializer = new SchemaRegistryApacheAvroSerializerBuilder()
     .schemaRegistryClient(schemaRegistryAsyncClient)
     .autoRegisterSchemas(true)
     .schemaGroup("{schema-group}")
     .buildSerializer();
 

Serialize an object

Serializes an Avro generated object into MessageContent. serialize(Object, TypeReference) assumes that 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, serialize(Object, TypeReference, 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("Alina")
     .setFavouriteColour("Turquoise")
     .build();

 MessageContent message = serializer.serialize(person,
     TypeReference.createInstance(MessageContent.class));
 

Deserialize an object

 // Message to deserialize. Assume that the body contains data which has been serialized using an Avro encoder.
 MessageContent message = new MessageContent()
     .setBodyAsBinaryData(BinaryData.fromBytes(new byte[0]))
     .setContentType("avro/binary+{schema-id}");

 // This is an object generated from the Avro schema used in the serialization sample.
 Person person = serializer.deserialize(message, TypeReference.createInstance(Person.class));
 

Serialize an object using a message factory

Serializes an Avro generated object into MessageContent. It uses the messageFactory to instantiate and populate the type.
 // 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("Alina")
     .setFavouriteColour("Turquoise")
     .build();

 // Serializes and creates an instance of ComplexMessage using the messageFactory function.
 ComplexMessage message = serializer.serialize(person,
     TypeReference.createInstance(ComplexMessage.class),
     (encodedData) -> {
         return new ComplexMessage("unique-id", OffsetDateTime.now());
     });