Pyspark 将AWS胶水输出格式化为JSON对象
这是我在AWS GLUE的pyspark工作中得到的结果Pyspark 将AWS胶水输出格式化为JSON对象,pyspark,aws-glue,Pyspark,Aws Glue,这是我在AWS GLUE的pyspark工作中得到的结果 {a:1,b:7} {a:1,b:9} {a:1,b:3} 但我需要在s3上编写这些数据,并将其发送到JSON数组中的API 格式 我尝试将输出转换为DataFrame,然后应用 toJSON() results=mapped\u dyF.toDF() jsonResults=results.toJSON().collect() 但是现在无法使用“write\u dynamic\u frame.from\u options” 由于它需要
{a:1,b:7}
{a:1,b:9}
{a:1,b:3}
但我需要在s3上编写这些数据,并将其发送到JSON数组中的API
格式
我尝试将输出转换为DataFrame,然后应用
toJSON()
results=mapped\u dyF.toDF()
jsonResults=results.toJSON().collect()
但是现在无法使用“write\u dynamic\u frame.from\u options”
由于它需要DF,但我的
'jsonResults'
现在不再是数据帧。为了将其转换为JSON数组格式,我通常执行以下操作:
df-->包含原始数据的数据帧
if df.count() > 0:
# Build the json file
data = list()
for row in df.collect():
data.append({"a": row['a'],
"b" : row['b']
})
我没有使用Gluewrite\u dynamic\u框架。在本例中,从\u options
中,我使用bot3
保存文件:
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))
为了将其转换为JSON数组格式,我通常会执行以下操作: df-->包含原始数据的数据帧
if df.count() > 0:
# Build the json file
data = list()
for row in df.collect():
data.append({"a": row['a'],
"b" : row['b']
})
我没有使用Gluewrite\u dynamic\u框架。在本例中,从\u options
中,我使用bot3
保存文件:
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))
如果您将条目弹出到数组列中,而不是将其转换为json,这可能会起作用。如果您将条目弹出到数组列中,这可能会起作用,而不是将其转换为json