def main(args: Array[String]): Unit = {
val conf = new SparkConf().setMaster("local[*]").setAppName("stremaingtest")
val sc = new SparkContext(conf)
val ssc = new StreamingContext(sc, Seconds(10))
ssc.checkpoint("/tmp/streamTest")
val sDstream = ssc.socketTextStream("localhost", 8888)
val words = sDstream.flatMap(_.split(" ")).map(x => (x, 1))
words.reduceByKeyAndWindow((x: Int, y: Int) => (x + y), Seconds(10), Seconds(10))
//获取状态
val statespc = StateSpec.function(transStateSpec _)
// words.mapWithState 1.6.2以后的API,全局的状态val wstate = words.mapWithState(statespc)
wstate.stateSnapshots().print()
ssc.start()
ssc.awaitTermination()
}
def transStateSpec(batchtime: Time, key: String, value: Option[Int], state: State[Long]): Option[(String, Long)] = {
val sum = value.getOrElse(0).toLong + state.getOption.getOrElse(0L)
val output = (key, sum)
state.update(sum)
Some(output)
}