Azure Schema Registry client library for Java - version 1.3.1

Azure Schema Registry is a schema repository service hosted by Azure Event Hubs, providing schema storage, versioning, and management. The registry is leveraged by serializers to reduce payload size while describing payload structure with schema identifiers rather than full schemas.

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

Getting started

Prerequisites

Include the package

Include the BOM file

Please include the azure-sdk-bom to your project to take dependency on the General Availability (GA) version of the library. In the following snippet, replace the {bom_version_to_target} placeholder with the version number. To learn more about the BOM, see the AZURE SDK BOM README.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-sdk-bom</artifactId>
            <version>{bom_version_to_target}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

and then include the direct dependency in the dependencies section without the version tag as shown below.

<dependencies>
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-data-schemaregistry</artifactId>
    </dependency>
</dependencies>

Include direct dependency

If you want to take dependency on a particular version of the library that is not present in the BOM, add the direct dependency to your project as follows.

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

Authenticate the client

In order to interact with the Azure Schema Registry service, you'll need to create an instance of the SchemaRegistryClient class through the SchemaRegistryClientBuilder. You will need an endpoint and an API key to instantiate a client object.

Create SchemaRegistryClient with Azure Active Directory Credential

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.

Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET.

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

SchemaRegistryAsyncClient schemaRegistryAsyncClient = new SchemaRegistryClientBuilder()
    .fullyQualifiedNamespace("{schema-registry-endpoint")
    .credential(tokenCredential)
    .buildAsyncClient();
Sync client
TokenCredential tokenCredential = new DefaultAzureCredentialBuilder().build();

SchemaRegistryClient schemaRegistryClient = new SchemaRegistryClientBuilder()
    .fullyQualifiedNamespace("{schema-registry-endpoint")
    .credential(tokenCredential)
    .buildClient();

Key concepts

Schemas

A schema has 6 components:

  • Group Name: The name of the group of schemas in the Schema Registry instance.
  • Schema Name: The name of the schema.
  • Schema ID: The ID assigned by the Schema Registry instance for the schema.
  • Serialization Type: The format used for serialization of the schema. For example, Avro.
  • Schema Content: The string representation of the schema.
  • Schema Version: The version assigned to the schema in the Schema Registry instance.

These components play different roles. Some are used as input into the operations and some are outputs. Currently, SchemaProperties only exposes those properties that are potential outputs that are used in SchemaRegistry operations. Those exposed properties are Content and Id.

Examples

Register a schema

Register a schema to be stored in the Azure Schema Registry.

String schemaContent = "{\n"
    + "    \"type\" : \"record\",  \n"
    + "    \"namespace\" : \"SampleSchemaNameSpace\", \n"
    + "    \"name\" : \"Person\", \n"
    + "    \"fields\" : [\n"
    + "        { \n"
    + "            \"name\" : \"FirstName\" , \"type\" : \"string\" \n"
    + "        }, \n"
    + "        { \n"
    + "            \"name\" : \"LastName\", \"type\" : \"string\" \n"
    + "        }\n"
    + "    ]\n"
    + "}";
SchemaProperties schemaProperties = schemaRegistryClient.registerSchema("{schema-group}", "{schema-name}",
    schemaContent, SchemaFormat.AVRO);

System.out.println("Registered schema: " + schemaProperties.getId());

Retrieve a schema's properties

Retrieve a previously registered schema's properties from the Azure Schema Registry.

SchemaRegistrySchema schema = schemaRegistryClient.getSchema("{schema-id}");

System.out.printf("Retrieved schema: '%s'. Contents: %s%n", schema.getProperties().getId(),
    schema.getDefinition());

Retrieve a schema

Retrieve a previously registered schema's content and properties from the Azure Schema Registry.

String schemaContent = "{\n"
    + "    \"type\" : \"record\",  \n"
    + "    \"namespace\" : \"SampleSchemaNameSpace\", \n"
    + "    \"name\" : \"Person\", \n"
    + "    \"fields\" : [\n"
    + "        { \n"
    + "            \"name\" : \"FirstName\" , \"type\" : \"string\" \n"
    + "        }, \n"
    + "        { \n"
    + "            \"name\" : \"LastName\", \"type\" : \"string\" \n"
    + "        }\n"
    + "    ]\n"
    + "}";
SchemaProperties properties = schemaRegistryClient.getSchemaProperties("{schema-group}", "{schema-name}",
    schemaContent, SchemaFormat.AVRO);

System.out.println("Retrieved schema id: " + properties.getId());

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