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> T
deserialize
(MessageContent message, TypeReference<T> typeReference) Deserializes a message into its object.<T> Mono<T>
deserializeAsync
(MessageContent message, TypeReference<T> typeReference) Deserializes a message into its object.<T extends MessageContent>
Tserialize
(Object object, TypeReference<T> typeReference) Serializes an object into a message.<T extends MessageContent>
Tserialize
(Object object, TypeReference<T> typeReference, Function<BinaryData, T> messageFactory) Serializes an object into a message.<T extends MessageContent>
Mono<T>serializeAsync
(Object object, TypeReference<T> typeReference) Serializes an object into a message.<T extends MessageContent>
Mono<T>serializeAsync
(Object object, TypeReference<T> typeReference, Function<BinaryData, T> messageFactory) Serializes an object into a message.
-
Method Details
-
serialize
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
null
if the message could not be serialized. - Throws:
IllegalArgumentException
- ifT
does not have a no argument constructor. Or if the schema could not be fetched fromT
.RuntimeException
- if an instance ofT
could not be instantiated.SchemaRegistryApacheAvroException
- if an instance ofT
could not be instantiated or there was a problem serializing the object.NullPointerException
- if theobject
is null ortypeReference
is null.ResourceNotFoundException
- if the schema could not be found andSchemaRegistryApacheAvroSerializerBuilder.autoRegisterSchemas(boolean)
is false.HttpResponseException
- if an error occurred while trying to fetch the schema from the service.
-
serialize
public <T extends MessageContent> T serialize(Object object, TypeReference<T> typeReference, Function<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
null
if the message could not be serialized. - Throws:
IllegalArgumentException
- ifmessageFactory
is null and typeT
does not have a no argument constructor. Or if the schema could not be fetched fromT
.RuntimeException
- if an instance ofT
could not be instantiated.NullPointerException
- if theobject
is null ortypeReference
is null.SchemaRegistryApacheAvroException
- if the object could not be serialized.ResourceNotFoundException
- if the schema could not be found andSchemaRegistryApacheAvroSerializerBuilder.autoRegisterSchemas(boolean)
is false.HttpResponseException
- if an error occurred while trying to fetch the schema from the service.
-
serializeAsync
public <T extends MessageContent> Mono<T> serializeAsync(Object object, 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
- ifT
does not have a no argument constructor. Or if the schema could not be fetched fromT
.RuntimeException
- if an instance ofT
could not be instantiated.NullPointerException
- if theobject
is null ortypeReference
is null.SchemaRegistryApacheAvroException
- if the object could not be serialized.ResourceNotFoundException
- if the schema could not be found andSchemaRegistryApacheAvroSerializerBuilder.autoRegisterSchemas(boolean)
is false.HttpResponseException
- if an error occurred while trying to fetch the schema from the service.
-
serializeAsync
public <T extends MessageContent> Mono<T> serializeAsync(Object object, TypeReference<T> typeReference, Function<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
- ifmessageFactory
is null and typeT
does not have a no argument constructor. Or if the schema could not be fetched fromT
.IllegalStateException
- ifschemaGroup
is not set.RuntimeException
- if an instance ofT
could not be instantiated.NullPointerException
- if theobject
is null ortypeReference
is null.SchemaRegistryApacheAvroException
- if the object could not be serialized.ResourceNotFoundException
- if the schema could not be found andSchemaRegistryApacheAvroSerializerBuilder.autoRegisterSchemas(boolean)
is false.HttpResponseException
- if an error occurred while trying to fetch the schema from the service.
-
deserialize
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
- ifmessage
ortypeReference
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.ResourceNotFoundException
- if a schema with a matching schema id could not be found.HttpResponseException
- if an issue was encountered while fetching the schema.SchemaRegistryApacheAvroException
- if the message could not be deserialized.ResourceNotFoundException
- if the schema could not be found andSchemaRegistryApacheAvroSerializerBuilder.autoRegisterSchemas(boolean)
is false.HttpResponseException
- if an error occurred while trying to fetch the schema from the service.
-
deserializeAsync
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
- ifmessage
ortypeReference
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.ResourceNotFoundException
- if a schema with a matching schema id could not be found.HttpResponseException
- if an issue was encountered while fetching the schema.SchemaRegistryApacheAvroException
- if the message could not be deserialized.ResourceNotFoundException
- if the schema could not be found andSchemaRegistryApacheAvroSerializerBuilder.autoRegisterSchemas(boolean)
is false.HttpResponseException
- if an error occurred while trying to fetch the schema from the service.
-