【发布时间】:2019-05-06 00:39:37
【问题描述】:
我正在使用pyspark,我想将结果写入json,但是当我使用时
df.write.json("s3e://somepath") 然后我得到的 json 为:part-00000-sdfh837fjh-6f8a-44d1-b0bb-sdjfh9236dj-c000.json
创建我的 df 的命令类似于以下:
import json
from pyspark.sql.functions import *
from pyspark.sql.types import *
rdd = sc.parallelize([(1,2,3),(4,5,6),(7,8,9)])
df = rdd.toDF(["a","b","c"])
resultrdd = df.rdd.map(lambda x: ({"x": {"y": x.a}, "xx" + "yy": {"yy" + "yy": x.b}}))
resultdf = resultrdd.toDF()
resultdf.write.json("s3e://mybucket/testingjson") # and the resulting files are binary and not json files. why? how can i fix it??
resultrdd.collect()
resultdf.printSchema()
当我打开生成s3e://mybucket/testingjson 的文件时,它们是二进制文件,无法使用文本编辑器打开。为什么会这样?如何让 df.write.json 创建实际的 json 文件?
注意打印的方案如下:
root
|-- x: map (nullable = true)
| |-- key: string
| |-- value: long (valueContainsNull = true)
|-- xxyy: map (nullable = true)
| |-- key: string
| |-- value: long (valueContainsNull = true)
请注意,如果我打印我得到的数据框(以验证 json 包含的内容):
resultdf
[{'x': {'y': 1}, 'xxyy': {'yyyy': 2}},
{'x': {'y': 4}, 'xxyy': {'yyyy': 5}},
{'x': {'y': 7}, 'xxyy': {'yyyy': 8}}]
【问题讨论】:
标签: apache-spark pyspark