对于之前提出的类似问题,我深表歉意。这个问题是用Python提出的。但我找不到正确的解决方案,我有以下数据帧df1
SomeJson
=================
[{
"Number": "1234",
"Color": "blue",
"size": "Medium"
}, {
"Number": "2222",
"Color": "red",
"size": "Small"
}
]
我试图将这个数据帧的内容写成json
df0.coalesce(300).write.mode('append').json(<json_Path>)
它引入了第一个键,如:
{
"SomeJson": [{
"Number": "1234",
"Color": "blue",
"size": "Medium"
}, {
"Number": "2222",
"Color": "red",
"size": "Small"
}
]
}
但是,我不想在输出文件中包含{“SomeJson”:}。我试着写在下面。但是,我在编写自定义Python函数以消除第一个头时迷失了方向。非常感谢您的帮助
df0.rdd.map(<custom_function>).saveAsTextFile(<json_Path>)
根据这个答案:Convert pyspark dataframe into list of python dictionaries
您可以这样做:
它产生如下输出:
编辑:
读取json时,Spark不维护顺序。但是我们可以改变我们收到的字典的顺序。由于python3中的dictionary保持插入顺序,因此我们只需要创建一个新的dictionary,并考虑插入顺序。剩下的只是字符串操作。我会这样做的
它产生以下输出
相关问题 更多 >
编程相关推荐