def create_client(): # [START create_sr_client_async] SCHEMA_REGISTRY_ENDPOINT = os.environ['SCHEMA_REGISTRY_ENDPOINT'] token_credential = DefaultAzureCredential() schema_registry_client = SchemaRegistryClient( endpoint=SCHEMA_REGISTRY_ENDPOINT, credential=token_credential) # [END create_sr_client_async] TENANT_ID = os.environ['SCHEMA_REGISTRY_AZURE_TENANT_ID'] CLIENT_ID = os.environ['SCHEMA_REGISTRY_AZURE_CLIENT_ID'] CLIENT_SECRET = os.environ['SCHEMA_REGISTRY_AZURE_CLIENT_SECRET'] token_credential = ClientSecretCredential(TENANT_ID, CLIENT_ID, CLIENT_SECRET) schema_registry_client = SchemaRegistryClient( endpoint=SCHEMA_REGISTRY_ENDPOINT, credential=token_credential) return schema_registry_client, token_credential
def create_client(): # [START create_sr_client_async] SCHEMAREGISTRY_FQN = os.environ["SCHEMAREGISTRY_FULLY_QUALIFIED_NAMESPACE"] token_credential = DefaultAzureCredential() schema_registry_client = SchemaRegistryClient( fully_qualified_namespace=SCHEMAREGISTRY_FQN, credential=token_credential) # [END create_sr_client_async] return schema_registry_client, token_credential
async def main(): token_credential = DefaultAzureCredential() schema_registry_client = SchemaRegistryClient( fully_qualified_namespace=SCHEMAREGISTRY_FQN, credential=token_credential) async with token_credential, schema_registry_client: schema_id = await register_schema(schema_registry_client, GROUP_NAME, NAME, DEFINITION, FORMAT) schema_str = await get_schema_by_id(schema_registry_client, schema_id) schema_id = await get_schema_id(schema_registry_client, GROUP_NAME, NAME, DEFINITION, FORMAT)
async def main(): schema_registry = SchemaRegistryClient( fully_qualified_namespace=SCHEMAREGISTRY_FULLY_QUALIFIED_NAMESPACE, credential=token_credential, ) encoder = AvroEncoder(client=schema_registry, group_name=GROUP_NAME, auto_register_schemas=True) event_data_ben = await encode_with_callback(encoder) await encoder.close() await token_credential.close()
async def main(): schema_registry = SchemaRegistryClient( fully_qualified_namespace=SCHEMAREGISTRY_FULLY_QUALIFIED_NAMESPACE, credential=token_credential, ) encoder = AvroEncoder(client=schema_registry, group_name=GROUP_NAME, auto_register_schemas=True) event_data = await encode_metadata_dict(encoder) decoded_data = await decode_with_data_and_content_type(encoder, event_data) await encoder.close() await token_credential.close()
async def main(): schema_registry = SchemaRegistryClient( fully_qualified_namespace=SCHEMAREGISTRY_FULLY_QUALIFIED_NAMESPACE, credential=token_credential) serializer = AvroSerializer(client=schema_registry, group_name=GROUP_NAME, auto_register_schemas=True) bytes_data_ben, bytes_data_alice = await serialize(serializer) dict_data_ben = await deserialize(serializer, bytes_data_ben) dict_data_alice = await deserialize(serializer, bytes_data_alice) await serializer.close() await token_credential.close()
async def test_schema_negative_wrong_credential_async( self, schemaregistry_endpoint, schemaregistry_group, **kwargs): credential = ClientSecretCredential(tenant_id="fake", client_id="fake", client_secret="fake") client = SchemaRegistryClient(endpoint=schemaregistry_endpoint, credential=credential) async with client, credential: schema_name = self.get_resource_name('test-schema-negative-async') schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}""" serialization_type = "Avro" with pytest.raises(ClientAuthenticationError): await client.register_schema(schemaregistry_group, schema_name, serialization_type, schema_str)
async def main(): token_credential = ClientSecretCredential(tenant_id=TENANT_ID, client_id=CLIENT_ID, client_secret=CLIENT_SECRET) schema_registry_client = SchemaRegistryClient( endpoint=SCHEMA_REGISTRY_ENDPOINT, credential=token_credential) async with token_credential, schema_registry_client: schema_id = await register_schema(schema_registry_client, SCHEMA_GROUP, SCHEMA_NAME, SERIALIZATION_TYPE, SCHEMA_STRING) schema_str = await get_schema_by_id(schema_registry_client, schema_id) schema_id = await get_schema_id(schema_registry_client, SCHEMA_GROUP, SCHEMA_NAME, SERIALIZATION_TYPE, SCHEMA_STRING)
async def main(): schema_registry = SchemaRegistryClient( fully_qualified_namespace=SCHEMAREGISTRY_FULLY_QUALIFIED_NAMESPACE, credential=token_credential, ) encoder = AvroEncoder(client=schema_registry, group_name=GROUP_NAME, auto_register=True) event_data_ben, event_data_alice = await encode_to_event_data_message( encoder) decoded_content_ben = await decode_event_data_message( encoder, event_data_ben) decoded_content_alice = await decode_event_data_message( encoder, event_data_alice) await encoder.close() await token_credential.close()
"name": "User", "fields": [ {"name": "name", "type": "string"}, {"name": "favorite_number", "type": ["int", "null"]}, {"name": "favorite_color", "type": ["string", "null"]} ] }""" # create an EventHubProducerClient instance eventhub_producer = EventHubProducerClient.from_connection_string( conn_str=EVENTHUB_CONNECTION_STR, eventhub_name=EVENTHUB_NAME) # create a AvroEncoder instance azure_credential = DefaultAzureCredential() # create a AvroEncoder instance avro_encoder = AvroEncoder(client=SchemaRegistryClient( fully_qualified_namespace=SCHEMAREGISTRY_FULLY_QUALIFIED_NAMESPACE, credential=azure_credential), group_name=GROUP_NAME, auto_register=True) async def send_event_data_batch(producer, encoder): event_data_batch = await producer.create_batch() dict_content = { "name": "Bob", "favorite_number": 7, "favorite_color": "red" } # Use the encode method to convert dict object to bytes with the given avro schema and set body of EventData. # The encode method will automatically register the schema into the Schema Registry Service and # schema will be cached locally for future usage.