Class SchemaRegistryApacheAvroSerializer
java.lang.Object
com.azure.data.schemaregistry.apacheavro.SchemaRegistryApacheAvroSerializer
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 intoMessageContent. 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 intoMessageContent. 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 TypeMethodDescription<T> Tdeserialize(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>
TSerializes an object into a message.<T extends com.azure.core.models.MessageContent>
Tserialize(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.
-
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 ofMessageContent.- Parameters:
object- Object to serialize.typeReference- Type of message to create.- Returns:
- The message encoded or
nullif the message could not be serialized. - Throws:
IllegalArgumentException- ifTdoes not have a no argument constructor. Or if the schema could not be fetched fromT.RuntimeException- if an instance ofTcould not be instantiated.SchemaRegistryApacheAvroException- if an instance ofTcould not be instantiated or there was a problem serializing the object.NullPointerException- if theobjectis null ortypeReferenceis null.com.azure.core.exception.ResourceNotFoundException- if the schema could not be found andSchemaRegistryApacheAvroSerializerBuilder.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 ofMessageContent.- 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
nullif the message could not be serialized. - Throws:
IllegalArgumentException- ifmessageFactoryis null and typeTdoes not have a no argument constructor. Or if the schema could not be fetched fromT.RuntimeException- if an instance ofTcould not be instantiated.NullPointerException- if theobjectis null ortypeReferenceis null.SchemaRegistryApacheAvroException- if the object could not be serialized.com.azure.core.exception.ResourceNotFoundException- if the schema could not be found andSchemaRegistryApacheAvroSerializerBuilder.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 ofMessageContent.- Parameters:
object- Object to serialize.typeReference- Type of message to create.- Returns:
- A Mono that completes with the serialized message.
- Throws:
IllegalArgumentException- ifTdoes not have a no argument constructor. Or if the schema could not be fetched fromT.RuntimeException- if an instance ofTcould not be instantiated.NullPointerException- if theobjectis null ortypeReferenceis null.SchemaRegistryApacheAvroException- if the object could not be serialized.com.azure.core.exception.ResourceNotFoundException- if the schema could not be found andSchemaRegistryApacheAvroSerializerBuilder.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 ofMessageContent.- 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- ifmessageFactoryis null and typeTdoes not have a no argument constructor. Or if the schema could not be fetched fromT.IllegalStateException- ifschemaGroupis not set.RuntimeException- if an instance ofTcould not be instantiated.NullPointerException- if theobjectis null ortypeReferenceis null.SchemaRegistryApacheAvroException- if the object could not be serialized.com.azure.core.exception.ResourceNotFoundException- if the schema could not be found andSchemaRegistryApacheAvroSerializerBuilder.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 ofMessageContent.- Parameters:
message- Object to deserialize.typeReference- Message type to deserialize to.- Returns:
- The message deserialized.
- Throws:
NullPointerException- ifmessageortypeReferenceis 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 andSchemaRegistryApacheAvroSerializerBuilder.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 ofMessageContent.- 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- ifmessageortypeReferenceis 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 andSchemaRegistryApacheAvroSerializerBuilder.autoRegisterSchemas(boolean)is false.com.azure.core.exception.HttpResponseException- if an error occurred while trying to fetch the schema from the service.
-