【问题标题】:Custom file name to write dataframe in PySpark在 PySpark 中写入数据帧的自定义文件名
【发布时间】:2020-04-24 17:20:25
【问题描述】:

我想写数据帧的记录。记录为 json 格式。所以我需要用我的自定义文件名而不是 part-0000-cfhbhgh.json 将内容写入文件。

【问题讨论】:

    标签: json python-3.x hadoop pyspark pyspark-dataframes


    【解决方案1】:

    我在 scala 中给出答案,但在 python 中,这些也是必不可少的步骤..

     import org.apache.hadoop.fs.{FileSystem, Path}
    
      val fs: FileSystem = FileSystem.get(spark.sparkContext.hadoopConfiguration);
      val file = fs.globStatus(new Path("data/jsonexample/part*"))(0).getPath().getName()
      println("file name " + file)
      fs.rename(
        new Path("data/jsonexample/" + file)
        , new Path("data/jsonexample/tsuresh97_json_toberenamed.json"))
    

    完整示例:

     import spark.implicits._
    
      val df = Seq(
        (123, "ITA", 1475600500, 18.0),
        (123, "ITA", 1475600500, 18.0),
        (123, "ITA", 1475600516, 19.0)
      ).toDF("Value", "Country", "Timestamp", "Sum")
      df.coalesce(1)
        .write
        .mode(SaveMode.Overwrite)
        .json("data/jsonexample/")
    
      import org.apache.hadoop.fs.{FileSystem, Path}
    
      val fs: FileSystem = FileSystem.get(spark.sparkContext.hadoopConfiguration);
      val file = fs.globStatus(new Path("data/jsonexample/part*"))(0).getPath().getName()
      println("file name " + file)
      fs.rename(
        new Path("data/jsonexample/" + file)
        , new Path("data/jsonexample/tsuresh97_json_toberenamed.json"))
    
    
    

    结果:

    json 内容:

    {"Value":123,"Country":"ITA","Timestamp":1475600500,"Sum":18.0}
    {"Value":123,"Country":"ITA","Timestamp":1475600500,"Sum":18.0}
    {"Value":123,"Country":"ITA","Timestamp":1475600516,"Sum":19.0}
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 2018-06-24
      • 2019-04-13
      • 2022-01-04
      • 1970-01-01
      • 2020-11-26
      相关资源
      最近更新 更多