How do i send data to specific partition (let's say partition 0) in event hub

409 views Asked by At

i have created 2 partitions in event hub following the same code to send data to event hub https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/eventhub/azure-eventhub/README.md#publish-events-to-an-event-hub

below code i tried i was able to send data but was not able to send it to specific partition

def submit_images(jsondata):      
    connection_str = ['****************************']
    eventhub_name = ['*********']
    client = EventHubProducerClient.from_connection_string(connection_str, eventhub_name=eventhub_name)
    
    event_data_batch = client.create_batch(partition_id=0)
   
    event_data_batch.add(EventData(jsondata))      

    with client:
        client.send_batch(event_data_batch)

    return jsondata
1

There are 1 answers

0
Kashif Khan On

the partition_id kwarg takes in a string value, so changing this line to event_data_batch = client.create_batch(partition_id='0') should send the data to the intended partition.

Another way to do that is to pass in a kwarg in to send_batch like this client.send_batch(event_data_batch, partition_id='1').

Here is a sample that has more examples