在使用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
Flink MapState 中对象更新与存储机制解析

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

被折叠的 条评论
为什么被折叠?



