【发布时间】:2021-12-24 15:48:06
【问题描述】:
我正在尝试在管道中的 Azure 数据工厂中运行我的以下脚本。我的 Python 代码从 Blob 存储中检索 2 个 CSV 文件,并根据密钥将它们合并到一个文件中,然后将其上传到数据湖存储中。我尝试过使用功能应用程序块,它给了我 InternalServerError,我还尝试了运行没有错误的 Web 活动。问题是我运行管道时没有创建文件,即使管道成功运行(使用 Web 块)。当我调用主函数并且在数据湖存储中创建文件时,该函数也会在本地运行。我在 VS Code 中也尝试过 http 触发器和持久函数,但它们都没有在 Azure 中创建“merged.csv”文件。
我的 Python 脚本(init.py):
import pandas as pd
import logging
from azure.storage.blob import BlobServiceClient
from azure.storage.filedatalake import DataLakeServiceClient
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
STORAGEACCOUNTURL= 'https://storage.blob.core.windows.net/'
STORAGEACCOUNTKEY= '****'
LOCALFILENAME= ['file1.csv', 'file2.csv']
CONTAINERNAME= 'inputblob'
file1 = pd.DataFrame()
file2 = pd.DataFrame()
#download from blob
blob_service_client_instance = BlobServiceClient(account_url=STORAGEACCOUNTURL, credential=STORAGEACCOUNTKEY)
for i in LOCALFILENAME:
with open(i, "wb") as my_blobs:
blob_client_instance = blob_service_client_instance.get_blob_client(container=CONTAINERNAME, blob=i, snapshot=None)
blob_data = blob_client_instance.download_blob()
blob_data.readinto(my_blobs)
if i == 'file1.csv':
file1 = pd.read_csv(i)
if i == 'file2.csv':
file2 = pd.read_csv(i)
# load
summary = pd.merge(left=file1, right=file2, on='key', how='inner')
summary.to_csv()
global service_client
service_client = DataLakeServiceClient(account_url="https://storage.dfs.core.windows.net/", credential='****')
file_system_client = service_client.get_file_system_client(file_system="outputdatalake")
directory_client = file_system_client.get_directory_client("functionapp")
file_client = directory_client.create_file("merged.csv")
file_contents = summary.to_csv()
file_client.upload_data(file_contents, overwrite=True)
return("This HTTP triggered function executed successfully.")
我的 JSON 文件(function.json):
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
【问题讨论】:
-
既然你说它在本地运行,那么当它在 azure 上运行时存在一些权限或配置问题,请尝试在代码中添加
try...except块以记录正确的错误消息。 -
您可以查看该功能的日志/应用洞察,看看您的代码/访问存储帐户是否有任何错误。您甚至可以仅使用数据工厂来合并 CSV 文件,例如 docs.microsoft.com/en-us/answers/questions/542994/…
标签: python azure visual-studio-code azure-functions azure-data-factory