Azure data lake的Azure函数绑定(python)

2024-05-19 22:10:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从Azure函数连接到Azure data lake v2(ADL),读取文件,使用python(pyspark)处理,然后在Azure data lake中再次写入。因此,我的输入和输出绑定将是ADL。python中是否有Azure函数的ADLS绑定可用?有人能给我一些建议吗

谢谢,, 天线D


Tags: 文件函数dataazure建议pysparkv2天线
1条回答
网友
1楼 · 发布于 2024-05-19 22:10:46

更新:

1,当我们读取数据时,我们可以使用blob输入绑定

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

这是azure功能可以支持哪种绑定的文档:

https://docs.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://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-input?tabs=csharp

如何写作:

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

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

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

相关问题 更多 >