【发布时间】:2020-05-22 00:39:56
【问题描述】:
我在 scala 和 spark 环境中工作,我想在其中读取 parquet 文件。在阅读之前,我想检查文件是否存在。我正在 jupyter notebook 中编写以下代码,但它不起作用 - 这意味着它不显示任何框架,因为函数 testDirExist 返回 false
import org.apache.hadoop.fs.FileSystem
import org.apache.hadoop.fs.Path
val hadoopfs: FileSystem = FileSystem.get(spark.sparkContext.hadoopConfiguration)
def testDirExist(path: String): Boolean = {
val p = new Path(path)
hadoopfs.exists(p) && hadoopfs.getFileStatus(p).isDirectory
}
val pt = "abfss://container@account.dfs.core.windows.net/blah/blah/blah
val exists = testDirExist(pt)
if(exists)
{
val dataframe = spark.read.parquet(pt)
dataframe.show()
}
但是,以下代码有效。它显示数据框
val k = spark.read.parquet("abfss://container@account.dfs.core.windows.net/blah/blah/blah)
k.show()
谁能帮我检查文件是否存在?
谢谢
【问题讨论】:
-
我遇到了同样的问题。使用hadoop配置创建的文件系统似乎指向本地文件系统。你找到解决办法了吗?
-
spark.sparkContext.hadoopConfiguration 不会自动填充。您必须使用@Chen Meyouhas answer中的值设置它们
标签: scala dataframe apache-spark hadoop azure-data-lake