Spark数据框保存在hdfs位置的单个文件中 [英] Spark dataframe save in single file on hdfs location

查看:181
本文介绍了Spark数据框保存在hdfs位置的单个文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据框,我想保存在hdfs位置的单个文件。

I have dataframe and i want to save in single file on hdfs location.

我在这里找到解决方案使用spark-csv编写单个CSV文件

i found the solution here Write single CSV file using spark-csv

df.coalesce(1)
    .write.format("com.databricks.spark.csv")
    .option("header", "true")
    .save("mydata.csv")

但所有数据都将写入mydata.csv / part -00000,我想成为mydata.csv文件。

But all data will be written to mydata.csv/part-00000 and i wanted to be mydata.csv file.

可以吗?

任何帮助欣赏

推荐答案

不可能使用标准的spark库,但是可以使用Hadoop API来管理文件系统 - 将输出保存在临时目录中,然后移动文件到请求的路径。例如(在pyspark中):

It's not possible using standard spark library, but you can use Hadoop API for managing filesystem - save output in temporary directory and then move file to the requested path. For example (in pyspark):

df.coalesce(1) \
    .write.format("com.databricks.spark.csv") \
    .option("header", "true") \
    .save("mydata.csv-temp")

from py4j.java_gateway import java_import
java_import(spark._jvm, 'org.apache.hadoop.fs.Path')

fs = spark._jvm.org.apache.hadoop.fs.FileSystem.get(spark._jsc.hadoopConfiguration())
file = fs.globStatus(sc._jvm.Path('mydata.csv-temp/part*'))[0].getPath().getName()
fs.rename(sc._jvm.Path('mydata.csv-temp/' + file), sc._jvm.Path('mydata.csv'))
fs.delete(sc._jvm.Path('mydata.csv-temp'), True)

这篇关于Spark数据框保存在hdfs位置的单个文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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