Explore Courses Blog Tutorials Interview Questions
0 votes
in Big Data Hadoop & Spark by (11.4k points)

I want to list all folders within a hdfs directory using Scala/Spark. In Hadoop I can do this by using the command: 

hadoop fs -ls hdfs://

I tried it with:

val conf = new Configuration()
val fs = FileSystem.get(new URI("hdfs://"), conf)

val path = new Path("hdfs://")

val files = fs.listFiles(path, false)

But it does not seem that he looks in the Hadoop directory as i cannot find my folders/files.

I also tried with:

FileSystem.get(sc.hadoopConfiguration).listFiles(new Path("hdfs://"), true)

But this also does not help.

Do you have any other idea?

1 Answer

0 votes
by (32.3k points)

In Hadoop 1.4, we are not provided with listFiles method so we use listStatus to get directories. It doesn't have recursive option but it is easy to manage recursive lookup.

val fs = FileSystem.get(new Configuration())

val status = fs.listStatus(new Path(YOUR_HDFS_PATH))

status.foreach(x=> println(x.getPath))

For Spark 2.0+,

import org.apache.hadoop.fs.{FileSystem, Path}

val fs = org.apache.hadoop.fs.FileSystem.get(spark.sparkContext.hadoopConfiguration)

fs.listStatus(new Path(s"${hdfs-path}")).filter(_.isDir).map(_.getPath).foreach(println)

Browse Categories