Flink的KeyedStateStore的5中state

KeyedStateStore有ValueState,ListState,ReducingState,AggregatingState,MapState5中状态。下面实现这5种状态示例。
1.KeyedStateStore接口的所有get方法
在这里插入图片描述
2.ValueState

  DataStream<MyType> stream = ...;
	 KeyedStream<MyType> keyedStream = stream.keyBy("id");
	
	 keyedStream.map(new RichMapFunction<MyType, Tuple2<MyType, Long>>() {
	
	     private ValueState<Long> count;
	
	     public void open(Configuration cfg) {
	         state = getRuntimeContext().getState(
	                 new ValueStateDescriptor<Long>("count", LongSerializer.INSTANCE, 0L));
	     }
	
	     public Tuple2<MyType, Long> map(MyType value) {
	         long count = state.value() + 1;
	         state.update(value);
	         return new Tuple2<>(value, count);
	     }
	 });
	 }

3.ListState

	 DataStream<MyType> stream = ...;
	 KeyedStream<MyType> keyedStream = stream.keyBy("id");
	
	 keyedStream.map(new RichFlatMapFunction<MyType, List<MyType>>() {
	
	     private ListState<MyType> state;
	
	     public void open(Configuration cfg) {
	         state = getRuntimeContext().getListState(
	                 new ListStateDescriptor<>("myState", MyType.class));
	     }
	
	     public void flatMap(MyType value, Collector<MyType> out) {
	         if (value.isDivider()) {
	             for (MyType t : state.get()) {
	                 out.collect(t);
	             }
	         } else {
	             state.add(value);
	         }
	     }
	 });
	 }

4.ReducingState

   DataStream<MyType> stream = ...;
	 KeyedStream<MyType> keyedStream = stream.keyBy("id");
	
	 keyedStream.map(new RichMapFunction<MyType, List<MyType>>() {
	
	     private ReducingState<Long> state;
	
	     public void open(Configuration cfg) {
	         state = getRuntimeContext().getReducingState(
	                 new ReducingStateDescriptor<>("sum", (a, b) -> a + b, Long.class));
	     }
	
	     public Tuple2<MyType, Long> map(MyType value) {
	         state.add(value.count());
	         return new Tuple2<>(value, state.get());
	     }
	 });
	
	 }

5.AggregatingState

	 DataStream<MyType> stream = ...;
	 KeyedStream<MyType> keyedStream = stream.keyBy("id");
	 AggregateFunction<...> aggregateFunction = ...
	
	 keyedStream.map(new RichMapFunction<MyType, List<MyType>>() {
	
	     private AggregatingState<MyType, Long> state;
	
	     public void open(Configuration cfg) {
	         state = getRuntimeContext().getAggregatingState(
	                 new AggregatingStateDescriptor<>("sum", aggregateFunction, Long.class));
	     }
	
	     public Tuple2<MyType, Long> map(MyType value) {
	         state.add(value);
	         return new Tuple2<>(value, state.get());
	     }
	 });
	
	 }

6.MapState

   DataStream<MyType> stream = ...;
	 KeyedStream<MyType> keyedStream = stream.keyBy("id");
	
	 keyedStream.map(new RichMapFunction<MyType, List<MyType>>() {
	
	     private MapState<MyType, Long> state;
	
	     public void open(Configuration cfg) {
	         state = getRuntimeContext().getMapState(
	                 new MapStateDescriptor<>("sum", MyType.class, Long.class));
	     }
	
	     public Tuple2<MyType, Long> map(MyType value) {
	         return new Tuple2<>(value, state.get(value));
	     }
	 });
	
	 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值