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));
	     }
	 });
	
	 }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FlinkValueState是一种状态类型,可以用于存储单个值。它通常用于对输入流的某些值进行聚合或累积计算,例如计算平均值或求和。以下是使用ValueState的示例代码: ``` // 导入必要的库 import org.apache.flink.api.common.functions.RichFlatMapFunction; import org.apache.flink.api.common.state.ValueState; import org.apache.flink.api.common.state.ValueStateDescriptor; import org.apache.flink.configuration.Configuration; import org.apache.flink.util.Collector; // 实现一个 RichFlatMapFunction public class MyFlatMapFunction extends RichFlatMapFunction<Integer, Integer> { private transient ValueState<Integer> sumState; @Override public void open(Configuration config) { // 初始化 ValueState ValueStateDescriptor<Integer> sumStateDescriptor = new ValueStateDescriptor<>("sum", Integer.class); sumState = getRuntimeContext().getState(sumStateDescriptor); } @Override public void flatMap(Integer value, Collector<Integer> out) throws Exception { // 从 ValueState 获取之前的累加结果 Integer sum = sumState.value(); if (sum == null) { sum = 0; } // 进行累加计算 sum += value; // 更新 ValueState 的值 sumState.update(sum); // 输出结果 out.collect(sum); } } ``` 在上面的示例,我们实现了一个 RichFlatMapFunction,使用ValueState对输入流的整数进行累加计算,并将结果输出。在open()方法,我们初始化了一个名为“sum”的ValueState。在flatMap()方法,我们首先从ValueState获取之前的累加结果,如果ValueState没有值,则将sum初始化为0。然后,我们进行累加计算,并更新ValueState的值。最后,我们将累加结果输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值