簡體   English   中英

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

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

我無法在線找到任何使用 Python 訂閱和使用來自 Azure 事件中心的消息的文檔。 我知道在 C、C# 和 Java 中是可能的。 我只需要知道是否可以使用 Python。

Azure python SDK 目前似乎只支持發送消息,但不開放異步連接來不斷地從事件中心接收消息。 http://azure-sdk-for-python.readthedocs.org/en/latest/servicebus.html#event-hub

我發現從 python 連接到 EventHubs 的唯一方法是使用 python-qpid-proton 庫/pypi 模塊。

這是因為 eventhub 使用 amqp 1.0 + TLS,所以您會發現大多數其他庫都不起作用(它們實現 <= amqp 0.9)。

我仍然希望找到一個更容易在 Windows 上與 python 一起使用的解決方案,但這應該適用於 OS X 和 Linux 機器。

在不了解您的儀表盤的詳細要求的情況下,我們只能給您一些高水平的建議。 簡而言之,您可以將儀表板視為一個消費者組(如果有多個分區,則為多個消費者組,因為每個組只能連接到一個分區)。 您只需要使用 AMQP 連接到事件中心。 在 Python 中,您可以使用 AMPQ 庫,例如https://pypi.python.org/pypi/amqp 別擔心,在您閱讀儀表板中的事件后,這些事件不會被刪除,因此它們將繼續提供給其他消費群體。 AMPQ 是一個標准。 因此,您只需要向庫提供事件中心的地址、身份驗證信息,然后就可以連接到事件中心。

對於您的項目來說可能為時已晚,但 azure-eventhub Python SDK 現在可用,提供向/從事件中心服務發送/接收事件的功能。

我將在此處發布信息,以幫助用戶稍后查找 SDK。

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

還有一個從 v1 到 v5遷移指南,適用於使用 v1 sdk 將程序順利遷移到 v5 的人。

要使用來自事件中心的消息,請遵循示例代碼

#!/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