代码之家  ›  专栏  ›  技术社区  ›  Jas

pyspark df.write.json(“s3e://somepath”)是二进制的

  •  1
  • Jas  · 技术社区  · 7 年前

    我正在使用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}}]
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Manoj Singh    7 年前

    检查您使用的S3 API。是打字错误吗?尝试用

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

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

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

    更多详细信息 Technically what is the difference between s3n, s3a and s3?

    推荐文章