注意:
- window length(窗口長度) - 窗口的持續時間(圖3)
- sliding interval(滑動時間間隔) - 執行窗口操作的時間間隔(圖2)
這兩個參數必須是DStream批處理的時間間隔的倍數
示例
- 代碼
object WindowApp {
def main(args: Array[String]) {
System.setProperty("hadoop.home.dir", "D:\\hadoop")
// 準備工作
val conf = new SparkConf().setMaster("local[2]").setAppName("WindowApp")
val ssc = new StreamingContext(conf, Seconds(10))
// 業務邏輯處理
val lines = ssc.socketTextStream("hadoop000", 9999) // 1 thread
lines.flatMap(_.split(","))
.map((_,1)).reduceByKeyAndWindow((a:Int,b:Int) => (a + b),
Seconds(10), Seconds(5))
.print()
ssc.start() // Start the computation
ssc.awaitTermination() // Wait for the computation to terminate
}
-
如上:val ssc = new StreamingContext(conf, Seconds(5))
這個批處理時間設置爲10s會報錯 -
原因:是下面設置滑動時間是5s(應該是10s的倍數)
.map((_,1)).reduceByKeyAndWindow((a:Int,b:Int) => (a + b),
Seconds(10), Seconds(5))