azure.schemaregistry package

class azure.schemaregistry.ApiVersion(value)[source]

Represents the Schema Registry API version to use for requests.

V2021_10 = '2021-10'

This is the default version.

class azure.schemaregistry.Schema(**kwargs: Any)[source]

The schema content of a schema, along with id and meta properties.

Variables
  • definition (str) – The content of the schema.

  • properties (SchemaProperties) – The properties of the schema.

class azure.schemaregistry.SchemaFormat(value)[source]

Represents the format of the schema to be stored by the Schema Registry service.

AVRO = 'Avro'

Represents the Apache Avro schema format.

class azure.schemaregistry.SchemaProperties(**kwargs: Any)[source]

Meta properties of a schema.

Variables
  • id (str) – References specific schema in registry namespace.

  • format (SchemaFormat) – Format for the schema being stored.

  • group_name (str) – Schema group under which schema is stored.

  • name (str) – Name of schema.

  • version (int) – Version of schema.

class azure.schemaregistry.SchemaRegistryClient(fully_qualified_namespace: str, credential: TokenCredential, **kwargs: Any)[source]

SchemaRegistryClient is a client for registering and retrieving schemas from the Azure Schema Registry service.

Parameters
  • fully_qualified_namespace (str) – The Schema Registry service fully qualified host name. For example: my-namespace.servicebus.windows.net.

  • credential (TokenCredential) – To authenticate managing the entities of the SchemaRegistry namespace.

Keyword Arguments

api_version (str) – The Schema Registry service API version to use for requests. Default value and only accepted value currently is “2021-10”.

Example:

Create a new instance of the SchemaRegistryClient.
SCHEMAREGISTRY_FQN = os.environ["SCHEMAREGISTRY_FULLY_QUALIFIED_NAMESPACE"]
token_credential = DefaultAzureCredential()
schema_registry_client = SchemaRegistryClient(
    fully_qualified_namespace=SCHEMAREGISTRY_FQN, credential=token_credential
)
close()None[source]

This method is to close the sockets opened by the client. It need not be used when using with a context manager.

get_schema(schema_id: str, **kwargs: Any)Schema[source]
get_schema(*, group_name: str, name: str, version: int, **kwargs)Schema

Gets a registered schema. There are two ways to call this method:

1) To get a registered schema by its unique ID, pass the schema_id parameter and any optional keyword arguments. Azure Schema Registry guarantees that ID is unique within a namespace.

2) To get a specific version of a schema within the specified schema group, pass in the required keyword arguments group_name, name, and version and any optional keyword arguments.

Parameters

schema_id (str) – References specific schema in registry namespace.

Keyword Arguments
  • group_name (str) – Name of schema group that contains the registered schema.

  • name (str) – Name of schema which should be retrieved.

  • version (int) – Version of schema which should be retrieved.

Return type

Schema

Raises

HttpResponseError

Example:

Get schema by id.
schema = schema_registry_client.get_schema(schema_id)
definition = schema.definition
properties = schema.properties
Get schema by version.
group_name = os.environ["SCHEMAREGISTRY_GROUP"]
name = "your-schema-name"
schema = schema_registry_client.get_schema(group_name=group_name, name=name, version=version)
definition = schema.definition
properties = schema.properties
get_schema_properties(group_name: str, name: str, definition: str, format: Union[str, azure.schemaregistry._common._constants.SchemaFormat], **kwargs: Any)azure.schemaregistry._common._schema.SchemaProperties[source]

Gets the schema properties corresponding to an existing schema within the specified schema group, as matched by schema definition comparison.

Parameters
  • group_name (str) – Schema group under which schema should be registered.

  • name (str) – Name of schema for which properties should be retrieved.

  • definition (str) – String representation of the schema for which properties should be retrieved.

  • format (Union[str, SchemaFormat]) – Format for the schema for which properties should be retrieved.

Return type

SchemaProperties

Raises

HttpResponseError

Example:

Get schema id.
group_name = os.environ["SCHEMAREGISTRY_GROUP"]
name = "your-schema-name"
format = "Avro"
schema_json = {
    "namespace": "example.avro",
    "type": "record",
    "name": "User",
    "fields": [
        {"name": "name", "type": "string"},
        {"name": "favorite_number", "type": ["int", "null"]},
        {"name": "favorite_color", "type": ["string", "null"]},
    ],
}
definition = json.dumps(schema_json, separators=(",", ":"))
schema_properties = schema_registry_client.get_schema_properties(
    group_name, name, definition, format
)
schema_id = schema_properties.id
register_schema(group_name: str, name: str, definition: str, format: Union[str, azure.schemaregistry._common._constants.SchemaFormat], **kwargs: Any)azure.schemaregistry._common._schema.SchemaProperties[source]

Register new schema. If schema of specified name does not exist in specified group, schema is created at version 1. If schema of specified name exists already in specified group, schema is created at latest version + 1.

Parameters
  • group_name (str) – Schema group under which schema should be registered.

  • name (str) – Name of schema being registered.

  • definition (str) – String representation of the schema being registered.

  • format (Union[str, SchemaFormat]) – Format for the schema being registered. For now Avro is the only supported schema format by the service.

Return type

SchemaProperties

Raises

HttpResponseError

Example:

Register a new schema.
GROUP_NAME = os.environ["SCHEMAREGISTRY_GROUP"]
NAME = "your-schema-name"
FORMAT = "Avro"
SCHEMA_JSON = {
    "namespace": "example.avro",
    "type": "record",
    "name": "User",
    "fields": [
        {"name": "name", "type": "string"},
        {"name": "favorite_number", "type": ["int", "null"]},
        {"name": "favorite_color", "type": ["string", "null"]},
    ],
}
DEFINTION = json.dumps(SCHEMA_JSON, separators=(",", ":"))
schema_properties = schema_registry_client.register_schema(
    GROUP_NAME, NAME, DEFINTION, FORMAT
)
schema_id = schema_properties.id