使用 Python 在 Azure eventhub 中发送标头数据

我正在使用 Microsoft 提供的标准代码向 Azure 事件中心发送一条消息,它工作正常,但是,我还想为每条消息添加一个标头以了解有关该消息的其他详细信息。我找不到办法做到这一点,下面是我的尝试,但没有奏效。


client = EventHubClient(ADDRESS, username=USER, password=KEY, debug=True)

    sender = client.add_sender(partition="0", send_timeout=2000, keep_alive=500)

    client.run()

    try:

        nevent_data = EventData('Message with properties')

        nevent_data.properties = {'prop': 'prop1'}

        sender.send(nevent_data)

在这里,我试图以 dict 的形式发送属性和消息正文,下面是监听器。


听众:


client = EventHubClient(ADDRESS, debug=False, username=USER, password=KEY)

try:

    receiver = client.add_receiver(

        CONSUMER_GROUP, PARTITION, prefetch=5000, offset=OFFSET)

    client.run()

    start_time = time.time()

    while True:

        for event_data in receiver.receive(timeout=1):

            print(event_data.properties)

            print("Received: {}".format(event_data.body_as_str(encoding='UTF-8')))

当我尝试获取属性时,它出错了。


临摹微笑
浏览 105回答 1
1回答

梵蒂冈之花

只需更改上面的一个参数即可。改变nevent_data.properties = {'prop': 'prop1'}到nevent_data.application_properties = {'prop': 'prop1'}在接收器上做同样的事情。print(event_data.application_properties)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python