SchemaRegistryApacheAvroSerializer Class

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

public final class SchemaRegistryApacheAvroSerializer

Schema Registry-based serializer implementation for Avro data format using Apache Avro.

Sample: Creating a SchemaRegistryApacheAvroSerializer

The following code sample demonstrates the creation of the serializer and SchemaRegistryAsyncClient. The credential used to create the async client 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, serialize(Object object, TypeReference<T> typeReference, Function<BinaryData,T> messageFactory).

// 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, it is assumed that the events in eventsList were created with the "Sample: Serialize an object" snippet above before being published to an Event Hub.

List<EventData> eventsList =
     consumer.receiveFromPartition("0", 10, EventPosition.latest(), Duration.ofSeconds(30))
         .stream()
         .map(partitionEvent -> partitionEvent.getData())
         .collect(Collectors.toList());

 for (EventData eventData : eventsList) {
     Person person = serializer.deserialize(eventData, TypeReference.createInstance(Person.class));

     System.out.printf("Name: %s, Number: %s%n", person.getName(), person.getFavouriteNumber());
 }

Sample: Serialize an object using a message factory

Serializes an Avro generated object into MessageContent. It uses the messageFactory to instantiate and populate the type. This overload is useful in cases where the type does not have a no argument constructor.

// 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")
     .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 and Description
T deserialize(MessageContent message, TypeReference<T> typeReference)

Deserializes a message into its object.

Mono<T> deserializeAsync(MessageContent message, TypeReference<T> typeReference)

Deserializes a message into its object.

T serialize(Object object, TypeReference<T> typeReference)

Serializes an object into a message.

T serialize(Object object, TypeReference<T> typeReference, Function<BinaryData,T> messageFactory)

Serializes an object into a message.

Mono<T> serializeAsync(Object object, TypeReference<T> typeReference)

Serializes an object into a message.

Mono<T> serializeAsync(Object object, TypeReference<T> typeReference, Function<BinaryData,T> messageFactory)

Serializes an object into a message.

Methods inherited from java.lang.Object

Method Details

deserialize

public T <T>deserialize(MessageContent message, TypeReference<T> typeReference)

Deserializes a message into its object.

Parameters:

message - Object to deserialize.
typeReference - Message type to deserialize to.

Returns:

The message deserialized.

deserializeAsync

public Mono<T> <T>deserializeAsync(MessageContent message, TypeReference<T> typeReference)

Deserializes a message into its object.

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.

serialize

public T <T>serialize(Object object, TypeReference<T> typeReference)

Serializes an object into a message.

Parameters:

object - Object to serialize.
typeReference - Type of message to create.

Returns:

The message encoded or null if the message could not be serialized.

serialize

public T <T>serialize(Object object, TypeReference<T> typeReference, Function<BinaryData,T> messageFactory)

Serializes an object into a message.

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.

serializeAsync

public Mono<T> <T>serializeAsync(Object object, TypeReference<T> typeReference)

Serializes an object into a message.

Parameters:

object - Object to serialize.
typeReference - Type of message to create.

Returns:

A Mono that completes with the serialized message.

serializeAsync

public Mono<T> <T>serializeAsync(Object object, TypeReference<T> typeReference, Function<BinaryData,T> messageFactory)

Serializes an object into a message.

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.

Applies to