Apache flink 是否强制清除窗口末尾的窗口状态对象?

Apache flink 是否强制清除窗口末尾的窗口状态对象?,apache-flink,flink-streaming,Apache Flink,Flink Streaming,我使用windowapi将数据划分为1小时的窗口。 在每个窗口中,我使用一个值状态为每个窗口存储一个布尔值 .assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor<Event>(Time.days(1)) { @Override public long extractTimestamp(Event element) { return element.times

我使用windowapi将数据划分为1小时的窗口。 在每个窗口中,我使用一个值状态为每个窗口存储一个布尔值

.assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor<Event>(Time.days(1)) {
    @Override
    public long extractTimestamp(Event element) {
        return element.timestamp;
    }
})

// Partition by user
.keyBy(new KeySelector<Event, Tuple2<Long, String>>() {
    @Override
    public Tuple2<Long, String> getKey(Event value) {
        return Tuple2.of(value.userGroup, value.userName);
    }
})

.window(TumblingEventTimeWindows.of(Time.minutes(60), Time.minutes(0)))
.allowedLateness(Time.days(1))
.trigger(new WindowTrigger<>(EVENTS_THRESHOLD))
.aggregate(new WindowAggregator(), new WindowProcessor())

.print();
.assignTimestampsAndWatermarks(新的BoundedAutofordernessTimestampExtractor(Time.days(1)){
@凌驾
公共长提取时间戳(事件元素){
返回元素。时间戳;
}
})
//按用户划分
.keyBy(新的KeySelector(){
@凌驾
公共元组2 getKey(事件值){
返回Tuple2.of(value.userGroup,value.userName);
}
})
.window(TumblingEventTimeWindows.of(Time.minutes(60),Time.minutes(0)))
.允许迟到(时间天数(1))
.trigger(新窗口触发器(事件\阈值))
.aggregate(新的WindowAggregator(),新的WindowProcessor())
.print();
公共类WindowProcessor扩展了ProcessWindowFunction{
私有最终值StateDescriptor windowAlertedDescriptor=新值StateDescriptor(“windowAlerted”,Boolean.class);
@凌驾
公共void进程(Tuple2键、上下文、Iterable元素、收集器输出)抛出异常{
long currentDownloadsCount=elements.iterator().next();
long windowStart=context.window().getStart();
long windowEnd=context.window().getEnd();
ValueState windowAlertedState=context.WindowsState().getState(windowAlertedDescriptor);
if(BooleanUtils.isTrue(windowAlertedState.value()){
返回;
}
我是否必须调用“clear()”方法来清理窗口状态数据? 我假设因为Flink处理窗口创建和清除,所以它在清除窗口时也应该处理状态清除

根据这里的答案 一旦窗口启动,窗口将自动清除其状态

但是Flink文档明确指出,您应该调用clear方法来删除窗口状态
窗口API中涉及的各种类在许多地方保持状态:

  • 分配给每个
    窗口的流记录列表
  • Trigger
    可以是有状态的(例如
    CountTrigger
  • 每个窗口状态(在
    ProcessWindowFunction.Context
    中)
  • 全局状态(也在
    ProcessWindowFunction.Context
    中)
清除窗口时,前两个(窗口内容和触发器状态)由Flink自动清除。清除窗口时,Flink还调用
ProcessWindowFunction
上的
clear
方法,您应该清除在
KeyedStateStore WindowsState()中创建的每个窗口状态
此时

另一方面,
KeyedStateStore globalState()
的目的是记住从一个窗口到另一个窗口的内容,因此您不会清除这些内容。但是,如果您有一个无界的密钥空间,您应该注意清除全局窗口状态中过时的密钥。唯一的方法是在状态描述符上指定为了全球国家

public class WindowProcessor extends ProcessWindowFunction<Long, String, Tuple2<Long, String>, TimeWindow> {

    private final ValueStateDescriptor<Boolean> windowAlertedDescriptor = new ValueStateDescriptor<>("windowAlerted", Boolean.class);

    @Override
    public void process(Tuple2<Long, String> key, Context context, Iterable<Long> elements, Collector<String> out) throws Exception {
        long currentDownloadsCount = elements.iterator().next();
        long windowStart = context.window().getStart();
        long windowEnd = context.window().getEnd();

        ValueState<Boolean> windowAlertedState = context.windowState().getState(windowAlertedDescriptor);
        if (BooleanUtils.isTrue(windowAlertedState.value())) {
            return;
        }