简体   繁体   English

使用 Python 使用来自 Azure 事件中心的消息?

[英]Consuming messages from an Azure Event Hub using Python?

I can't find any documentation online using Python to subscribe and consume messages from an Azure Event Hub.我无法在线找到任何使用 Python 订阅和使用来自 Azure 事件中心的消息的文档。 I know it's possible in C, C#, and Java.我知道在 C、C# 和 Java 中是可能的。 I just need to know if it is possible to use Python.我只需要知道是否可以使用 Python。

The Azure python SDK currently seems to only support sending messages, but not opening up an Async connection to constantly receive messages from the Event Hub. Azure python SDK 目前似乎只支持发送消息,但不开放异步连接来不断地从事件中心接收消息。 http://azure-sdk-for-python.readthedocs.org/en/latest/servicebus.html#event-hub http://azure-sdk-for-python.readthedocs.org/en/latest/servicebus.html#event-hub

The only way I've found to connect to EventHubs from python is to use the python-qpid-proton library/pypi module.我发现从 python 连接到 EventHubs 的唯一方法是使用 python-qpid-proton 库/pypi 模块。

This is because eventhubs use amqp 1.0 + TLS so most of the other libraries you'll find won't work (they implement <= amqp 0.9).这是因为 eventhub 使用 amqp 1.0 + TLS,所以您会发现大多数其他库都不起作用(它们实现 <= amqp 0.9)。

I'm still hoping to find a solution that's easier to use with python on windows, but that should work on OS X and Linux boxes just fine.我仍然希望找到一个更容易在 Windows 上与 python 一起使用的解决方案,但这应该适用于 OS X 和 Linux 机器。

Without knowing the detailed requirements of your dashboard, we can only give you some high level advice.在不了解您的仪表盘的详细要求的情况下,我们只能给您一些高水平的建议。 In a nut shell, you can think your dashboard as a consumer group (or multiple consumer groups if there're multiple partitions, as each group can only connect to a single partition).简而言之,您可以将仪表板视为一个消费者组(如果有多个分区,则为多个消费者组,因为每个组只能连接到一个分区)。 You just need to use AMQP to connect to the event hub.您只需要使用 AMQP 连接到事件中心。 In Python you can use an AMPQ library such as https://pypi.python.org/pypi/amqp .在 Python 中,您可以使用 AMPQ 库,例如https://pypi.python.org/pypi/amqp Don't worry, after reading the events in your dashboard, the events will not be deleted, so they will continue to be available to other consumer groups.别担心,在您阅读仪表板中的事件后,这些事件不会被删除,因此它们将继续提供给其他消费群体。 AMPQ is a standard. AMPQ 是一个标准。 So you just need to provide the library the event hub's address, authentication info, and then you can connect to event hub.因此,您只需要向库提供事件中心的地址、身份验证信息,然后就可以连接到事件中心。

It might be too late for your project but azure-eventhub Python SDK is available now providing the functionality to send/receive events to/from the Event Hub service.对于您的项目来说可能为时已晚,但 azure-eventhub Python SDK 现在可用,提供向/从事件中心服务发送/接收事件的功能。

I'll post the information here which would help users looking for the SDK at a later time.我将在此处发布信息,以帮助用户稍后查找 SDK。

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

There is also a migration guide from v1 to v5 for those who are using v1 sdk to smoothly migrate the program to v5.还有一个从 v1 到 v5迁移指南,适用于使用 v1 sdk 将程序顺利迁移到 v5 的人。

To consume messages from Event Hub, please follow the 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.')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM