Azure Schema Registry Apache Avro Serializer client library for Java - version 1.1.1

Azure Schema Registry Apache Avro is a serializer and deserializer library for Avro data format that is integrated with Azure Schema Registry hosted in Azure Event Hubs, providing schema storage, versioning, and management. This package provides an Avro serializer capable of serializing and deserializing payloads containing Schema Registry schema identifiers and Avro-encoded data. This library uses Apache Avro implementation for Avro serialization and deserialization.

Source code | Package (Maven) | API reference documentation | Product Documentation | Samples

Getting started

Prerequisites

Include the Package

<dependency>
  <groupId>com.azure</groupId>
  <artifactId>azure-data-schemaregistry-apacheavro</artifactId>
  <version>1.1.1</version>
</dependency>

Create SchemaRegistryApacheAvroSerializer instance

The SchemaRegistryApacheAvroSerializer instance is the main class that provides APIs for serializing and deserializing avro data format. The avro schema is stored and retrieved from the Schema Registry service through the SchemaRegistryAsyncClient. So, before we create the serializer, we should create the client.

Create SchemaRegistryAsyncClient with Azure Active Directory Credential

In order to interact with the Azure Schema Registry service, you'll need to create an instance of the SchemaRegistryAsyncClient class through the SchemaRegistryClientBuilder. You will need the Schema Registry endpoint.

You can authenticate with Azure Active Directory using the Azure Identity library. Note that regional endpoints do not support AAD authentication. Create a custom subdomain for your resource in order to use this type of authentication.

To use the DefaultAzureCredential provider shown below, or other credential providers provided with the Azure SDK, please include the azure-identity package:

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-identity</artifactId>
    <version>1.5.4</version>
</dependency>

You will also need to register a new AAD application and grant access to Schema Registry service.

TokenCredential tokenCredential = new DefaultAzureCredentialBuilder().build();

// {schema-registry-endpoint} is the fully qualified namespace of the Event Hubs instance. It is usually
// of the form "{your-namespace}.servicebus.windows.net"
SchemaRegistryAsyncClient schemaRegistryAsyncClient = new SchemaRegistryClientBuilder()
    .fullyQualifiedNamespace("{your-event-hubs-namespace}.servicebus.windows.net")
    .credential(tokenCredential)
    .buildAsyncClient();

Create SchemaRegistryAvroSerializer through the builder

SchemaRegistryApacheAvroSerializer serializer = new SchemaRegistryApacheAvroSerializerBuilder()
    .schemaRegistryClient(schemaRegistryAsyncClient)
    .schemaGroup("{schema-group}")
    .buildSerializer();

Key concepts

This library provides a serializer, SchemaRegistryApacheAvroSerializer. The SchemaRegistryAvroSerializer utilizes a SchemaRegistryAsyncClient to construct messages using a wire format containing schema information such as a schema ID.

This serializer requires the Apache Avro library. The payload types accepted by this serializer include GenericRecord and SpecificRecord.

Examples

Serialize

Serialize a strongly-typed object into Schema Registry-compatible avro payload.

PlayingCard playingCard = new PlayingCard();
playingCard.setPlayingCardSuit(PlayingCardSuit.SPADES);
playingCard.setIsFaceCard(false);
playingCard.setCardValue(5);

MessageContent message = serializer.serialize(playingCard,
    TypeReference.createInstance(MessageContent.class));

The avro type PlayingCard is available in samples package com.azure.data.schemaregistry.avro.generatedtestsources.

Deserialize

Deserialize a Schema Registry-compatible avro payload into a strongly-type object.

SchemaRegistryApacheAvroSerializer serializer = createAvroSchemaRegistrySerializer();
MessageContent message = getSchemaRegistryAvroMessage();
PlayingCard playingCard = serializer.deserialize(message, TypeReference.createInstance(PlayingCard.class));

Troubleshooting

Enabling Logging

Azure SDKs for Java offer a consistent logging story to help aid in troubleshooting application errors and expedite their resolution. The logs produced will capture the flow of an application before reaching the terminal state to help locate the root issue. View the logging wiki for guidance about enabling logging.

Next steps

More samples can be found here.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Impressions