简体   繁体   English

Azure 函数/事件中心和 Blob 输出绑定。 如何有效地保存消息(Python)

[英]Azure Functions / Events Hub and Blob ouput binding. How to save messages efficiently (Python)

I'm stuck with Azure Functions connected to Event Hub and I don't find that the documentation is very relevant for this.我坚持使用连接到事件中心的 Azure 函数,我发现文档与此无关。

If I take the small python example to make a loop for existing events in order to save them manually to a blob storage:如果我使用小型 python 示例为现有事件创建一个循环,以便手动将它们保存到 blob 存储:

from typing import List
import logging
import azure.functions as func


def main(events: List[func.EventHubEvent], outputBlob: func.Out[func.InputStream]):
    for event in events:
        logging.info('Python EventHub trigger processed an event: %s', event.get_body().decode('utf-8'))
        outputBlob.set(event.get_body().decode('utf-8')) # Save to storage.

and for the function.json:对于 function.json:

"bindings": [ ..., 
{
  "type": "blob",
  "direction": "out",
  "name": "outputBlob",
  "path": "outcontainer/{rand-guid}",
  "connection": "storage_STORAGE"
}

It works... but not correctly: if 3 messages are in the events, the first and the third are saved... but never the second.它可以工作...但不正确:如果事件中有 3 条消息,则保存第一条和第三条消息...但不会保存第二条。 Any way to improve this, and to save each event with a name depending of the content of the message, or something else by providing the filename depending of some value of the event and managed in the loop by example?有什么方法可以改进这一点,并根据消息的内容使用名称保存每个事件,或者通过根据事件的某些值提供文件名并通过示例在循环中进行管理来保存其他事件?

Thanks,谢谢,

Using storage output binding does cause such a problem, you can use storage SDK as an alternative solution, please refer to the following code:使用storage output binding确实会导致这样的问题,可以使用storage SDK作为替代方案,请参考以下代码:

Code:代码:

from typing import List
import logging, time
import azure.functions as func
import os, uuid
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
import tempfile


def main(events: List[func.EventHubEvent]):
    connect_str = '<your-storage-connection-string>'
    blob_service_client = BlobServiceClient.from_connection_string(connect_str)
    container_name = '<your-container-name>'
    container_client = blob_service_client.get_container_client(container_name)
    for event in events:
        logging.info('Python EventHub trigger processed an event: %s', event.get_body().decode('utf-8'))

        temp_path = tempfile.gettempdir()

        # Create a file in the local data directory to upload and download
        local_file_name = str(uuid.uuid4()) + ".txt"
        upload_file_path = os.path.join(temp_path, local_file_name)

        # Write text to the file
        file = open(upload_file_path, 'w')
        file.write(event.get_body().decode('utf-8'))
        file.close()

        # Create a blob client using the local file name as the name for the blob
        blob_client = blob_service_client.get_blob_client(container=container_name, blob=local_file_name)

        print("\nUploading to Azure Storage as blob:\n\t" + local_file_name)

        # Upload the created file
        with open(upload_file_path, "rb") as data:
            blob_client.upload_blob(data)

Function.json: Function.json:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "events",
      "direction": "in",
      "eventHubName": "frankhub",
      "connection": "frankeventhub_RootManageSharedAccessKey_EVENTHUB",
      "cardinality": "many",
      "consumerGroup": "$default",
      "dataType": "binary"
    }
  ]
}

You can refer to the following tutorial:您可以参考以下教程:

https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python

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

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