python将pyspark数据帧写入json而不带头

2024-10-02 02:23:58 发布

您现在位置:Python中文网/ 问答频道 /正文

对于之前提出的类似问题,我深表歉意。这个问题是用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>)

Tags: 数据pathjsonnumber内容sizebluered
1条回答
网友
1楼 · 发布于 2024-10-02 02:23:58

根据这个答案:Convert pyspark dataframe into list of python dictionaries

您可以这样做:

df0.rdd.map(lambda x: [ele.asDict() for ele in x["SomeJson"]]).saveAsTextFile("data/output.json")

它产生如下输出:

[{'Color': 'blue', 'Number': '1234', 'size': 'Medium'}, {'Color': 'red', 'Number': '2222', 'size': 'Small'}]

编辑:

读取json时,Spark不维护顺序。但是我们可以改变我们收到的字典的顺序。由于python3中的dictionary保持插入顺序,因此我们只需要创建一个新的dictionary,并考虑插入顺序。剩下的只是字符串操作。我会这样做的

required_order = ["Number","Color","size"]

def change_order(row_dict, order):
    temp_dict = {}
    for name in order:
        temp_dict[name] = row_dict[name]
    return temp_dict

df0.rdd.map(lambda x: "{" + ",".join([str(ele) for ele in [change_order(ele.asDict(), required_order) for ele in x["SomeJson"]]]) + "}").saveAsTextFile("data/output.json")

它产生以下输出

{{'Number': '1234', 'Color': 'blue', 'size': 'Medium'},{'Number': '2222', 'Color': 'red', 'size': 'Small'}}

相关问题 更多 >

    热门问题