Spark: How to save a dataframe with headers?

后端 未结 2 932
被撕碎了的回忆
被撕碎了的回忆 2021-01-02 07:10

dataframe.saveasTextFile, saves only the data in a delimited format. How do I save the dataframe with headers in JAVA.

sourceRufFrame.toJavaRDD(         


        
相关标签:
2条回答
  • 2021-01-02 07:53

    With Spark 2.x,

    df.write.option("header", "true").csv("path")
    

    Cheers

    0 讨论(0)
  • 2021-01-02 07:56

    If you want to save as csv file, i would suggest using spark-csv package. You can save your dataframe simply with spark-csv as below with header.

    dataFrame.write
      .format("com.databricks.spark.csv")
      .option("header", "true")
      .option("delimiter",<your delimiter>)
      .save(output)
    

    You can refer below link, for further information: https://github.com/databricks/spark-csv

    Spark-csv has maven dependency.

    0 讨论(0)
提交回复
热议问题