val sparkconf = new SparkConf().setAppName("st").setMaster("local[2]") val ssc = new StreamingContext(sc, Seconds(60)) val path = "C:\\Users\\Desktop\\files\\*" val lines: DStream[String] = ssc.fileStream[LongWritable, Text, TextInputFormat](path, (t: org.apache.hadoop.fs.Path) => true, newFilesOnly = false).map(_._2.toString) val wc = lines .flatMap(_.split(",")) .map(arr => (arr, 1)) .reduceByKey(_+_) wc.count().print()
这里本想连同路径下已经存在的文件一起处理,但是设置newFilesOnly = false并没有生效,经过查询测试,newFilesOnly =false只会处理与当前处理窗口相邻的上一个窗口中的文件,上述代码中设置的窗口时长是60s,假如当前时间是12:05, 则(12:04,12:05]创建的历史文件是可以被处理的,再往前就处理不了了,处理办法是加上参数: sparkconf.set("spark.streaming.fileStream.minRememberDuration", "86400s"),指定多少时间,就能处理当前时间向前推这个时间段内生成的历史数据