使用Python从Azure事件中心使用消息?

时间:2015-06-08 13:39:00

标签: python django azure azure-eventhub

我无法使用Python在线查找任何文档来订阅和使用来自Azure Event Hub的邮件。我知道在C,C#和Java中它是可能的。我只需要知道是否可以使用Python。

Azure python SDK目前似乎只支持发送消息,但不会打开Async连接以不断接收来自Event Hub的消息。 http://azure-sdk-for-python.readthedocs.org/en/latest/servicebus.html#event-hub

3 个答案:

答案 0 :(得分:3)

我发现从python连接到EventHubs的唯一方法是使用python-qpid-proton library / pypi模块。

这是因为eventhubs使用amqp 1.0 + TLS,因此您找到的大多数其他库都不起作用(它们实现< = amqp 0.9)。

我仍然希望找到一个更容易在Windows上使用python的解决方案,但这应该适用于OS X和Linux盒子。

答案 1 :(得分:2)

在不了解仪表板的详细要求的情况下,我们只能为您提供一些高级建议。在坚果壳中,您可以将仪表板视为消费者组(如果存在多个分区,则将多个消费者组视为一个,因为每个组只能连接到一个分区)。您只需使用AMQP连接到事件中心。在Python中,您可以使用AMPQ库,例如https://pypi.python.org/pypi/amqp。不用担心,在阅读仪表板中的事件后,事件将不会被删除,因此它们将继续可供其他消费者群体使用。 AMPQ是一个标准。所以你只需要为库提供事件中心的地址,身份验证信息,然后就可以连接到事件中心了。

答案 2 :(得分:1)

对于您的项目来说可能为时已晚,但是azure-eventhub Python SDK现在可用,它提供了向/从Event Hub服务发送/接收事件的功能。

我将在此处发布信息,这将有助于用户以后查找SDK。

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

对于使用v1 sdk将程序顺利迁移到v5的用户来说,还有一个migration guide from v1 to v5

要使用来自事件中心的消息,请遵循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.')
相关问题