Evictors
Flink的窗口模型允许除了WindowAssigner和Trigger之外还指定一个可选的Evictor。可以使用evictor(…)方法完成此操作。Evictors可以在触发器触发后,应用Window Function之前或之后从窗口中删除元素。
public interface Evictor<T, W extends Window> extends Serializable {
/**
* Optionally evicts elements. Called before windowing function.
*
* @param elements The elements currently in the pane.
* @param size The current number of elements in the pane.
* @param window The {@link Window}
* @param evictorContext The context for the Evictor
*/
void evictBefore(Iterable<TimestampedValue<T>> elements, int size, W window, EvictorContext evictorContext);
/**
* Optionally evicts elements. Called after windowing function.
*
* @param elements The elements currently in the pane.
* @param size The current number of elements in the pane.
* @param window The {@link Window}
* @param evictorContext The context for the Evictor
*/
void evictAfter(Iterable<TimestampedValue<T>> elements, int size, W window, EvictorContext evictorContext);
/**
* A context object that is given to {@link Evictor} methods.
*/
interface EvictorContext {
/**
* Returns the current processing time.
*/
long getCurrentProcessingTime();
/**
* Returns the metric group for this {@link Evictor}. This is the same metric
* group that would be returned from {@link RuntimeContext#getMetricGroup()} in a user
* function.
*
* <p>You must not call methods that create metric objects
* (such as {@link MetricGroup#counter(int)} multiple times but instead call once
* and store the metric object in a field.
*/
MetricGroup getMetricGroup();
/**
* Returns the current watermark time.
*/
long getCurrentWatermark();
}
}
evictBefore()包含要在窗口函数之前应用的剔除逻辑,而evictAfter()包含要在窗口函数之后应用的剔除逻辑。应用窗口功能之前剔除的元素将不会被其处理。
Flink DataStream Window内置了三种剔除器: CountEvictor、DeltaEvictor、TimeEvictor。
- CountEvictor: 数量剔除器。在Window中保留指定数量的元素,并从窗口头部开始丢弃其余元素。
- DeltaEvictor: 阈值剔除器。计算Window中最后一个元素与其余每个元素之间的增量,丢弃增量大于或等于阈值的元素。
- TimeEvictor: 时间剔除器。保留Window中最近一段时间内的元素,并丢弃其余元素。
CountEvictor
1.窗口中数据总条数<=要保留的数据条数(maxCount),不剔除。
2.否则,从Window头部遍历并剔除。
private void evict(Iterable<TimestampedValue<Object>> elements, int size, EvictorContext ctx) {
if (size <= maxCount) {
//总条数<=要保留的数据条数(maxCount),不剔除
return;
} else {
//否则&#