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());
     });
 
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    deserialize(com.azure.core.models.MessageContent message, com.azure.core.util.serializer.TypeReference<T> typeReference)
    Deserializes a message into its object.
    <T> Mono<T>
    deserializeAsync(com.azure.core.models.MessageContent message, com.azure.core.util.serializer.TypeReference<T> typeReference)
    Deserializes a message into its object.
    <T extends com.azure.core.models.MessageContent>
    T
    serialize(Object object, com.azure.core.util.serializer.TypeReference<T> typeReference)
    Serializes an object into a message.
    <T extends com.azure.core.models.MessageContent>
    T
    serialize(Object object, com.azure.core.util.serializer.TypeReference<T> typeReference, Function<com.azure.core.util.BinaryData,T> messageFactory)
    Serializes an object into a message.
    <T extends com.azure.core.models.MessageContent>
    Mono<T>
    serializeAsync(Object object, com.azure.core.util.serializer.TypeReference<T> typeReference)
    Serializes an object into a message.
    <T extends com.azure.core.models.MessageContent>
    Mono<T>
    serializeAsync(Object object, com.azure.core.util.serializer.TypeReference<T> typeReference, Function<com.azure.core.util.BinaryData,T> messageFactory)
    Serializes an object into a message.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • serialize

      public <T extends com.azure.core.models.MessageContent> T serialize(Object object, com.azure.core.util.serializer.TypeReference<T> typeReference)
      Serializes an object into a message.
      Type Parameters:
      T - Concrete type of MessageContent.
      Parameters:
      object - Object to serialize.
      typeReference - Type of message to create.
      Returns:
      The message encoded or null if the message could not be serialized.
      Throws:
      IllegalArgumentException - if T does not have a no argument constructor. Or if the schema could not be fetched from T.
      RuntimeException - if an instance of T could not be instantiated.
      SchemaRegistryApacheAvroException - if an instance of T could not be instantiated or there was a problem serializing the object.
      NullPointerException - if the object is null or typeReference is null.
      com.azure.core.exception.ResourceNotFoundException - if the schema could not be found and SchemaRegistryApacheAvroSerializerBuilder.autoRegisterSchemas(boolean) is false.
      com.azure.core.exception.HttpResponseException - if an error occurred while trying to fetch the schema from the service.
    • serialize

      public <T extends com.azure.core.models.MessageContent> T serialize(Object object, com.azure.core.util.serializer.TypeReference<T> typeReference, Function<com.azure.core.util.BinaryData,T> messageFactory)
      Serializes an object into a message.
      Type Parameters:
      T - Concrete type of MessageContent.
      Parameters:
      object - Object to serialize.
      typeReference - Type of message to create.
      messageFactory - Factory to create an instance given the serialized Avro.
      Returns:
      The message encoded or null if the message could not be serialized.
      Throws:
      IllegalArgumentException - if messageFactory is null and type T does not have a no argument constructor. Or if the schema could not be fetched from T.
      RuntimeException - if an instance of T could not be instantiated.
      NullPointerException - if the object is null or typeReference is null.
      SchemaRegistryApacheAvroException - if the object could not be serialized.
      com.azure.core.exception.ResourceNotFoundException - if the schema could not be found and SchemaRegistryApacheAvroSerializerBuilder.autoRegisterSchemas(boolean) is false.
      com.azure.core.exception.HttpResponseException - if an error occurred while trying to fetch the schema from the service.
    • serializeAsync

      public <T extends com.azure.core.models.MessageContent> Mono<T> serializeAsync(Object object, com.azure.core.util.serializer.TypeReference<T> typeReference)
      Serializes an object into a message.
      Type Parameters:
      T - Concrete type of MessageContent.
      Parameters:
      object - Object to serialize.
      typeReference - Type of message to create.
      Returns:
      A Mono that completes with the serialized message.
      Throws:
      IllegalArgumentException - if T does not have a no argument constructor. Or if the schema could not be fetched from T.
      RuntimeException - if an instance of T could not be instantiated.
      NullPointerException - if the object is null or typeReference is null.
      SchemaRegistryApacheAvroException - if the object could not be serialized.
      com.azure.core.exception.ResourceNotFoundException - if the schema could not be found and SchemaRegistryApacheAvroSerializerBuilder.autoRegisterSchemas(boolean) is false.
      com.azure.core.exception.HttpResponseException - if an error occurred while trying to fetch the schema from the service.
    • serializeAsync

      public <T extends com.azure.core.models.MessageContent> Mono<T> serializeAsync(Object object, com.azure.core.util.serializer.TypeReference<T> typeReference, Function<com.azure.core.util.BinaryData,T> messageFactory)
      Serializes an object into a message.
      Type Parameters:
      T - Concrete type of MessageContent.
      Parameters:
      object - Object to serialize.
      typeReference - Type of message to create.
      messageFactory - Factory to create an instance given the serialized Avro. If null is passed in, then the no argument constructor will be used.
      Returns:
      A Mono that completes with the serialized message.
      Throws:
      IllegalArgumentException - if messageFactory is null and type T does not have a no argument constructor. Or if the schema could not be fetched from T.
      IllegalStateException - if schemaGroup is not set.
      RuntimeException - if an instance of T could not be instantiated.
      NullPointerException - if the object is null or typeReference is null.
      SchemaRegistryApacheAvroException - if the object could not be serialized.
      com.azure.core.exception.ResourceNotFoundException - if the schema could not be found and SchemaRegistryApacheAvroSerializerBuilder.autoRegisterSchemas(boolean) is false.
      com.azure.core.exception.HttpResponseException - if an error occurred while trying to fetch the schema from the service.
    • deserialize

      public <T> T deserialize(com.azure.core.models.MessageContent message, com.azure.core.util.serializer.TypeReference<T> typeReference)
      Deserializes a message into its object.
      Type Parameters:
      T - Concrete type of MessageContent.
      Parameters:
      message - Object to deserialize.
      typeReference - Message type to deserialize to.
      Returns:
      The message deserialized.
      Throws:
      NullPointerException - if message or typeReference is null.
      IllegalArgumentException - if the message does not have a content type to use for deserialization. If the mime-type in the content type cannot be parsed or the type is not avro/binary.
      com.azure.core.exception.ResourceNotFoundException - if a schema with a matching schema id could not be found.
      com.azure.core.exception.HttpResponseException - if an issue was encountered while fetching the schema.
      SchemaRegistryApacheAvroException - if the message could not be deserialized.
      com.azure.core.exception.ResourceNotFoundException - if the schema could not be found and SchemaRegistryApacheAvroSerializerBuilder.autoRegisterSchemas(boolean) is false.
      com.azure.core.exception.HttpResponseException - if an error occurred while trying to fetch the schema from the service.
    • deserializeAsync

      public <T> Mono<T> deserializeAsync(com.azure.core.models.MessageContent message, com.azure.core.util.serializer.TypeReference<T> typeReference)
      Deserializes a message into its object.
      Type Parameters:
      T - Concrete type of MessageContent.
      Parameters:
      message - Object to deserialize.
      typeReference - Message to deserialize to.
      Returns:
      A Mono that completes when the message encoded. If message.getBodyAsBinaryData() is null or empty, then an empty Mono is returned.
      Throws:
      NullPointerException - if message or typeReference is null.
      IllegalArgumentException - if the message does not have a content type to use for deserialization. If the mime-type in the content type cannot be parsed or the type is not avro/binary.
      com.azure.core.exception.ResourceNotFoundException - if a schema with a matching schema id could not be found.
      com.azure.core.exception.HttpResponseException - if an issue was encountered while fetching the schema.
      SchemaRegistryApacheAvroException - if the message could not be deserialized.
      com.azure.core.exception.ResourceNotFoundException - if the schema could not be found and SchemaRegistryApacheAvroSerializerBuilder.autoRegisterSchemas(boolean) is false.
      com.azure.core.exception.HttpResponseException - if an error occurred while trying to fetch the schema from the service.