Writing CSV file using Spark and scala - empty quotes instead of Null values

自闭症网瘾萝莉.ら 提交于 2020-05-14 07:08:50

问题


I'm using spark 2.4.1 and scala, and trying to write DF to csv file. it seems that in case of null values ,the csv contains "". Is it possible to remove those empty quotes?

 val data = Seq(
      Row(1, "a"),
      Row(5, "z"),
      Row(5, null)
    )

    val schema = StructType(
      List(
        StructField("num", IntegerType, true),
        StructField("letter", StringType, true)
      )
    )

    var df = spark.createDataFrame(
      spark.sparkContext.parallelize(data),
      schema
    )
  df.write.csv("location/")

The output seems like:

1,a
5,z
5,""

And I want it will be:

1,a
5,z
5,

What should I do?

Thanks!


回答1:


You can use options of the writer see CSV specific options(SaveMode is not related to answer);

 df.write.option("nullValue", null).mode(SaveMode.Overwrite).csv("location/")


来源:https://stackoverflow.com/questions/57312569/writing-csv-file-using-spark-and-scala-empty-quotes-instead-of-null-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!