gpt4 book ai didi

scala - Spark : Read file only if the path exists

转载 作者:行者123 更新时间:2023-12-04 01:58:31 26 4
gpt4 key购买 nike

我正在尝试读取 Sequence 上的文件scala 中的路径。以下是示例(伪)代码:

val paths = Seq[String] //Seq of paths
val dataframe = spark.read.parquet(paths: _*)

现在,在上述序列中,有些路径存在而有些则不存在。阅读 parquet 时有什么方法可以忽略丢失的路径吗?文件(以避免 org.apache.spark.sql.AnalysisException: Path does not exist )?

我已经尝试了以下方法,它似乎正在工作,但是,我最终读取了相同的路径两次,这是我想避免做的事情:
val filteredPaths = paths.filter(p => Try(spark.read.parquet(p)).isSuccess)

我查了 options DataFrameReader 的方法但这似乎没有任何类似于 ignore_if_missing 的选项.

此外,这些路径可以是 hdfss3 (此 Seq 作为方法参数传递)并且在阅读时,我不知道路径是否为 s3hdfs所以不能使用 s3hdfs特定的 API 来检查是否存在。

最佳答案

从 Spark 2.3.0 开始有一个配置 spark.sql.files.ignoreMissingFiles .只需将其设置为 true .
https://spark.apache.org/docs/latest/configuration.html

关于scala - Spark : Read file only if the path exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45193825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com