简体   繁体   English

SPARK SCALA将DF导出到TextFile

[英]SPARK SCALA Export DF to TextFile

I want to export DF to text file. 我想将DF导出到文本文件。

There are two problems to export it as text file. 将其导出为文本文件有两个问题。

  1. It has 46 columns, and error log says text only support 1 column. 它有46列,并且错误日志显示文本仅支持1列。

  2. I want the file is rolling fie means I want that same file store the result of next run. 我希望文件滚动,即表示我希望该文件存储下一次运行的结果。

Can you suggest me the way? 能给我建议一下吗?

There are two methods of writing out the dataframe. 有两种写出数据框的方法。 The easiest method is to write out the file using the Spark SQL API, but you can also use the RDD API (keep in mind it will be written out as a single column with the RDD API). 最简单的方法是使用Spark SQL API来写出文件,但是您也可以使用RDD API(请注意,它将与RDD API一起写成单列)。

Regarding writing out as a rolling file, I'm not sure that's possible with Spark. 关于写为滚动文件,我不确定Spark是否可以实现。 It would require opening the previous file in Spark, unioning the data, then writing it back to disk. 它将需要在Spark中打开先前的文件,合并数据,然后将其写回到磁盘。 You would probably be better off using incremental file names, which can be glob loaded as a single file. 使用增量文件名可能会更好,这些文件名可以作为单个文件进行全局加载。

Example Code for saving the DF as a csv: 将DF保存为csv的示例代码:

val exampleDF = spark.read.option("header","true").option("inferSchema","true").format("csv").load("example.csv")

exampleDF.write.csv("example.out")

Spark SQL DF Example Results: Spark SQL DF示例结果:

1,0,3,Braund, Mr. Owen Harris,male,22.0,1,0,A/5 21171,7.25,null,S
2,1,1,Cumings, Mrs. John Bradley (Florence Briggs Thayer),female,38.0,1,0,PC 17599,71.2833,C85,C
3,1,3,Heikkinen, Miss. Laina,female,26.0,0,0,STON/O2. 3101282,7.925,null,S
4,1,1,Futrelle, Mrs. Jacques Heath (Lily May Peel),female,35.0,1,0,113803,53.1,C123,S



Example Code for saving the RDD as text: 用于将RDD保存为文本的示例代码:

val exampleDF = spark.read.option("header","true").option("inferSchema","true").format("csv").load("example.csv")

exampleDF.rdd.saveAsTextFile("example.out")

RDD Example Results: RDD示例结果:

[1,0,3,Braund, Mr. Owen Harris,male,22.0,1,0,A/5 21171,7.25,null,S]
[2,1,1,Cumings, Mrs. John Bradley (Florence Briggs Thayer),female,38.0,1,0,PC 17599,71.2833,C85,C]
[3,1,3,Heikkinen, Miss. Laina,female,26.0,0,0,STON/O2. 3101282,7.925,null,S]
[4,1,1,Futrelle, Mrs. Jacques Heath (Lily May Peel),female,35.0,1,0,113803,53.1,C123,S]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM