我有数据框,我想保存在 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 位置的单个文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!