我有一个要求,比如我想从 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)的文档:
下面是一个简单的代码示例:
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
)
原始答案:
我认为下面的文档会对您有所帮助:
如何阅读:
如何写:
顺便说一下,不要使用 blob 的输出绑定(bind)。通过绑定(bind)可以实现读取,但不能写入。(Blob 存储服务和 Datalake 服务基于不同的对象。虽然使用 Blob 输入绑定(bind)来读取文件完全没问题,但请不要使用 Blob 输出绑定(bind)来写入文件,因为它会影响文件的大小。不创建基于Datalake Service的对象。)
让我知道上面的文档是否可以帮助你,如果没有,我将更新一个简单的 python 示例。
关于python - Azure 数据湖的 Azure 函数绑定(bind) (python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64527808/