Flink会话窗口:计算事件并触发特定事件计数

时间:2018-03-29 07:43:09

标签: apache-flink flink-streaming

当窗口中有5个事件时,我正在使用自定义触发器来触发。这适用于TumblingEventTimeWindow和SlidingTimeWindow,因为它们都有一个固定的窗口开始和结束。但是逻辑不适用于Session Windows,因为每个事件都会导致创建一个窗口并在以后合并。我正在使用缩减状态来计算事件。

如果会话窗口必须计算事件,如何处理这种情况?

代码要点: https://gist.github.com/thepythonista/4ad2f8c41f56aaea6ebf13fd9392c4bc

其他问题: 我能够在OnMergeContext的mergePartitionState方法中使用ReducingStateDescriptor。事件被正确计算。但是当我尝试将mergePartitionState方法用于ValueStateDescriptor时,会抛出编译时错误

ReducingStateDescriptor<Long> eventCounterDescriptor = new ReducingStateDescriptor<>("COUNT", new Sum(), LongSerializer.INSTANCE);
ValueStateDescriptor<Boolean> exitEventDescriptor = new ValueStateDescriptor<>("EXIT_EVENT", Boolean.class);

@Override
public void onMerge(TimeWindow window, OnMergeContext ctx) {
    ctx.mergePartitionedState(reducingStateDescriptor); // can do this
    ctx.mergePartitionedState(valueStateDescriptor); // won't compile
    ctx.registerEventTimeTimer(window.maxTimestamp());
}


Error:(133, 16) java: method mergePartitionedState in interface org.apache.flink.streaming.api.windowing.triggers.Trigger.OnMergeContext cannot be applied to given types;
  required: org.apache.flink.api.common.state.StateDescriptor<S,?>
  found: org.apache.flink.api.common.state.ValueStateDescriptor<java.lang.Boolean>
  reason: inference variable S has incompatible bounds
    equality constraints: org.apache.flink.api.common.state.ValueState<java.lang.Boolean>
    upper bounds: org.apache.flink.api.common.state.MergingState<?,?>

1 个答案:

答案 0 :(得分:1)

我所要做的就是调用OnMergeContext的mergePartitionedState函数。更新了要点。

@Override
public void onMerge(TimeWindow window, OnMergeContext ctx) {
    ctx.mergePartitionedState(reducingStateDescriptor);
    ctx.registerEventTimeTimer(window.maxTimestamp());
}