列出 Spark 本地可用的所有文件名的最有效方法是什么?我正在使用 Scala API,但是,Python 也应该没问题。
最佳答案
import org.apache.hadoop.fs.{FileSystem, FileUtil, Path}
import scala.collection.mutable.Stack
val fs = FileSystem.get( sc.hadoopConfiguration )
var dirs = Stack[String]()
val files = scala.collection.mutable.ListBuffer.empty[String]
val fs = FileSystem.get(sc.hadoopConfiguration)
dirs.push("/user/username/")
while(!dirs.isEmpty){
val status = fs.listStatus(new Path(dirs.pop()))
status.foreach(x=> if(x.isDirectory) dirs.push(x.getPath.toString) else
files+= x.getPath.toString)
}
files.foreach(println)
关于python - 使用 Scala 或 Python 列出存储在 Hadoop HDFS 上的 Spark 集群中可用的所有文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23478377/