python - Azure 数据湖的 Azure 函数绑定(bind) (python)

标签 python azure azure-functions azure-data-lake

我有一个要求,比如我想从 Azure 函数连接到我的 Azure 数据湖 v2(ADLS)、读取文件、使用 python(pyspark) 处理它并在 Azure 数据湖中再次写入。所以我的输入和输出绑定(bind)将是 ADLS。 python 中的 Azure 函数有可用的 ADLS 绑定(bind)吗?有人可以对此提出任何建议吗?

谢谢, 安腾D

最佳答案

更新:

1、当我们读取数据时,我们可以使用blob输入绑定(bind)。

2,但是当我们写入数据时,我们不能使用blob输出绑定(bind)。(这是因为对象不同。)并且azure函数不支持ADLS输出绑定(bind),所以我们需要将逻辑代码放在body中当我们要编写代码时的函数。

这是有关 azure 函数可以支持哪种绑定(bind)的文档:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings?tabs=csharp#supported-bindings

下面是一个简单的代码示例:

import logging

import azure.functions as func
from azure.storage.filedatalake import DataLakeServiceClient

def main(req: func.HttpRequest, inputblob: func.InputStream) -> func.HttpResponse:
    connect_str = "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx;EndpointSuffix=core.windows.net"
    datalake_service_client = DataLakeServiceClient.from_connection_string(connect_str)
    myfilesystem = "test"
    myfile       = "FileName.txt"
    file_system_client = datalake_service_client.get_file_system_client(myfilesystem)    
    file_client = file_system_client.create_file(myfile)
    inputstr = inputblob.read().decode("utf-8")
    print("length of data is "+str(len(inputstr)))
    filesize_previous = 0
    print("length of currentfile is "+str(filesize_previous))
    file_client.append_data(inputstr, offset=filesize_previous, length=len(inputstr))
    file_client.flush_data(filesize_previous+len(inputstr))
    return func.HttpResponse(
            "This is a test."+inputstr,
            status_code=200
    )

原始答案:

我认为下面的文档会对您有所帮助:

如何阅读:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-input?tabs=csharp

如何写:

https://learn.microsoft.com/en-us/python/api/azure-storage-file-datalake/azure.storage.filedatalake.datalakeserviceclient?view=azure-python

顺便说一下,不要使用 blob 的输出绑定(bind)。通过绑定(bind)可以实现读取,但不能写入。(Blob 存储服务和 Datalake 服务基于不同的对象。虽然使用 Blob 输入绑定(bind)来读取文件完全没问题,但请不要使用 Blob 输出绑定(bind)来写入文件,因为它会影响文件的大小。不创建基于Datalake Service的对象。)

让我知道上面的文档是否可以帮助你,如果没有,我将更新一个简单的 python 示例。

关于python - Azure 数据湖的 Azure 函数绑定(bind) (python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64527808/

相关文章:

java - Azure Spring Boot 功能 - 异常 : UnsupportedOperationException: At the moment only Tuple-based function are supporting multiple arguments

Azure 二头肌嵌套循环

typescript - 为azure函数api( typescript )创建文档swagger

azure - 如何使用 Azure DevOps 管道变量配置 Azure Function?

Python3 正则表达式 findall

python - CSV 写入以注释行开头的文件

python - 删除中间带有大写字母的单词

python - 如何在 PostgreSQL 中将整数解压缩为 float ?

powershell - 使用 Azure Powershell 函数列出我的所有 Azure 网站

Azure 函数成本 - 函数触发的操作是否也算作函数执行时间?