flink MapState 更新存储对象问题

Flink MapState 中对象更新与存储机制解析
本文探讨了在使用Flink的MapState时遇到的问题,即修改存储对象后MapState并未自动更新。通过官方文档和源码分析,了解到MapState并非如常规Map那样实时更新。原因在于不同StateBackend的存储方式,如InMemoryStateBackend和FsStateBackend保存对象引用,而RocksDBStateBackend则进行序列化操作,导致修改对象不影响状态。因此建议在对象修改后显式更新MapState。

在使用flink中的状态管理器时,由于需求背景我选用了MapState存储结构,但是在使用过程中发现修改存储对象的时候并没有MapState中的值并没有自动更新,这让我产生了很大的疑惑,看官方文档和flink源码中我都一直把MapState认为是类似于Map结构的,那自然认为修改里面的存储对象时整个map会自动更新,但是实际使用和测试下来确发现不是这样。
先贴一段官方文档:

MapState<UK, UV>: This keeps a list of mappings. You can put key-value pairs into the state and retrieve an Iterable over all currently stored mappings. Mappings are added using put(UK, UV) or putAll(Map<UK, UV>). The value associated with a user key can be retrieved using get(UK). The iterable views for mappings, keys and values can be retrieved using entries(), keys() and values() respectively. You can also use isEmpty() to check whether this map contains any key-value mappings.

接下来再贴一下代码:
首先来看map中修改对象的case:

// 实体类
public class OutputEntity {
   
   
    private Integer amt;

    public static OutputEntity getOneOutput(Integer amt){
   
   
        OutputEntity output = new OutputEntity(amt);
        output.amt = 10;
        return output;
    }

    public OutputEntity(Integer amt){
   
   
        this.amt = amt;
    }

    public void incr(Integer newAmt){
   
   
        this.amt += newAmt;
    }

    public Integer getAmt() {
   
   
        return amt;
    }

    public void 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值