我之前遇到过处理日志文件的是文件里面没有日期,日期在文件名上,后来我就上网上搜了搜,发现spark并没有对文件名有过多的解释,但是看到一些文章写的是spark对文件名的操作,领略了下,把自己的想法写出来给大家看看
def get_hdfs_dir(input: String, sc: SparkContext): Array[String] = {
//创建[LongWritable, Text, TextInputFormat]的rdd
val fileRDD = sc.newAPIHadoopFile[LongWritable, Text, TextInputFormat](input)
//获取hadoop的rdd
val hadoopRDD = fileRDD.asInstanceOf[NewHadoopRDD[LongWritable, Text]]
//通过文件进行分区,然后通过不同分区来获取分区的path
val fileAdnLine = hadoopRDD.mapPartitionsWithInputSplit((inputSplit: InputSplit, iterator: Iterator[(LongWritable, Text)]) => {
val file = inputSplit.asInstanceOf[FileSplit]
iterator.take(1).map(x => {
file.getPath.toString() //就是当前数据的所在路径
})
})
val dirOut: Array[String] = fileAdnLine
.distinct() //可能会有很多一样的path,进去去重操作
.coalesce(1)
// val array: Array[List[Char]] = fileAdnLine //这里面存放的就是path
.map(lines => {
lines.toString
})
.collect()
dirOut //dirOut 是一个Array类型的数据里面存放的就是传入文件夹下的路径
}