gpt4 book ai didi

python - Azure EventHub,无法订阅接收

转载 作者:行者123 更新时间:2023-12-03 00:13:44 25 4
gpt4 key购买 nike

我尝试通过 Python 从 Azure EventHub 接收消息,遗憾的是我无法订阅它。

我的脚本基于 https://gist.github.com/tomconte/e2a4667185a9bf674f59另一个类似的问题已经在 python script which subscribes/listens to Azure Event Hub? 中提出。 ,不幸的是没有解决它。

我的设置:Python 2.7.9(Ubuntu 15.04)

通过 pip 安装 qpid-proton:

pip show python-qpid-proton
...
Version: 0.11.1
...

所以我正在尝试以下操作:

from proton import *
import urllib
key = urllib.quote(FOOBAR,"")
address = "amqps://name:" + key + "@nsname.servicebus.windows.net/eventhubname/ConsumerGroups/$Default/Partitions/0"
messenger = Messenger()
messenger.subscribe(address)

proton.MessengerException: Cannot subscribe to [ADDRESS]

名称/ key 应该没问题,因为它在另一个应用程序中工作。

有什么猜测吗?

最佳答案

另一种选择是使用最新的 azure-eventhub Python SDK 从事件中心接收消息。

azure-eventhub 可在 pypi 上使用:https://pypi.org/project/azure-eventhub/

您可以关注receive sample code接收消息:

#!/usr/bin/env python

# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

"""
An example to show receiving events from an Event Hub.
"""
import os
from azure.eventhub import EventHubConsumerClient

CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"]
EVENTHUB_NAME = os.environ['EVENT_HUB_NAME']


def on_event(partition_context, event):
# Put your code here.
# If the operation is i/o intensive, multi-thread will have better performance.
print("Received event from partition: {}.".format(partition_context.partition_id))


def on_partition_initialize(partition_context):
# Put your code here.
print("Partition: {} has been initialized.".format(partition_context.partition_id))


def on_partition_close(partition_context, reason):
# Put your code here.
print("Partition: {} has been closed, reason for closing: {}.".format(
partition_context.partition_id,
reason
))


def on_error(partition_context, error):
# Put your code here. partition_context can be None in the on_error callback.
if partition_context:
print("An exception: {} occurred during receiving from Partition: {}.".format(
partition_context.partition_id,
error
))
else:
print("An exception: {} occurred during the load balance process.".format(error))


if __name__ == '__main__':
consumer_client = EventHubConsumerClient.from_connection_string(
conn_str=CONNECTION_STR,
consumer_group='$Default',
eventhub_name=EVENTHUB_NAME,
)

try:
with consumer_client:
consumer_client.receive(
on_event=on_event,
on_partition_initialize=on_partition_initialize,
on_partition_close=on_partition_close,
on_error=on_error,
starting_position="-1", # "-1" is from the beginning of the partition.
)
except KeyboardInterrupt:
print('Stopped receiving.')

关于python - Azure EventHub,无法订阅接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35440915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com