Azure Schema Registry Client Library

1.3.0 · active · verified Sat Apr 11

The Azure Schema Registry client library for Python (version 1.3.0) provides a robust interface for interacting with the Azure Schema Registry service. This service, hosted by Azure Event Hubs, offers a centralized repository for managing, versioning, and storing schemas. The library enables applications to register and retrieve schemas, and includes a JSON schema-based encoder for efficient payload encoding and decoding using schema identifiers rather than full schema definitions. It adheres to the Azure SDK guidelines, offering a consistent and Pythonic experience. It is actively maintained with a regular release cadence as part of the broader Azure SDK for Python.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate a `SchemaRegistryClient` using `DefaultAzureCredential` and then register and retrieve a JSON schema. Ensure that `SCHEMAREGISTRY_FULLY_QUALIFIED_NAMESPACE`, `SCHEMAREGISTRY_GROUP_NAME`, and `SCHEMAREGISTRY_SCHEMA_NAME` environment variables are set, along with Azure AD credentials (`AZURE_TENANT_ID`, `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`) for `DefaultAzureCredential` to function.

import os
from azure.schemaregistry import SchemaRegistryClient
from azure.identity import DefaultAzureCredential

# Set environment variables for authentication and Schema Registry endpoint
# AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET for DefaultAzureCredential
# SCHEMAREGISTRY_FULLY_QUALIFIED_NAMESPACE for the Schema Registry endpoint

fully_qualified_namespace = os.environ.get('SCHEMAREGISTRY_FULLY_QUALIFIED_NAMESPACE', 'your-namespace.servicebus.windows.net')
schema_group = os.environ.get('SCHEMAREGISTRY_GROUP_NAME', 'my-schema-group')
schema_name = os.environ.get('SCHEMAREGISTRY_SCHEMA_NAME', 'my-json-schema')
schema_definition = '''{
    "type": "object",
    "$id": "https://example.com/example.json",
    "title": "Person",
    "description": "A person schema",
    "properties": {
        "name": {
            "type": "string",
            "description": "The person's name."
        },
        "age": {
            "type": "integer",
            "description": "The person's age.",
            "minimum": 0
        }
    },
    "required": ["name"]
}'''
schema_format = "Json"

credential = DefaultAzureCredential()
client = SchemaRegistryClient(fully_qualified_namespace, credential)

try:
    # Register a schema
    schema_properties = client.register_schema(
        group_name=schema_group,
        name=schema_name,
        format=schema_format,
        definition=schema_definition
    )
    print(f"Registered schema with ID: {schema_properties.id}")
    print(f"Schema Version: {schema_properties.version}")

    # Get a schema by its ID
    retrieved_schema = client.get_schema(schema_properties.id)
    print(f"Retrieved schema name: {retrieved_schema.name}")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Always close the credential when done (if using certain types like ManagedIdentityCredential)
    # DefaultAzureCredential handles this contextually.
    pass

view raw JSON →