Trigger determines when the window (formed by the window allocator) is ready to be processed by the window function. Each WindowAssigner comes with a default value Trigger. If the default trigger does not meet your needs, you can use trigger(…).
public abstract class Trigger<T, W extends Window> implements Serializable { /** 只要有元素落⼊到当前窗⼝, 就会调⽤该⽅法 * @param element 收到的元素 * @param timestamp 元素抵达时间. * @param window 元素所属的window窗口. * @param ctx ⼀个上下⽂对象,通常⽤该对象注册 timer(ProcessingTime/EventTime) 回调. */ public abstract TriggerResult onElement(T var1, long var2, W var4, Trigger.TriggerContext var5) throws Exception; /** * processing-time 定时器回调函数 * * @param time 定时器触发的时间. * @param window 定时器触发的窗口对象. * @param ctx ⼀个上下⽂对象,通常⽤该对象注册 timer(ProcessingTime/EventTime) 回调. */ public abstract TriggerResult onProcessingTime(long var1, W var3, Trigger.TriggerContext var4) throws Exception; /** * event-time 定时器回调函数 * * @param time 定时器触发的时间. * @param window 定时器触发的窗口对象. * @param ctx ⼀个上下⽂对象,通常⽤该对象注册 timer(ProcessingTime/EventTime) 回调. */ public abstract TriggerResult onEventTime(long var1, W var3, Trigger.TriggerContext var4) throws Exception; /** * 当 多个窗口合并到⼀个窗⼝的时候,调用该方法法,例如系统SessionWindow * * @param window 合并后的新窗口对象 * @param ctx ⼀个上下⽂对象,通常用该对象注册 timer(ProcessingTime/EventTime)回调以及访问状态 */ public void onMerge(W window, Trigger.OnMergeContext ctx) throws Exception { throw new UnsupportedOperationException("This trigger does not support merging."); } /** * 当窗口被删除后执⾏所需的任何操作。例如:可以清除定时器或者删除状态数据 */ public abstract void clear(W var1, Trigger.TriggerContext var2) throws Exception; }
public enum TriggerResult { // 表示对窗口不执行任何操作。即不触发窗口计算,也不删除元素。 CONTINUE(false, false), // 触发窗口计算,输出结果,然后将窗口中的数据和窗口进行清除。 FIRE_AND_PURGE(true, true), // 触发窗口计算,但是保留窗口元素 FIRE(true, false), // 不触发窗口计算,丢弃窗口,并且删除窗口的元素。 PURGE(false, true); private final boolean fire; private final boolean purge; private TriggerResult(boolean fire, boolean purge) { this.purge = purge; this.fire = fire; } public boolean isFire() { return this.fire; } public boolean isPurge() { return this.purge; } }
Once the trigger determines that the window is ready for processing, it will fire, and the return status can be FIRE or FIRE_AND_PURGE. Among them, FIRE triggers the window calculation and retains the window content, while FIRE_AND_PURGE triggers the window calculation and deletes the window content. By default, pre-implemented triggers simply FIRE without clearing the window state.
EventTimeTrigger: Determine whether to trigger window calculation by comparing EventTime with the Endtime of the window. If EventTime is greater than Window EndTime, it will trigger, otherwise it will not trigger. The window will continue to wait.
ProcessTimeTrigger: Determine whether to trigger the window by comparing ProcessTime and window EndTme. If ProcessTime is greater than EndTime, the calculation is triggered, otherwise the window continues to wait.
ContinuousEventTimeTrigger: Calculated based on the interval periodic trigger window or the end time of the Window is less than the current EndTime trigger window.
ContinuousProcessingTimeTrigger: Calculated based on the interval periodic trigger window or the end time of the Window is less than the current ProcessTime trigger window.
CountTrigger: Determine whether to trigger window calculation based on whether the amount of accessed data exceeds the set threshold.
DeltaTrigger: Determine whether to trigger window calculation based on whether the Delta indicator calculated from the access data exceeds the specified Threshold.
PurgingTrigger: Any trigger can be converted into a Purge type trigger as a parameter, and the data will be cleaned after the calculation is completed.
NeverTrigger: Do not trigger window calculation at any time
Mainly look at the source code of EventTimeTrigger and ProcessingTimeTrigger .
public class EventTimeTrigger extends Trigger<Object, TimeWindow> { private static final long serialVersionUID = 1L; private EventTimeTrigger() { } public TriggerResult onElement(Object element, long timestamp, TimeWindow window, TriggerContext ctx) throws Exception { if (window.maxTimestamp() <= ctx.getCurrentWatermark()) { return TriggerResult.FIRE; } else { ctx.registerEventTimeTimer(window.maxTimestamp()); return TriggerResult.CONTINUE; } } public TriggerResult onEventTime(long time, TimeWindow window, TriggerContext ctx) { return time == window.maxTimestamp() ? TriggerResult.FIRE : TriggerResult.CONTINUE; } public TriggerResult onProcessingTime(long time, TimeWindow window, TriggerContext ctx) throws Exception { return TriggerResult.CONTINUE; } public void clear(TimeWindow window, TriggerContext ctx) throws Exception { ctx.deleteEventTimeTimer(window.maxTimestamp()); } public boolean canMerge() { return true; } public void onMerge(TimeWindow window, OnMergeContext ctx) { long windowMaxTimestamp = window.maxTimestamp(); if (windowMaxTimestamp > ctx.getCurrentWatermark()) { ctx.registerEventTimeTimer(windowMaxTimestamp); } } public String toString() { return "EventTimeTrigger()"; } public static EventTimeTrigger create() { return new EventTimeTrigger(); } }
public class ProcessingTimeTrigger extends Trigger<Object, TimeWindow> { private static final long serialVersionUID = 1L; private ProcessingTimeTrigger() { } public TriggerResult onElement(Object element, long timestamp, TimeWindow window, TriggerContext ctx) { ctx.registerProcessingTimeTimer(window.maxTimestamp()); return TriggerResult.CONTINUE; } public TriggerResult onEventTime(long time, TimeWindow window, TriggerContext ctx) throws Exception { return TriggerResult.CONTINUE; } public TriggerResult onProcessingTime(long time, TimeWindow window, TriggerContext ctx) { return TriggerResult.FIRE; } public void clear(TimeWindow window, TriggerContext ctx) throws Exception { ctx.deleteProcessingTimeTimer(window.maxTimestamp()); } public boolean canMerge() { return true; } public void onMerge(TimeWindow window, OnMergeContext ctx) { long windowMaxTimestamp = window.maxTimestamp(); if (windowMaxTimestamp > ctx.getCurrentProcessingTime()) { ctx.registerProcessingTimeTimer(windowMaxTimestamp); } } public String toString() { return "ProcessingTimeTrigger()"; } public static ProcessingTimeTrigger create() { return new ProcessingTimeTrigger(); } }
In the onElement() method, ctx.registerProcessingTimeTimer(window.maxTimestamp()) will register a ProcessingTime timer, time The parameter is window.maxTimestamp(), which is the final time of the window. When the time reaches the final time of the window, the timer triggers and calls the onProcessingTime() method. In the onProcessingTime() method, return TriggerResult.FIRE returns FIRE, triggering the window. Calculation of data in the window, but window elements will be retained.
It should be noted that the ProcessingTimeTrigger class will only trigger the calculation of the window function when the final time of the window arrives. After the calculation is completed, the data in the window will not be cleared. The data is stored in memory unless PURGE is called. or FIRE_AND_PURGE, otherwise the data will always be in memory. In fact, none of the Trigger classes provided in Flink, except the PurgingTrigger class, will clear the data in the window.
TumblingEventTimeWindows :EventTimeTrigger public class TumblingEventTimeWindows extends WindowAssigner<Object, TimeWindow> { public Trigger<Object, TimeWindow> getDefaultTrigger(StreamExecutionEnvironment env) { return EventTimeTrigger.create(); } }
TumblingProcessingTimeWindows:ProcessingTimeTrigger
public class TumblingProcessingTimeWindows extends WindowAssigner<Object, TimeWindow> { public Trigger<Object, TimeWindow> getDefaultTrigger(StreamExecutionEnvironment env) { return ProcessingTimeTrigger.create(); } }
SlidingEventTimeWindows:EventTimeTrigger public class SlidingEventTimeWindows extends WindowAssigner<Object, TimeWindow> { public Trigger<Object, TimeWindow> getDefaultTrigger(StreamExecutionEnvironment env) { return EventTimeTrigger.create(); } }
SlidingProcessingTimeWindows:ProcessingTimeTrigger
public class SlidingProcessingTimeWindows extends WindowAssigner<Object, TimeWindow> { public Trigger<Object, TimeWindow> getDefaultTrigger(StreamExecutionEnvironment env) { return ProcessingTimeTrigger.create(); } }
EventTimeSessionWindows:EventTimeTrigger public class EventTimeSessionWindows extends MergingWindowAssigner<Object, TimeWindow> { public Trigger<Object, TimeWindow> getDefaultTrigger(StreamExecutionEnvironment env) { return EventTimeTrigger.create(); } }
ProcessingTimeSessionWindows: ProcessingTimeTrigger
public class ProcessingTimeSessionWindows extends MergingWindowAssigner<Object, TimeWindow> { public Trigger<Object, TimeWindow> getDefaultTrigger(StreamExecutionEnvironment env) { return ProcessingTimeTrigger.create(); } }
GlobalWindows :NeverTrigger public class GlobalWindows extends WindowAssigner<Object, GlobalWindow> { public Trigger<Object, GlobalWindow> getDefaultTrigger(StreamExecutionEnvironment env) { return new GlobalWindows.NeverTrigger(); } }
The above is the detailed content of How to use Java Flink window trigger Trigger. For more information, please follow other related articles on the PHP Chinese website!