Spark:java.io.FileNotFoundException:copyMerge中不存在文件

时间:2017-10-23 06:01:52

标签: scala hadoop apache-spark hdfs spark-csv

我正在尝试合并目录中的所有spark输出部分文件,并在Scala中创建单个文件。

这是我的代码:

import org.apache.spark.sql.functions.input_file_name
import org.apache.spark.sql.functions.regexp_extract

def merge(srcPath: String, dstPath: String): Unit =  {
   val hadoopConfig = new Configuration()
   val hdfs = FileSystem.get(hadoopConfig)
   FileUtil.copyMerge(hdfs, new Path(srcPath), hdfs, new Path(dstPath), true, hadoopConfig, null) 
   // the "true" setting deletes the source files once they are merged into the new output
}

然后在最后一步,我正在写下面的数据帧输出。

dfMainOutputFinalWithoutNull.repartition(10).write.partitionBy("DataPartition","StatementTypeCode")
  .format("csv")
  .option("nullValue", "")
  .option("header", "true")
  .option("codec", "gzip")
  .mode("overwrite")
  .save(outputfile)
  merge(mergeFindGlob, mergedFileName )
  dfMainOutputFinalWithoutNull.unpersist()

当我运行时,我得到以下异常

java.io.FileNotFoundException: File does not exist: hdfs:/user/zeppelin/FinancialLineItem/temp_FinancialLineItem
  at org.apache.hadoop.hdfs.DistributedFileSystem$22.doCall(DistributedFileSystem.java:1309)

这是我获取输出的方式

enter image description here

我想合并文件夹中的所有文件并创建一个文件,而不是文件夹。

1 个答案:

答案 0 :(得分:0)

Hadoop 2中有一个copyMerge API: https://hadoop.apache.org/docs/r2.7.1/api/src-html/org/apache/hadoop/fs/FileUtil.html#line.382

不幸的是,这将在Hadoop 3.0中被弃用和删除。

这里重新实现了copyMerge(虽然在PySpark中)我不得不写,因为我们无法找到更好的解决方案: https://github.com/Tagar/stuff/blob/master/copyMerge.py

希望它也可以帮助其他人。

相关问题