我想循环遍历Hadoop目录中的所有文本文件,并计算“错误”一词的所有出现次数。有没有办法做一个 hadoop fs -ls /users/ubuntu/
使用Apache Spark Scala API列出目录中的所有文件?
从给定的 第一个例子,spark上下文似乎只能通过以下方式单独访问文件:
val file = spark.textFile("hdfs://target_load_file.txt")
在我的问题中,我不知道预先在HDFS文件夹中有多少文件名。看着 spark context docs 但找不到这种功能。
您可以使用通配符:
val errorCount = sc.textFile("hdfs://some-directory/*")
.flatMap(_.split(" ")).filter(_ == "error").count
您可以使用通配符:
val errorCount = sc.textFile("hdfs://some-directory/*")
.flatMap(_.split(" ")).filter(_ == "error").count
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)