java Spark:如何使用标题保存数据帧?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36505156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:32:10  来源:igfitidea点击:

Spark: How to save a dataframe with headers?

javaapache-spark

提问by user3897533

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

dataframe.saveasTextFile, 仅以分隔格式保存数据。如何在 JAVA 中保存带有标题的数据帧。

sourceRufFrame.toJavaRDD().map(new TildaDelimiter()).coalesce(1, true).saveAsTextFile(targetSrcFilePath);

回答by Srini

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

如果你想保存为 csv 文件,我建议使用spark-csvpackage.json 。您可以spark-csv使用标题简单地保存您的数据框,如下所示。

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

您可以参考以下链接,了解更多信息:https: //github.com/databricks/spark-csv

Spark-csvhas maven dependency.

Spark-csv有 maven 依赖。

回答by Chitral Verma

With Spark 2.x,

使用 Spark 2.x,

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

Cheers

干杯