State通过用来保存中间状态
public class TestWindows extends RichWindowFunction<Long, Long, Long, Window> {
private MapState<Long, Double> mapState = null;
@Override
public void open(Configuration parameters) throws Exception {
//
super.open(parameters);
MapStateDescriptor<Long, Double> descriptor = new MapStateDescriptor<Long, Double>("descriptor",
TypeInformation.of(Long.class), TypeInformation.of(Double.class));
mapState = getRuntimeContext().getMapState(descriptor);
}
@Override
public void close() throws Exception {
super.close();
mapState = null;
}
@Override
public void apply(Long aLong, Window window, Iterable<Long> input, Collector<Long> out) throws Exception {
//这里面对state进行查询和修改
Double oldValue = null;
try {
oldValue = mapState.get(aLong);
} catch (Exception e) {
}
mapState.put(aLong, oldValue);
}
}