Azure EventHub,无法订阅接收

时间:2016-02-16 19:02:51

标签: python azure amqp qpid azure-eventhub

我尝试通过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]

name / key应该没问题,因为它可以在另一个应用程序中运行。

任何猜测?

2 个答案:

答案 0 :(得分:1)

另一种选择是使用最新的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.')

答案 1 :(得分:0)

看起来您的密钥可能包含" /"因此,您可能想要访问azure portal,看看是否可以使用辅助密钥。您可能必须创建一个新的共享访问策略"

key shared access policy