【问题标题】:How to read parquet files from aws s3 bucket and save them as jsons in jupyter如何从 aws s3 存储桶中读取镶木地板文件并将它们保存为 jupyter 中的 jsons
【发布时间】:2020-07-16 04:16:17
【问题描述】:

我正在使用带有 python 的 jupyter notebook 工作。我正在尝试读取 aws s3 存储桶中文件夹中的所有镶木地板文件,并将它们作为 jsons 保存在我的 jupyter 目录中的文件夹中。我有以下代码,但我相信它只是在阅读它们,我想将它们保存为 jsons。谢谢!

bucketname = 'my-bucket'
bucket = response.Bucket(bucketname)
for obj in bucket.objects.all():
    key = obj.key
    body = obj.get()['Body'].read()

【问题讨论】:

    标签: amazon-s3 multi-factor-authentication


    【解决方案1】:

    如果我正确理解您的问题,您希望将文件下载到文件系统而不是加载到内存中。这是一个完成这项工作的示例代码 sn-p。

    bucketname = 'my-bucket'
    bucket = response.Bucket(bucketname)
    for obj in bucket.objects.all():
        obj.Object().download_file('<specify-the-local-filename>')
    

    您可以找到文档here

    【讨论】:

    • 谢谢!有没有办法将它下载为 json 文件而不是像现在这样的镶木地板?
    • 这给了我's3.ObjectSummary'对象没有属性'download_file'',你知道为什么会这样吗?
    • @supercool 对不起,我已经更新了答案。
    【解决方案2】:

    parquet pip 模块可以做到这一点:https://pypi.org/project/parquet/。他们也有一个例子,在这里复制以供快速参考:

    import parquet
    import json
    
    ## assuming parquet file with two rows and three columns:
    ## foo bar baz
    ## 1   2   3
    ## 4   5   6
    
    with open("test.parquet") as fo:
       # prints:
       # {"foo": 1, "bar": 2}
       # {"foo": 4, "bar": 5}
       for row in parquet.DictReader(fo, columns=['foo', 'bar']):
           print(json.dumps(row))
    

    【讨论】:

    • 谢谢!有没有办法保存这些 json,并且对每个文件都这样做?
    猜你喜欢
    • 1970-01-01
    • 2022-11-06
    • 1970-01-01
    • 2018-05-14
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    相关资源
    最近更新 更多