def main(args: Array[String]): Unit = {
val env = StreamExecutionEnvironment.getExecutionEnvironment
env.fromCollection(List(
(1L, 3L),
(1L, 5L),
(1L, 7L),
(1L, 4L),
(1L, 2L),
(2L, 7L),
(2L, 4L),
(2L, 2L)
)).keyBy(_._1)
.flatMap(new CountSate)
// .mapWithState[(Long,Long),(Long,Long)](
// (in:(Long,Long),count:Option[(Long,Long)]) =>
// count match {
// case Some(c)=>((in._1,in._2+c._2),Some(in._1+c._1,in._2+c._2))
// case None => ( (in._1, in._2), Some(in._1,in._2) )
// }
// )
.print()
// the printed output will be (1,4) and (1,5)
env.execute("ExampleManagedState")
}
class CountSate extends RichFlatMapFunction[(Long,Long),(Long,Long)] {
private var sum:ValueState[(Long )]=_
override def flatMap(value: (Long, Long), out: Collector[(Long, Long)]): Unit = {
val tmpCurrentValue=sum.value()
val CurrentValue=if(tmpCurrentValue!=null){
tmpCurrentValue
}
else
{
0L
}
val newSum=CurrentValue+value._2
// println(newSum)
sum.update(newSum)
out.collect((value._1,newSum))
}
override def open(parameters: Configuration): Unit = {
sum=getRuntimeContext.getState(new ValueStateDescriptor[(Long)]("average",createTypeInformation[(Long)],0L))
}
}
Flink自定义状态实现wordcoun通过扩展RichFlatMapFunction
最新推荐文章于 2024-05-05 23:20:58 发布