pyspark - 将 AWS 胶水输出格式化为 JSON OBJECT
问题描述
这是我在 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()
但现在无法在 s3 上写回结果,'write_dynamic_frame.from_options'
因为它需要 DF,但我'jsonResults'
现在不再是 DataFrame。
解决方案
为了将其放入 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']
})
在这种情况下,我没有使用 Glue 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))
推荐阅读
- swift - 我需要帮助播放在线广播 http://www.vdee.org:8000/salinas。到目前为止我有这个代码
- c++ - 具有另一个类对象的 std::vector 的类
- ssl - 为什么浏览器第一次访问网站时会发送 TLS 会话 ID?
- rabbitmq - 使用 Spring 的 Queue 类创建队列和直接从 RabbitMq 控制台创建有什么区别?
- css - 从less编译到css时-webkit-box-orient丢失
- django - Matplotlib 图像未显示在 Django 模板中
- linux - Run and start denodo platform using Command Line Linux Remote Server without GUI
- c# - 单击调用 JavaScript 函数并将附加数据加载到页面后的 Anglesharp Parse
- spring - spring security注册后如何登录?
- django - Cant get queryset to return list of objects that is descending by tag matches