【问题标题】:pyspark df.write.json('s3e://somepath') is binarypyspark df.write.json('s3e://somepath') 是二进制的
【发布时间】: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


    【解决方案1】:

    检查您正在使用的 S3 API。是拼写错误“s3e”吗?试试

    resultdf.write.json("s3a://mybucket/testingjson")
    

    如果它是一个小数据集,你可以合并成一个文件。

    resultdf.coalesce(1).write.json("s3a://mybucket/testingjson")
    

    更多细节 Technically what is the difference between s3n, s3a and s3?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      相关资源
      最近更新 更多