使用spark-csv编写单个CSV文件 [英] Write single CSV file using spark-csv

查看:1453
本文介绍了使用spark-csv编写单个CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 https://github.com/databricks/spark-csv ,我要写一个

需要一个scala函数,它将接受路径和文件名等参数,并写入该CSV文件。 p>

Need a scala function which will take parameter like path and file name and write that CSV file.

推荐答案

正在创建一个包含多个文件的文件夹,因为每个分区都是单独保存的。如果您需要单个输出文件(仍在文件夹中),则可以在保存之前重新分区数据框:

It is creating a folder with multiple files, because each partition is saved individually. If you need a single output file (still in a folder) you can repartition data frame before saving:

df
   // place all data in a single partition 
   .coalesce(1)
   .write.format("com.databricks.spark.csv")
   .option("header", "true")
   .save("mydata.csv")

所有数据将被写入 mydata.csv / part-00000 。在使用此选项之前,请确保您了解正在发生的情况,以及将所有数据传输到单个工作人员的费用。如果您使用分布式文件系统与复制,数据将被多次传输 - 首先提取到单个工作线程,然后分布在存储节点上。

All data will be written to mydata.csv/part-00000. Before you use this option be sure you understand what is going on and what is the cost of transferring all data to a single worker. If you use distributed file system with replication, data will be transfered multiple times - first fetched to a single worker and subsequently distributed over storage nodes.

或者,您可以保留代码,使用通用工具,例如 cat HDFS getmerge 以后简单地合并所有部分。

Alternatively you can leave your code as it is and use general purpose tools like cat or HDFS getmerge to simply merge all the parts afterwards.

这篇关于使用spark-csv编写单个CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆