SCHEMA_REGISTRY_ENDPOINT = "<your-event-hub>.servicebus.windows.net"
SCHEMA_GROUP="user_schemagroup"
SCHEMA_STRING = """
{"namespace": "example.avro",
"type": "record",
"name": "User",
"fields": [
{"name": "user_name", "type": "string"},
{"name": "user_score", "type": ["int", "null"]},
{"name": "user_id", "type": ["string", "null"]}
]
}"""
import random
import string
import uuid
def send_event_data_batch(producer, serializer):
event_data_batch = producer.create_batch()
username = ''.join(random.choice(string.ascii_lowercase) for i in range(10))
number = random.randint(1, 9)
guid = str(uuid.uuid4())
dict_data = {"user_name": username, "user_score": number, "user_id": guid}
# Use the serialize method to convert dict object to bytes with the given avro schema.
# 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.')
Produce events to Event Hub with Azure Schema Registry
This code is a slight modification of what's available here - essentially, it has been modified to randomize the data being sent.
Last refresh: Never