Class 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()
         .schemaRegistryAsyncClient(schemaRegistryAsyncClient)
         .autoRegisterSchema(true)
         .schemaGroup("{schema-group}")
         .buildSerializer();
     

    Serialize an object

    Serializes an Avro generated object into MessageWithMetadata. serializeMessageData(Object, TypeReference) assumes that there is a no argument constructor used to instantiate the MessageWithMetadata type. If there is a different way to instantiate the concrete type, use the overload which takes a message factory function, serializeMessageData(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();
    
     MessageWithMetadata message = serializer.serializeMessageData(person,
         TypeReference.createInstance(MessageWithMetadata.class));
     

    Deserialize an object

     // Message to deserialize. Assume that the body contains data which has been serialized using an Avro encoder.
     MessageWithMetadata message = new MessageWithMetadata()
         .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.deserializeMessageData(message, TypeReference.createInstance(Person.class));
     

    Serialize an object using a message factory

    Serializes an Avro generated object into MessageWithMetadata. 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.serializeMessageData(person,
         TypeReference.createInstance(ComplexMessage.class),
         (encodedData) -> {
             return new ComplexMessage("unique-id", OffsetDateTime.now());
         });
     
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T> T deserializeMessageData​(com.azure.core.experimental.models.MessageWithMetadata message, com.azure.core.util.serializer.TypeReference<T> typeReference)
      Deserializes a message into its object.
      <T> Mono<T> deserializeMessageDataAsync​(com.azure.core.experimental.models.MessageWithMetadata message, com.azure.core.util.serializer.TypeReference<T> typeReference)
      Deserializes a message into its object.
      <T extends com.azure.core.experimental.models.MessageWithMetadata>
      T
      serializeMessageData​(Object object, com.azure.core.util.serializer.TypeReference<T> typeReference)
      Serializes an object into a message.
      <T extends com.azure.core.experimental.models.MessageWithMetadata>
      T
      serializeMessageData​(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.experimental.models.MessageWithMetadata>
      Mono<T>
      serializeMessageDataAsync​(Object object, com.azure.core.util.serializer.TypeReference<T> typeReference)
      Serializes an object into a message.
      <T extends com.azure.core.experimental.models.MessageWithMetadata>
      Mono<T>
      serializeMessageDataAsync​(Object object, com.azure.core.util.serializer.TypeReference<T> typeReference, Function<com.azure.core.util.BinaryData,​T> messageFactory)
      Serializes an object into a message.
    • Method Detail

      • serializeMessageData

        public <T extends com.azure.core.experimental.models.MessageWithMetadata> T serializeMessageData​(Object object,
                                                                                                         com.azure.core.util.serializer.TypeReference<T> typeReference)
        Serializes an object into a message.
        Type Parameters:
        T - Concrete type of MessageWithMetadata.
        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.autoRegisterSchema(boolean) is false.
        com.azure.core.exception.HttpResponseException - if an error occurred while trying to fetch the schema from the service.
      • serializeMessageData

        public <T extends com.azure.core.experimental.models.MessageWithMetadata> T serializeMessageData​(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 MessageWithMetadata.
        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.autoRegisterSchema(boolean) is false.
        com.azure.core.exception.HttpResponseException - if an error occurred while trying to fetch the schema from the service.
      • serializeMessageDataAsync

        public <T extends com.azure.core.experimental.models.MessageWithMetadata> Mono<T> serializeMessageDataAsync​(Object object,
                                                                                                                    com.azure.core.util.serializer.TypeReference<T> typeReference)
        Serializes an object into a message.
        Type Parameters:
        T - Concrete type of MessageWithMetadata.
        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.autoRegisterSchema(boolean) is false.
        com.azure.core.exception.HttpResponseException - if an error occurred while trying to fetch the schema from the service.
      • serializeMessageDataAsync

        public <T extends com.azure.core.experimental.models.MessageWithMetadata> Mono<T> serializeMessageDataAsync​(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 MessageWithMetadata.
        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.
        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.autoRegisterSchema(boolean) is false.
        com.azure.core.exception.HttpResponseException - if an error occurred while trying to fetch the schema from the service.
      • deserializeMessageData

        public <T> T deserializeMessageData​(com.azure.core.experimental.models.MessageWithMetadata message,
                                            com.azure.core.util.serializer.TypeReference<T> typeReference)
        Deserializes a message into its object.
        Type Parameters:
        T - Concrete type of MessageWithMetadata.
        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.autoRegisterSchema(boolean) is false.
        com.azure.core.exception.HttpResponseException - if an error occurred while trying to fetch the schema from the service.
      • deserializeMessageDataAsync

        public <T> Mono<T> deserializeMessageDataAsync​(com.azure.core.experimental.models.MessageWithMetadata message,
                                                       com.azure.core.util.serializer.TypeReference<T> typeReference)
        Deserializes a message into its object.
        Type Parameters:
        T - Concrete type of MessageWithMetadata.
        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.autoRegisterSchema(boolean) is false.
        com.azure.core.exception.HttpResponseException - if an error occurred while trying to fetch the schema from the service.