How to send Properties details in EventHub message using python?

后端 未结 3 361
温柔的废话
温柔的废话 2021-01-24 17:20

I am referring to this article \"https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-python-get-started-send\" related to sending messages to EventHub using Python.

相关标签:
3条回答
  • 2021-01-24 18:01

    I asked this question elsewhere as the solution provided by PraveenS does not actually solve the problem. The correct answer which I received is below, sharing to help others:

    The solution is to assign the properties you want to be sent with the message to EventData.properties, applying that to the above code would look like this:

    from azure.eventhub import EventHubClient, Receiver, Offset, Sender, EventData
    from uamqp import Message
    
    ADDRESS = "amqps://<>.windows.net/<>"
    USER = "RootManageSharedAccessKey"
    KEY = "<>"
    
    client = EventHubClient(ADDRESS, debug=True, username=USER, password=KEY)
    Sender = client.add_sender(partition="0")
    client.run()
    
    event = EventData(body="TESTTWO:100, Temperature:-127.0")
    ### THIS IS WHERE THE CHANGE IS ###
    event.properties = {"Type": "iPhone"}
    Sender.send(event)
    
    0 讨论(0)
  • 2021-01-24 18:18

    You may also find useful the following way, which basically do the same:

    props = {"Type": "iPhone"} # properties you want to send
    
    columns = ['body', 'properties']
    values = [(write_binneddata, props)] # You can also send multiple messages adding tuples to the list
    df = spark.createDataFrame(values, columns)
    
    conn_string = f"Endpoint=sb://{event_hub_namespace}.servicebus.windows.net/;SharedAccessKeyName={shared_acc_keyname};SharedAccessKey={shared_acc_key};EntityPath={event_hub_name}"
    conf = { 'eventhubs.connectionString' : conn_string }
    
    ds = (
      df
      .write 
      .format("eventhubs") 
      .options(**conf) 
      .option("checkpointLocation", f"{checkpoint_path}")
      .save()
    )
    

    PS: it needs this library installed.

    0 讨论(0)
  • 2021-01-24 18:22

    Found solution to this, yes we can use "application_properties" to send "properties" of message. This is my sample code that worked:

    from azure.eventhub import EventHubClient, Receiver, Offset, Sender, EventData
    from uamqp import Message
    
    ADDRESS = "amqps://<>.windows.net/<>"
    USER = "RootManageSharedAccessKey"
    KEY = "<>"
    
    client = EventHubClient(ADDRESS, debug=True, username=USER, password=KEY)
    Sender = client.add_sender(partition="0")
    client.run()
    
    event = EventData(body="TESTTWO:100, Temperature:-127.0")
    event.application_properties = {"Type": "iPhone"}
    Sender.send(event)
    
    0 讨论(0)
提交回复
热议问题