首页 > 解决方案 > 如何处理在火花中读取不存在的文件

问题描述

我正在尝试使用 spark 从 HDFS 读取一些文件sc.wholeTextFiles,我传递了所需文件的列表,但工作一直在抛出

py4j.protocol.Py4JJavaError: An error occurred while calling o98.showString.
: org.apache.hadoop.mapreduce.lib.input.InvalidInputException: Input path does not exist:

如果其中一个文件不存在。

如何绕过未找到的文件而只读取找到的文件?

标签: apache-sparkpyspark

解决方案


要知道文件是否存在(并在我的情况下将其删除),我执行以下操作:

import org.apache.hadoop.fs.{FileSystem, Path}

val fs = FileSystem.get(spark.sparkContext.hadoopConfiguration)
if (fs.exists(new Path(fullPath))) {
  println("Output directory already exists. Deleting it...")
  fs.delete(new Path(fullPath), true)
}

推荐阅读