如何合并由 SPARK 数据框创建的文件夹中的所有零件文件并在 Scala 中重命名为文件夹名称 [英] How to merge all part files in a folder created by SPARK data frame and rename as folder name in scala
问题描述
我有我的 spark 数据框的输出,它创建了文件夹结构并创建了部分文件.现在我必须合并文件夹内的所有部分文件并将该文件重命名为文件夹路径名.
Hi i have output of my spark data frame which creates folder structure and creates so may part files . Now i have to merge all part files inside the folder and rename that one file as folder path name .
这就是我做分区的方式
df.write.partitionBy("DataPartition","PartitionYear")
.format("csv")
.option("nullValue", "")
.option("header", "true")/
.option("codec", "gzip")
.save("hdfs:///user/zeppelin/FinancialLineItem/output")
它创建这样的文件夹结构
It creates folder structure like this
hdfs:///user/zeppelin/FinancialLineItem/output/DataPartition=Japan/PartitionYear=1971/part-00001-87a61115-92c9-4926-a803-b46315e55a08.c000.csv.gz
hdfs:///user/zeppelin/FinancialLineItem/output/DataPartition=Japan/PartitionYear=1971/part-00002-87a61115-92c9-4926-a803-b46315e55a08.c001.csv.gz
我必须像这样创建最终文件
I have to create final file like this
hdfs:///user/zeppelin/FinancialLineItem/output/Japan.1971.currenttime.csv.gz
此处没有将 001 和 002 合并为二合一的零件文件.
No part files here bith 001 and 002 is merged two one .
我的数据大小非常大,300 GB gzip 和 35 GB 压缩,所以 coalesce(1) 和 repartition
变得非常慢.
My data size it very big 300 GB gzip and 35 GB zipped so coalesce(1) and repartition
becomes very slow .
我在这里看到了一个解决方案使用 spark-csv 写入单个 CSV 文件 但我我无法实现它,请帮助我.
I have seen one solution here Write single CSV file using spark-csv but i am not able to implement it please help me with it .
重新分区抛出错误
error: value repartition is not a member of org.apache.spark.sql.DataFrameWriter[org.apache.spark.sql.Row]
dfMainOutputFinalWithoutNull.write.repartition("DataPartition","StatementTypeCode")
推荐答案
从 Spark 外部的头节点试试这个...
Try this from the head node outside of Spark...
hdfs dfs -getmerge
https://hadoop.apache.org/docs/r1.2.1/file_system_shell.html#getmerge
将源目录和目标文件作为输入,并将 src 中的文件连接到目标本地文件中.可以选择设置 addnl 以启用在每个文件的末尾添加换行符."
"Takes a source directory and a destination file as input and concatenates files in src into the destination local file. Optionally addnl can be set to enable adding a newline character at the end of each file."
这篇关于如何合并由 SPARK 数据框创建的文件夹中的所有零件文件并在 Scala 中重命名为文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!