Flink源码剖析:ValueState

本文介绍了Flink中的State概念,特别是ValueState的定义和使用。通过一个包含定时器和Keyed State的代码示例,展示了ValueState如何在无明显Key存储结构的情况下工作。接着,深入源码,探讨了Update和Value方法,揭示了StateMapEntry作为存储结构的角色,以及哈希表在其中的关键作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 定义

State一般指一个具体的task的状态,而checkpoint则表示了一个Flink Job在一个特定时刻的一份全局状态快照,即对所有task的state进行持久化。Flink中有两种基本类型的State:Keyed State,Operator State。

2. 示例

下面一个有关定时器timer和Keyed State的代码示例,实现将10s内未出现的消息发往下游的功能,示例代码可以运行,有兴趣的同学不妨跑一下[github demo]

// 这是我们要存储在State中的结构
public class CountWithTimestamp {
   
    public String key;
    public long count;
    public long lastModified;
}
// 这是一个自定义flatMapFunction处理逻辑
public class Splitter implements FlatMapFunction<String, Tuple2<String, Integer>> {
   
    @Override
    public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception {
   

        if(StringUtils.isNullOrWhitespaceOnly(s)) {
   
            System.out.println("invalid line");
            return;
        }

        for(String word : s.split(" ")) {
   
            collector.collect(new Tuple2<String, Integer>(word, 1));
        }
    }
}
// 主类
public class ProcessTime {
   

    /**
     * KeyedProcessFunction的子类,作用是将每个单词最新出现时间记录到backend,并创建定时器,
     * 定时器触发的时候,检查这个单词距离上次出现是否已经达到10秒,如果是,就发射给下游算子
     */
    static class CountWithTimeoutFunction extends KeyedProcessFunction<Tuple, Tuple2<String, Integer>, Tuple2<String, Long>> {
   

        // 自定义状态
        private ValueState<CountWithTimestamp> state;

        @Override
        public void open(Configuration parameters) throws Exception {
   
            // 初始化状态,name是myState
            state = getRuntimeContext().getState(new ValueStateDescriptor<>("myState", CountWithTimestamp.class));
        }

        @Override
        public void processElement(
                Tuple2<String, Integer> value,
                Context ctx,
                Collector<Tuple2<String, Long>> out) throws Exception {
   

            // 取得当前是哪个单词
            Tuple currentKey = ctx.getCurrentKey();

            // 从backend取得当前单词的myState状态
            CountWithTimestamp current = state.value();

            // 如果myState还从未没有赋值过,就在此初始化
            if (current == null) {
   
                current = new CountWithTimestamp();
                current.key = value.f0;
            }

            // 单词数量加一
            current.count++;

            // 取当前元素的时间戳,作为该单词最后一次出现的时间
            current.lastModified = ctx.timestamp();

            // 重新保存到backend,包括该单词出现的次数,以及最后一次出现的时间
            state.update(current);

            // 为当前单词创建定时器,十秒后后触发
            long timer = current.lastModified + 10000;

            ctx.timerService().registerProcessingTimeTimer(timer);

            // 打印所有信息,用于核对数据正确性
            System.out.println(String.format
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值