簡體   English   中英

如何使用 pyarrow 從 s3 讀取分區的鑲木地板文件?

[英]How do I read partitioned parquet files from s3 using pyarrow?

我正在嘗試讀取存儲在 s3 存儲桶中的分區鑲木地板目錄。

為了這個問題,我們將桶稱為bucket 該存儲桶有一個文件夾,該文件夾具有基於年/月/日/小時的后續分區。

因此,如果要訪問 .parquet 文件,則 url 將是:

s3://bucket/folder/year/month/day/hour

我試着像閱讀任何其他鑲木地板文件一樣閱讀它。 我最近一直在和他們一起工作。 但是,到目前為止我還沒有嘗試讀取分區文件。

我在下面包含了我的示例代碼:

import s3fs
import pandas as pd
import boto3

# Creating an S3 Filesystem (Only required when using S3)

s3 = s3fs.S3FileSystem()
s3_path = "s3://bucket"
directory = 'folder'

# Loading Files (S3)

data = pq.ParquetDataset(f'{s3_path}/{directory}', filesystem = s3).read_pandas().to_pandas()

這是我使用過的流程,我知道它適用於一般的鑲木地板文件。 現在,我得到的錯誤是這樣的:

ValueError: Directory name did not appear to be a partition: 2019

我已經嘗試進入2019因為我認為第一級只有2019作為文件夾,所以它可能認為它是一個子目錄而不是分區。

路徑看起來像s3://bucket/folder/2019

但是,這給了我以下錯誤,沿着類似的路線:

ValueError: Directory name did not appear to be a partition: 05

我還嘗試fastparquet以下問題的方法使用fastparquetHow to read partitioned parquet files from S3 using pyarrow in python

那也沒有用。 如果我嘗試使用上述問題的答案中的all_paths_from_s3打印文件列表,它會給我一個空白列表[]

發生這種情況是因為分區路徑應如下所示:

s3://bucket/folder/year=2019/month=05/day=01

如果您使用Kinesis Firehose將數據保存到 S3 存儲桶(例如),您可以使用前綴選項來覆蓋默認的 AWS年/月/日/小時格式。

暫無
暫無

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

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