簡體   English   中英

使用 Python 將 DataFrame 轉換為 Azure Functionapp 中的 Parquet

[英]Convert DataFrame to Parquet in Azure Functionapp using Python

我正在從我的 Azure 數據湖存儲(第 2 代)下載 2 個 csv 文件。 然后將它們合並在一起並以 parquet 格式將其上傳到同一個存儲帳戶,但到不同的文件夾。 我想使用 VS Code 中的 FuctionApp 以鑲木地板格式將摘要 dataframe 上傳到我的存儲帳戶。 代碼在本地完美運行,但 functionapp 給了我“500-internal server error”。 我用於 to_parquet 方法的 Pyarrow 引擎存在問題。Azure 似乎不支持該引擎。

    import pandas as pd
    from azure.storage.filedatalake import DataLakeServiceClient
    import azure.functions as func
    from io import StringIO

def main(req: func.HttpRequest) -> func.HttpResponse:
    
    STORAGEACCOUNTURL= 'https://storage_acc_name.dfs.core.windows.net/'
    STORAGEACCOUNTKEY= 'Key'
    LOCALFILENAME= ['file1', 'file2']

    file1 = pd.DataFrame()
    file2 = pd.DataFrame()
   

    service_client = DataLakeServiceClient(account_url=STORAGEACCOUNTURL, credential=STORAGEACCOUNTKEY)
    adl_client_instance = service_client.get_file_system_client(file_system="raw")
    
    directory_client = adl_client_instance.get_directory_client("raw")

    for i in LOCALFILENAME:
        if i == 'file1.csv':
            file_client = adl_client_instance.get_file_client(i)
            adl_data = file_client.download_file()
            byte1 = adl_data.readall()
            s=str(byte1,'utf-8')
            file1 = pd.read_csv(StringIO(s))
            
        if i == 'file2.csv':
            file_client = adl_client_instance.get_file_client(i)
            adl_data = file_client.download_file()
            byte2 = adl_data.readall()
            s=str(byte2,'utf-8')
            file2 = pd.read_csv(StringIO(s))
    
    
    summary = pd.merge(left=file1, right=file2, on='key', how='inner')

    service_client = DataLakeServiceClient(account_url=STORAGEACCOUNTURL, credential=STORAGEACCOUNTKEY)
    file_system_client = service_client.get_file_system_client(file_system="output")
    directory_client = file_system_client.get_directory_client("output") 
    file_client = directory_client.create_file("output.parquet") 
    file_contents = pd.DataFrame(summary).to_parquet()
    file_client.append_data(data=file_contents, offset=0, length=len(file_contents)) 
    
    file_client.flush_data(len(file_contents))

    return("This HTTP triggered function executed successfully.")

if __name__ == '__main__':
    main("name")

也許你可以使用 pyspark

df_MF=spark_session.createDataFrame(df)
# now you get spark df,you can save it use spark save it

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM