Flink-Keyed State-ValueState使用(结合Window)

① 值状态(ValueState),将状态表示为单个值;(直接.value获取,Set操作是.update)

  • get操作: ValueState.value()
  • set操作: ValueState.update(T value)
package com.leilei;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.RichWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.streaming.api.windowing.windows.Window;
import org.apache.flink.util.Collector;

/**
 * @author lei
 * @version 1.0
 * @date 2021/3/17 22:56
 * @flink manageState下 keyState 下的 valueState
 */
public class Flink_State_1_ValueState {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setRuntimeMode(RuntimeExecutionMode.BATCH);
        env.setParallelism(1);
        DataStreamSource<Tuple2<String, Integer>> source = env.fromElements(
                Tuple2.of("迪迦奥特曼", 45),
                Tuple2.of("赛文奥特曼", 45),
                Tuple2.of("迪迦奥特曼", 24),
                Tuple2.of("赛文奥特曼", 99),
                Tuple2.of("赛文奥特曼", 98),
                Tuple2.of("雷欧奥特曼", 21));
        SingleOutputStreamOperator<Tuple2<String, Integer>> stream = source.keyBy(t -> t.f0).maxBy(1);
        stream.print("maxBy算子 底层自动实现");
        SingleOutputStreamOperator<Tuple2<String, Integer>> result = source.keyBy(t -> t.f0)
                .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
                .apply(new MyCalcWindow());
        result.print("valueState 个人手动实现");
        env.execute();
    }

    public static class MyCalcWindow extends RichWindowFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, String, TimeWindow> {
        private ValueState<Integer> maxHeightState = null;

        @Override
        public void apply(String s, TimeWindow window, Iterable<Tuple2<String, Integer>> input, Collector<Tuple2<String, Integer>> out) throws Exception {

            for (Tuple2<String, Integer> tuple2 : input) {
                // 缓存中获取历史身高记录
                Integer historyHeight = maxHeightState.value();
                // 输入数据中获取当前身高记录
                Integer currentHeight = tuple2.f1;
                if (historyHeight == null || historyHeight < currentHeight) {
                    historyHeight = currentHeight;
                    maxHeightState.update(historyHeight);
                }
            }
            out.collect(Tuple2.of(s,maxHeightState.value()));
        }

        @Override
        public void open(Configuration parameters) throws Exception {
            super.open(parameters);
            ValueStateDescriptor<Integer> heightStateDescriptor = new ValueStateDescriptor<>("maxHeightState", Integer.class);
            // 初始化身高缓存
            maxHeightState = getRuntimeContext().getState(heightStateDescriptor);
        }
    }

}

image-20210608223645455

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值