如何从Azure Python函数BLOB输入绑定中读取拼图文件?

问题描述

我有一个带有BLOB输入绑定的python函数。有问题的斑点包含镶木地板文件。最终,我希望将绑定的Blob读取到 pandas 数据帧中,但我不确定执行此操作的正确方法。

我已经验证了绑定设置正确,并且能够成功读取纯文本文件。我很高兴拼花文件的完整性是好的,因为我已经能够使用这里提供的示例来阅读它:https://arrow.apache.org/docs/python/parquet.html#reading-a-parquet-file-from-azure-blob-storage

以下代码显示了我正在尝试执行的操作:


import logging
import io
import azure.functions as func
import pyarrow.parquet as pq


def main(req: func.HttpRequest,  inputblob: func.InputStream) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    # Create a bytestream to hold blob content
    byte_stream = io.BytesIO()
    byte_stream.write(inputblob.read())
    df = pq.read_table(source=byte_stream).to_pandas()

我收到以下错误消息:

pyarrow.lib.ArrowIOError: Couldn't deserialize thrift: TProtocolException: Invalid data

以下是我的函数.json文件:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    },
    {
        "name": "inputblob",
        "type": "blob",
        "path": "<container>/file.parquet",
        "connection": "AzureWebJobsStorage",
        "direction": "in"
    }
  ]
}

我的host.json文件:

{
    "version":  "2.0",
    "functionTimeout": "00:10:00",
    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[1.*, 2.0.0)"
    }
}

解决方案

我一直在解决相同的问题,此解决方案对我有效。

__ini_.py文件:

from io import BytesIO
import azure.functions as func

def main(blobTrigger: func.InputStream):

    # Read the blob as bytes
    blob_bytes = blobTrigger.read()
    blob_to_read = BytesIO(blob_bytes)
    df = pd.read_parquet(blob_to_read, engine='pyarrow')
    print("Length of the parquet file:" + str(len(df.index)))

相关文章