检查文件夹是否存在于 blob 存储中并添加到列表中

check if folder exists in blob storage and add to a list

我正在 Databricks 中创建一个函数来检查 Azure Blob 存储中是否存在文件夹:

import java.io.File

def checkFolder(paths: List[String]): Unit = {
  
  for (f <- paths) {
    try
      {   
        var pathCheck = dbutils.fs.ls(f) 
       }
  catch {
    case ex: Exception => {
      println("Folder does not exists: " + f)
      }
    }
  }
}

我想添加列表中存在的文件夹,以便我可以读取这些路径中的数据!我也想添加不同列表中不存在的路径。

我该怎么做?

尝试这样的事情:

val (exist, doesnt) = paths.map {
  f => try {
    dbutils.fs.ls(f)
    (f, true)
  } catch {
    case _: Exception => 
      (f, false)
  }
}.partition(_._2)

这将为您提供两个类型为 (String, Boolean) 的现有和不现有文件序列,然后您可以使用以下方法从中提取文件路径:

exist.map(_._1)
doesnt.map(_._1)