Skip navigation links

Azure SDK for Java Reference Documentation

Current version is 1.0.0-beta.4, click here for the index

See: Description

Other Packages 
Package Description
com.azure.data.schemaregistry
Package containing clients for Azure Schema Registry service.
com.azure.data.schemaregistry.models
Package containing the model classes for schema registry.
Current version is 1.0.0-beta.4, click here for the index

Azure Schema Registry client library for Java

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][sample_readme]

Getting started

Prerequisites

Include the Package

<dependency>
  <groupId>com.azure</groupId>
  <artifactId>azure-data-schemaregistry</artifactId>
  <version>1.0.0-beta.3</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][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.1.2</version>
</dependency>

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

Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: AZURECLIENTID, AZURETENANTID, AZURECLIENTSECRET.

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

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

SchemaRegistryClient schemaRegistryClient = new SchemaRegistryClientBuilder()
    .endpoint("{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][schema_properties] 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.

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

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

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, SerializationType.AVRO);
System.out.println("Registered schema: " + schemaProperties.getSchemaId());

Retrieve a schema ID

Retrieve a previously registered schema ID from the Azure Schema Registry.

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

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

SchemaProperties schemaProperties = schemaRegistryClient.getSchema("{schema-id}");
System.out.println("Retrieved schema: " + schemaProperties.getSchemaName());

Retrieve a schema

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

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

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

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"
    + "}";
String schemaId = schemaRegistryClient.getSchemaId("{schema-group}", "{schema-name}",
    schemaContent, SerializationType.AVRO);
System.out.println("Retreived schema id: " + schemaId);

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][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)][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][coc]. For more information see the [Code of Conduct FAQ][cocfaq] or contact [opencode@microsoft.com][coccontact] with any additional questions or comments.

Impressions

Skip navigation links
Visit the Azure for Java Developerssite for more Java documentation, including quick starts, tutorials, and code samples.

Copyright © 2020 Microsoft Corporation. All rights reserved.