【问题标题】:How to send Properties details in EventHub message using python?如何使用 python 在 EventHub 消息中发送属性详细信息?
【发布时间】:2019-02-22 04:19:30
【问题描述】:

我指的是这篇文章“https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-python-get-started-send”与使用 Python 向 EventHub 发送消息有关。

消息具有以下组件:偏移量、正文、系统属性、属性。 偏移量是自动生成的,但我们可以提供另一个。

对于我的项目 - 除了消息正文之外,我还需要发送不属于正文的"Properties",该怎么做?我检查了EventData(object): 类,看起来可以使用 application_properties 来执行此操作,但我不确定如何实现。

application_properties 是否可以类比获取消息中的“属性”?

您能否在文章中添加更多详细信息,并举例说明如何使用 python EventData 类发送除属性和 SystemProperties 等消息正文之外的详细信息。 截至目前,属性以空列表的形式发送。

我需要使用 Python 发送到 EventHub 的示例消息格式:

Sender.send(EventData('{"**offset**":"2415248","**body**":"TESTone:100, 
Temperature:553.0","**systemProperties**":[{"key":{"string":"x-opt- 
sequence-number"},"value":{"string":"23512"}},{"key":{"string":"x-opt- 
offset"},"value":{"string":"2415248"}},{"key":{"string":"x-opt- 
enqueued-time"},"value":{"string":"Fri Feb 22 02:14:23 UTC 
2019"}}],"**properties**":[]}'))

我想在 properties 中发送这个 dict 值:{"key":{"string":"Type"},"value":{"string":"iPhone"}}

【问题讨论】:

    标签: python azure-iot-hub azure-eventhub


    【解决方案1】:

    找到了解决方案,是的,我们可以使用“application_properties”来发送消息的“属性”。 这是我的示例代码:

    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)
    

    【讨论】:

      【解决方案2】:

      我在其他地方问过这个问题,因为 PraveenS 提供的解决方案实际上并没有解决问题。我收到的正确答案如下,分享给大家:

      解决方案是将要与消息一起发送的属性分配给 EventData.properties,将其应用于上述代码将如下所示:

      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)
      

      【讨论】:

        【解决方案3】:

        您可能还会发现以下方式很有用,基本上是一样的:

        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:需要安装this库。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-10-30
          • 1970-01-01
          • 2021-12-03
          • 2019-05-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多