您可以将其放入 try 块和 catch 异常中
try {
val df: DataFrame = spark.read
.format("csv")
.option("delimiter", ",")
.schema(schema)
.load(inputPath + "/*.csv*")
}catch (Exception e) {
print("Do something else here")
e.getMessage();
}
如果你想检查 csv 是否存在,你可以先检查文件是否存在
import java.nio.file.{Paths, Files}
exist = Files.exists(Paths.get(inputPath + "/*.csv*"))
if (exist){
val df: DataFrame = spark.read
.format("csv")
.option("delimiter", ",")
.schema(schema)
.load(inputPath + "/*.csv*")
}
例如,如果您在inputPaths 中有多个路径,则可以将它们过滤为
inputPaths.filter(f => Files.exists(Paths.get(f + "/*.csv*")))
对于hdfs文件系统,可以将上面的逻辑替换为
单个文件
val conf = sc.hadoopConfiguration
val fs = org.apache.hadoop.fs.FileSystem.get(conf)
val exists = fs.exists(new org.apache.hadoop.fs.Path(inputPath + "/*.csv*"))
if (exist){
val df: DataFrame = spark.read
.format("csv")
.option("delimiter", ",")
.schema(schema)
.load(inputPath + "/*.csv*")
}
对于存储在数组中的多个文件位置。
val conf = sc.hadoopConfiguration
val fs = org.apache.hadoop.fs.FileSystem.get(conf)
inputPaths.filter(f => fs.exists(new org.apache.hadoop.fs.Path(f + "/*.csv*")))