Flink 之 状态管理
一:Operator State
二:KeyedState
open 是生命周期函数。只有在open之后才能够使用getRuntimeContext
键控状态的每一个分区都会维护一组状态,每个分区的状态互不干涉。
@Override
public Integer map(Sensor value) throws Exception {
System.out.println("map method is called.... the current value is = " + value.getId());
for (String str : myListState.get()) {
System.out.println("value is = " + str);
}
myListState.add("hello");
Integer count = valueState.value();
System.out.println("value state count is " + count);
count = count == null ? 0 : count;
++count;
valueState.update(count);
return count;
}```
通过上面的输出发现,每一条记录都会触发一次map函数执行
```java
@Override
public Integer map(Sensor value) throws Exception {
System.out.println("map method is called.... the current value is = " + value.getId());
for (String str : myListState.get()) {
System.out.println("value is = " + str);
}
myListState.add("hello");
// myMapState.get("1");
// myMapState.put("2", 12.3);
// myMapState.remove("2");
// myMapState.clear();
Integer count = valueState.value();
System.out.println("value state count is " + count);
count = count == null ? 0 : count;
++count;
valueState.update(count);
return count;
}