簡體   English   中英

上傳到 Azure Data Lake gen 2 后 Parquet 文件不可讀(Python)

[英]Parquet file after upload to Azure Data Lake gen 2 not readable (Python)

你好 stackoverflow 社區,

我在讀取鑲木地板文件時遇到了一些問題。 在我使用 Python 將 Parquet 文件上傳到 Azure Data Lake gen 2 后,問題就開始了。

我正在使用官方的 Micorsoft 文檔: https://docs.microsoft.com/en-us/azure/storage/blobs/data-lake-storage-directory-file-acl-python

除了認證,這部分:

def upload_file_to_directory():
try:



    file_system_client = service_client.get_file_system_client(file_system="my-file-system")



    directory_client = file_system_client.get_directory_client("my-directory")
    
    file_client = directory_client.create_file("uploaded-file.txt")
    local_file = open("C:\\file-to-upload.txt",'r')



    file_contents = local_file.read()



    file_client.append_data(data=file_contents, offset=0, length=len(file_contents))



    file_client.flush_data(len(file_contents))



except Exception as e:
  print(e)

當我使用代碼上傳一個小的 csv 文件時,它工作得很好。 csv 文件已上傳,當我下載文件時,我可以毫無問題地打開它。

如果我將相同的數據框轉換為一個小的鑲木地板文件並上傳文件,則上傳工作正常。 但是當我下載文件並嘗試打開它時,我收到錯誤消息:

ArrowInvalid:在頁腳中找不到 Parquet 魔術字節。 文件已損壞,或者這不是鑲木地板文件。

如果我直接讀取 Parquet 字段而不上傳,它可以正常工作。

有沒有人建議我如何修改代碼,這樣我就不會破壞我的鑲木地板文件?

謝謝!

我不確定你的代碼有什么問題(你的代碼似乎不完整),你可以試試這個代碼,它對我有用:

try:
    file_system_client = service_client.get_file_system_client(file_system="my-file-system")

    directory_client = file_system_client.get_directory_client("my-directory")

    file_client = directory_client.create_file("data.parquet")

    df = pd.DataFrame({'one': [-1, np.nan, 2.5],
                       'two': ['foo', 'bar', 'baz'],
                       'three': [True, False, True]},
                      index=list('abc')).to_parquet()

    file_client.append_data(data=df, offset=0, length=len(df))

    file_client.flush_data(len(df))

except Exception as e:
    print(e)

我今天剛剛在我的項目中解決了這個錯誤。

我正在使用pyarrow.parquet.write_table來編寫我的 Parquet 文件。

我將本機 Python 文件 object 傳遞給where參數,這不知何故導致頁腳永遠不會被寫入。

當我切換到使用PyArrow output 流而不是本機 Python 文件對象時,頁腳在 stream 上正確寫入,這為我解決了這個問題。

暫無
暫無

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

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