import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}
object windowOpObj {
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setAppName("windowOpObj").setMaster("local[*]")
//创建
val ssc= new StreamingContext(conf,Seconds(5))
//存储不同(之前)批次的RDD数据
ssc.checkpoint("D:\\数据\\spark")
//获取数据
val datream = ssc.socketTextStream("192.168.88.130",8888)
//调用窗口函数来聚合多个批次的数据,现在的操作基于每一个批次
val tupls = datream.flatMap(_.split(" ")).map((_,1))
//设置窗口长度以及滑动时间间隔
//处理多个批次,2参数是窗口长度,第3个参数是窗口的滑动时间,可以计算指定范围内的批次数据
val res= tupls.reduceByKeyAndWindow((x:Int,y:Int)=>(x+y),Seconds(15),Seconds(10))
res.print()
ssc.start()
ssc.awaitTermination()
}
}