【问题标题】:formatting AWS glue output to JSON OBJECT将 AWS 胶水输出格式化为 JSON OBJECT
【发布时间】:2019-07-28 10:56:23
【问题描述】:

这是我在 AWS GLUE 中的 pyspark 作业得到的结果

{a:1,b:7}
{a:1,b:9}
{a:1,b:3}

但我需要在 s3 上写入这些数据并将其发送到 JSON 数组中的 API 格式

[
 {a:1,b:2}, 
 {a:1,b:7}, 
 {a:1,b:9}, 
 {a:1,b:3}
]

我尝试将我的输出转换为 DataFrame,然后应用 toJSON() results = mapped_dyF.toDF() jsonResults = results.toJSON().collect()

但现在无法使用'write_dynamic_frame.from_options' 在 s3 上写回结果 因为它需要 DF,但 my'jsonResults' 现在不再是 DataFrame。

【问题讨论】:

  • 如果您将条目弹出到数组列中,这可能会起作用,而不是将其转换为 json

标签: pyspark aws-glue


【解决方案1】:

为了将其放入 JSON 数组格式,我通常会执行以下操作: df --> 包含原始数据的DataFrame。

if df.count() > 0:
    # Build the json file
    data = list()
    for row in df.collect():
        data.append({"a": row['a'],
                     "b" : row['b']
                    })

在这种情况下我没有使用胶水write_dynamic_frame.from_options,但我使用boto3 来保存文件:

import boto3
import json

s3 = boto3.resource('s3')
# Dump the json file to s3 bucket  
filename = '/{0}_batch_{1}.json'.format(str(uuid.uuid4()))
obj = s3.Object(bucket_name, filename)
obj.put(Body=json.dumps(data))

【讨论】:

    猜你喜欢
    • 2021-07-26
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    相关资源
    最近更新 更多