복붙노트

[HADOOP] Spark를 사용하여 Hadoop HDFS 디렉토리의 모든 파일을 나열 하시겠습니까?

HADOOP

Spark를 사용하여 Hadoop HDFS 디렉토리의 모든 파일을 나열 하시겠습니까?

Hadoop 디렉토리의 모든 텍스트 파일을 반복하여 "error"라는 단어가 모두 나오길 원합니다. hadoop fs -ls / users / ubuntu /를 Apache Spark Scala API를 사용하여 dir의 모든 파일을 나열하는 방법이 있습니까?

주어진 첫 번째 예제에서, 스파크 컨텍스트는 다음과 같은 것을 통해서만 개별적으로 파일에 액세스하는 것처럼 보입니다.

val file = spark.textFile("hdfs://target_load_file.txt")

내 문제는, 내가 얼마나 많은 HDFS 폴더에있는 파일의 이름을 사전에 모르겠다. 스파크 컨텍스트 문서를 보았지만 이런 종류의 기능을 찾을 수 없었습니다.

해결법

  1. ==============================

    1.와일드 카드를 사용할 수 있습니다.

    와일드 카드를 사용할 수 있습니다.

    val errorCount = sc.textFile("hdfs://some-directory/*")
                       .flatMap(_.split(" ")).filter(_ == "error").count
    
  2. ==============================

    2.

    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)
    
  3. from https://stackoverflow.com/questions/23352311/use-spark-to-list-all-files-in-a-hadoop-hdfs-directory by cc-by-sa and MIT license