Compartilhar via


Azure Schema Registry Json Schema Serializer client library for Java - version 1.0.0

Azure Schema Registry Json Schema is a serializer and deserializer library for JSON data format that is integrated with Azure Schema Registry hosted in Azure Event Hubs, providing schema storage, versioning, and management. This package provides a serializer capable of serializing and deserializing payloads containing Schema Registry schema identifiers and JSON encoded data.

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-jsonschema</artifactId>
  <version>1.0.0</version>
</dependency>

Create SchemaRegistryJsonSchemaSerializer instance

The SchemaRegistryJsonSchemaSerializer instance is the main class that provides APIs for serializing and deserializing JSON schema format. The JSON 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.13.3</version>
</dependency>

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

TokenCredential tokenCredential = new DefaultAzureCredentialBuilder().build();
JsonSchemaGenerator jsonSchemaGenerator = null;

// {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 SchemaRegistryJsonSchemaSerializer through the builder

SchemaRegistryJsonSchemaSerializer serializer = new SchemaRegistryJsonSchemaSerializerBuilder()
    .schemaRegistryClient(schemaRegistryAsyncClient)
    .schemaGroup("{schema-group}")
    .jsonSchemaGenerator(jsonSchemaGenerator)
    .buildSerializer();

Key concepts

This library provides a serializer, SchemaRegistryJsonSchemaSerializer. The SchemaRegistryJsonSchemaSerializer utilizes a SchemaRegistryAsyncClient to construct messages using a wire format containing schema information such as a schema ID. This library allows users to serialize, deserialize, register, and validate JSON objects by plugging in a JSON schema implementation.

Examples

Serialize

Serialize a POJO object, Address, and register the matching JSON schema if SchemaRegistryJsonSchemaSerializerBuilder.autoRegisterSchema(boolean) is set to true in when creating the serializer.

Address address = new Address();
address.setNumber(105);
address.setStreetName("1st");
address.setStreetType("Street");

EventData eventData = serializer.serialize(address, TypeReference.createInstance(EventData.class));

The type Address is available in the test package com.azure.data.schemaregistry.jsonschema.Address.

Deserialize

Deserialize a Schema Registry-compatible JSON payload into a strongly-type object and validate it against its JSON schema.

SchemaRegistryJsonSchemaSerializer serializer = createSerializer();
MessageContent message = getSchemaRegistryJSONMessage();
Address address = serializer.deserialize(message, TypeReference.createInstance(Address.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