【问题标题】:Listing all files available in Spark cluster stored on Hadoop HDFS using Scala or Python?使用 Scala 或 Python 列出存储在 Hadoop HDFS 上的 Spark 集群中的所有可用文件?
【发布时间】:2014-06-22 02:28:51
【问题描述】:

列出 Spark 中本地可用的所有文件名的最有效方法是什么?我正在使用 Scala API,但是,Python 也应该没问题。

【问题讨论】:

  • 列出 HDFS 上的文件并不是 Spark 特有的。只需使用org.apache.hadoop.fs.FileSystem.listStatus()。
  • 这实际上不起作用,但感谢您的提示,我知道要寻找什么。

标签: python scala hadoop apache-spark


【解决方案1】:
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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 2016-12-25
    • 2015-07-07
    • 2019-12-23
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    相关资源
    最近更新 更多