【问题标题】:How to read parquet file data partitioned on column from AWS S3 using python如何使用 python 从 AWS S3 读取在列上分区的镶木地板文件数据
【发布时间】:2022-02-10 17:52:47
【问题描述】:

我已使用 pyspark 将下表保存到 AWS S3,按列“channel_name”分区。使用下面的代码。

 df.write.option("header",True) \
                .partitionBy("channel_name") \
                .mode('append')\
                .parquet("s3://path") 

start_timestamp channel_name value
2020-11-02 08:51:50 velocity 1
2020-11-02 09:14:29 Temp 0
2020-11-02 09:18:32 velocity 0
2020-11-02 09:32:42 velocity 4
2020-11-03 13:06:03 Temp 2
2020-11-03 13:10:01 Temp 1
2020-11-03 13:54:38 Temp 5
2020-11-03 14:46:25 velocity 5
2020-11-03 14:57:31 Kilometer 6
2020-11-03 15:07:07 Kilometer 7

但我想使用 python 读取在“channel_name”列上分区的相同数据,它不工作,它不包括分区列“channel_name”。下面是我尝试使用 AWSwrangler 的代码。

import awswrangler as wr
df = wr.s3.read_parquet(path="s3://shreyasbigdata/Prod_test_item_id=V214944/")

看起来像这样,但我也想要那个“channel_name”列。

start_timestamp value
2020-11-02 08:51:50 1
2020-11-02 09:14:29 0
2020-11-02 09:18:32 0
2020-11-02 09:32:42 4
2020-11-03 13:06:03 2
2020-11-03 13:10:01 1
2020-11-03 13:54:38 5
2020-11-03 14:46:25 5
2020-11-03 14:57:31 6
2020-11-03 15:07:07 7

我尝试了不同的库,但它不起作用。 如果您能帮我阅读所有列,包括分区列,那就太好了。

【问题讨论】:

    标签: python amazon-s3 parquet


    【解决方案1】:

    我得到了答案,谢谢

    import s3fs
    import pyarrow.parquet as pq
    fs = s3fs.S3FileSystem()
    
    bucket = 'bucket_name'
    path = 'path_of_folder' #if its a directory omit the traling /
    bucket_uri = f's3://{bucket}/{path}'
    
    dataset = pq.ParquetDataset(bucket_uri, filesystem=fs)
    table = dataset.read()
    df = table.to_pandas() 
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    • 在以下链接中找到了这个答案:stackoverflow.com/questions/45082832/…
    猜你喜欢
    • 2019-10-27
    • 2017-12-18
    • 2019-12-07
    • 2019-12-12
    • 2021-08-26
    • 1970-01-01
    • 2021-12-28
    • 2022-01-12
    • 1970-01-01
    相关资源
    最近更新 更多