class _EncodeTest(_SchemaRegistryAvroTest): def __init__(self, arguments): super().__init__(arguments) self.sync_credential = DefaultAzureCredential() self.sync_client = SchemaRegistryClient( fully_qualified_namespace=self.fully_qualified_namespace, credential=self.sync_credential, ) self.sync_encoder = AvroEncoder(client=self.sync_client, group_name=self.group_name, auto_register_schemas=True) self.async_credential = AsyncDefaultAzureCredential() self.async_client = AsyncSchemaRegistryClient( fully_qualified_namespace=self.fully_qualified_namespace, credential=self.async_credential, ) self.async_encoder = AsyncAvroEncoder(client=self.async_client, group_name=self.group_name, auto_register_schemas=True) async def global_setup(self): await super().global_setup() async def close(self): self.sync_client.close() self.sync_credential.close() self.sync_encoder.close() await self.async_client.close() await self.async_credential.close() await self.async_encoder.close() await super().close()
class _GetSchemaTest(_SchemaRegistryTest): def __init__(self, arguments): super().__init__(arguments) self.sync_credential = DefaultAzureCredential() self.sync_client = SchemaRegistryClient( fully_qualified_namespace=self.fully_qualified_namespace, credential=self.sync_credential, ) self.async_credential = AsyncDefaultAzureCredential() self.async_client = AsyncSchemaRegistryClient( fully_qualified_namespace=self.fully_qualified_namespace, credential=self.async_credential, ) self.schema_id = self._preregister_schema() def _preregister_schema(self): with self.sync_client as client: schema_properties = client.register_schema(self.group_name, self.name, self.definition, self.format) return schema_properties.id async def global_setup(self): await super().global_setup() async def close(self): self.sync_client.close() self.sync_credential.close() await self.async_client.close() await self.async_credential.close() await super().close()
def __init__(self, arguments): super().__init__(arguments) self.sync_credential = DefaultAzureCredential() self.sync_client = SchemaRegistryClient( fully_qualified_namespace=self.fully_qualified_namespace, credential=self.sync_credential, ) self.async_credential = AsyncDefaultAzureCredential() self.async_client = AsyncSchemaRegistryClient( fully_qualified_namespace=self.fully_qualified_namespace, credential=self.async_credential, )
def test_schema_negative_wrong_credential(self, schemaregistry_endpoint, schemaregistry_group, **kwargs): credential = ClientSecretCredential(tenant_id="fake", client_id="fake", client_secret="fake") client = SchemaRegistryClient(endpoint=schemaregistry_endpoint, credential=credential) schema_name = self.get_resource_name('test-schema-negative') 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): client.register_schema(schemaregistry_group, schema_name, serialization_type, schema_str)
def create_client(): # [START create_sr_client_sync] 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_sync] 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
def test_schema_negative_wrong_credential(self, **kwargs): schemaregistry_fully_qualified_namespace = kwargs.pop( "schemaregistry_fully_qualified_namespace") print(schemaregistry_fully_qualified_namespace) schemaregistry_group = kwargs.pop("schemaregistry_group") credential = ClientSecretCredential(tenant_id="fake", client_id="fake", client_secret="fake") client = SchemaRegistryClient( fully_qualified_namespace=schemaregistry_fully_qualified_namespace, credential=credential) name = self.get_resource_name('test-schema-negative') 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"]}]}""" format = "Avro" with pytest.raises(ClientAuthenticationError): client.register_schema(schemaregistry_group, name, schema_str, format)
def create_client(): # [START create_sr_client_sync] 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_sync] return schema_registry_client
def __init__(self, arguments): super().__init__(arguments) self.sync_credential = DefaultAzureCredential() self.sync_client = SchemaRegistryClient( fully_qualified_namespace=self.fully_qualified_namespace, credential=self.sync_credential, ) self.sync_encoder = AvroEncoder(client=self.sync_client, group_name=self.group_name, auto_register_schemas=True) self.async_credential = AsyncDefaultAzureCredential() self.async_client = AsyncSchemaRegistryClient( fully_qualified_namespace=self.fully_qualified_namespace, credential=self.async_credential, ) self.async_encoder = AsyncAvroEncoder(client=self.async_client, group_name=self.group_name, auto_register_schemas=True)
class _RegisterTest(_SchemaRegistryTest): def __init__(self, arguments): super().__init__(arguments) self.sync_credential = DefaultAzureCredential() self.sync_client = SchemaRegistryClient( fully_qualified_namespace=self.fully_qualified_namespace, credential=self.sync_credential, ) self.async_credential = AsyncDefaultAzureCredential() self.async_client = AsyncSchemaRegistryClient( fully_qualified_namespace=self.fully_qualified_namespace, credential=self.async_credential, ) async def global_setup(self): await super().global_setup() async def close(self): self.sync_client.close() self.sync_credential.close() await self.async_client.close() await self.async_credential.close() await super().close()
def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') schema_registry = SchemaRegistryClient(endpoint=SCHEMA_REGISTRY_ENDPOINT, credential=token_credential) serializer = SchemaRegistryAvroSerializer(schema_registry, SCHEMA_GROUP) bytes_data_ben, bytes_data_alice = serialize(serializer) dict_data_ben = deserialize(serializer, bytes_data_ben) dict_data_alice = deserialize(serializer, bytes_data_alice) serializer.close() return func.HttpResponse( "Schema Registry Executed.", status_code=200 )
schema_id, schema.schema_content)) print("Schema properties are {}".format(schema_id)) return schema.schema_content def get_schema_id(client, schema_group, schema_name, serialization_type, schema_string): print("Getting schema id...") schema_properties = client.get_schema_id(schema_group, schema_name, serialization_type, schema_string) print("The schema id is: {}".format(schema_properties.schema_id)) print("Schema properties are {}".format(schema_properties)) return schema_properties.schema_id if __name__ == '__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) with schema_registry_client: schema_id = register_schema(schema_registry_client, SCHEMA_GROUP, SCHEMA_NAME, SERIALIZATION_TYPE, SCHEMA_STRING) schema_str = get_schema_by_id(schema_registry_client, schema_id=schema_id) schema_id = get_schema_id(schema_registry_client, SCHEMA_GROUP, SCHEMA_NAME, SERIALIZATION_TYPE, SCHEMA_STRING)
) def decode_with_content_and_content_type(encoder, event_data): # get content as bytes content = bytearray() for c in event_data.body: content += c content_bytes = bytes(content) message_content = MessageContent({ "content": content_bytes, "content_type": event_data.content_type }) decoded_content = encoder.decode(message_content) print("Decoded content is: ", decoded_content) return decoded_content if __name__ == "__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 = encode_message_content_dict(encoder) decoded_content = decode_with_content_and_content_type(encoder, event_data) encoder.close()
# The deserialize method would extract the schema id from the payload, and automatically retrieve the Avro Schema # from the Schema Registry Service. The schema would be cached locally for future usage. deserialized_data = avro_serializer.deserialize(bytes_payload) print( 'The dict data after deserialization is {}'.format(deserialized_data)) # create an EventHubConsumerClient instance eventhub_consumer = EventHubConsumerClient.from_connection_string( conn_str=EVENTHUB_CONNECTION_STR, consumer_group='$Default', eventhub_name=EVENTHUB_NAME, ) # create a AvroSerializer instance avro_serializer = AvroSerializer(client=SchemaRegistryClient( fully_qualified_namespace=SCHEMAREGISTRY_FULLY_QUALIFIED_NAMESPACE, credential=DefaultAzureCredential()), group_name=GROUP_NAME, auto_register_schemas=True) try: with eventhub_consumer, avro_serializer: eventhub_consumer.receive( on_event=on_event, starting_position= "-1", # "-1" is from the beginning of the partition. ) except KeyboardInterrupt: print('Stopped receiving.')
def get_schema_by_id(client, schema_id): print("Getting schema by id...") schema = client.get_schema(schema_id) print("The schema string of schema id: {} string is {}".format( id, schema.definition)) print("Schema properties are {}".format(schema_id)) return schema.definition def get_schema_id(client, group_name, name, definition, format): print("Getting schema id...") schema_properties = client.get_schema_properties(group_name, name, definition, format) print("The schema id is: {}".format(schema_properties.id)) print("Schema properties are {}".format(schema_properties)) return schema_properties.id if __name__ == "__main__": token_credential = DefaultAzureCredential() schema_registry_client = SchemaRegistryClient( fully_qualified_namespace=SCHEMAREGISTRY_FQN, credential=token_credential) with schema_registry_client: schema_id = register_schema(schema_registry_client, GROUP_NAME, NAME, DEFINITION, FORMAT) schema_str = get_schema_by_id(schema_registry_client, schema_id) schema_id = get_schema_id(schema_registry_client, GROUP_NAME, NAME, DEFINITION, FORMAT)
# The serialize method would automatically register the schema into the Schema Registry Service and # schema would be cached locally for future usage. payload_bytes = serializer.serialize(data=dict_data, schema=SCHEMA_STRING) print('The bytes of serialized dict data is {}.'.format(payload_bytes)) event_data = EventData(body=payload_bytes) # pass the bytes data to the body of an EventData event_data_batch.add(event_data) producer.send_batch(event_data_batch) print('Send is done.') # create an EventHubProducerClient instance eventhub_producer = EventHubProducerClient.from_connection_string( conn_str=EVENTHUB_CONNECTION_STR, eventhub_name=EVENTHUB_NAME ) # create a SchemaRegistryAvroSerializer instance avro_serializer = SchemaRegistryAvroSerializer( schema_registry=SchemaRegistryClient( endpoint=SCHEMA_REGISTRY_ENDPOINT, credential=DefaultAzureCredential() ), schema_group=SCHEMA_GROUP ) with eventhub_producer, avro_serializer: send_event_data_batch(eventhub_producer, avro_serializer)