1、CountEvictor
仅记录用户指定数量的元素,一旦窗口中的元素超过这个数量,多余的元素会从窗口缓存的开头移除。
2、DeltaEvictor
接收 DeltaFunction 和 threshold 参数,计算最后一个元素与窗口缓存中所有元素的差值,并移除差值大于或等于 threshold 的元素。
3、TimeEvictor
接收 interval 参数,以毫秒表示,它会找到窗口中元素的最大 timestamp max_ts,并移除比 max_ts - interval 小的所有元素。
注意:
Flink 不对窗口中元素的顺序做任何保证,即使 evictor 从窗口缓存的开头移除一个元素,这个元素也不一定是最先或者最后到达窗口的;
默认情况下,所有内置的 evictor 逻辑都在调用窗口函数前执行;
4、代码示例
package com.xu.flink.datastream.day09;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.functions.windowing.delta.DeltaFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.evictors.CountEvictor;
import org.apache.flink.streaming.api.windowing.evictors.DeltaEvictor;
import org.apache.flink.streaming.api.windowing.evictors.TimeEvictor;
import org.apache.flink.streaming.api.windowing.triggers.EventTimeTrigger;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import java.time.Duration;
/**
* Evictor 可以在 trigger 触发后、调用窗口函数之前或之后从窗口中删除元素
* evictBefore() 包含在调用窗口函数前的逻辑,在调用窗口函数之前被移除的元素不会被窗口函数计算
* evictAfter() 包含在窗口函数调用之后的逻辑
* <p>
* -默认情况下,所有内置的 evictor 逻辑都在调用窗口函数前执行-
* CountEvictor: 仅记录用户指定数量的元素,一旦窗口中的元素超过这个数量,多余的元素会从窗口缓存的开头移除。
* DeltaEvictor: 接收 DeltaFunction 和 threshold 参数,计算最后一个元素与窗口缓存中所有元素的差值,并移除差值大于或等于 threshold 的元素。
* TimeEvictor: 接收 interval 参数,以毫秒表示,它会找到窗口中元素的最大 timestamp `max_ts`,并移除比 `max_ts - interval` 小的所有元素。
* <p>
* 注意:Flink 不对窗口中元素的顺序做任何保证,即使 evictor 从窗口缓存的开头移除一个元素,这个元素也不一定是最先或者最后到达窗口的。
*/
public class _12_WindowDefaultEvictors {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStreamSource<String> input = env.socketTextStream("localhost", 8888);
// 测试时限制了分区数,生产中需要设置空闲数据源
env.setParallelism(2);
// 事件时间需要设置水位线策略和时间戳
SingleOutputStreamOperator<Tuple2<String, Long>> map = input.map(new MapFunction<String, Tuple2<String, Long>>() {
@Override
public Tuple2<String, Long> map(String input) throws Exception {
String[] fields = input.split(",");
return new Tuple2<>(fields[0], Long.parseLong(fields[1]));
}
});
SingleOutputStreamOperator<Tuple2<String, Long>> watermarks = map.assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple2<String, Long>>forBoundedOutOfOrderness(Duration.ofSeconds(0))
.withTimestampAssigner(new SerializableTimestampAssigner<Tuple2<String, Long>>() {
@Override
public long extractTimestamp(Tuple2<String, Long> input, long l) {
return input.f1;
}
}));
//1、CountEvictor: 仅记录用户指定数量的元素,一旦窗口中的元素超过这个数量,多余的元素会从窗口缓存的开头移除(默认)。
//1.1 doEvictAfter = false
//a,1718157600000
//b,1718157600000
//c,1718157600000
//
//a,1718157602000
//b,1718157602000
//c,1718157602000
//
//a,1718157604000
//b,1718157604000
//c,1718157604000
//
//a,1718157605001
//b,1718157605001
//
//Window 的开始和结束时间=>1718157600000-1718157605000
//2> a
//Window 的开始和结束时间=>1718157600000-1718157605000
//1> b
//Window 的开始和结束时间=>1718157600000-1718157605000
//1> c
//
//c,1718157605001
//
//1.2 doEvictAfter = true
//a,1718157600000
//b,1718157600000
//c,1718157600000
//
//a,1718157602000
//b,1718157602000
//c,1718157602000
//
//a,1718157604000
//b,1718157604000
//c,1718157604000
//
//a,1718157605001
//b,1718157605001
//
//Window 的开始和结束时间=>1718157600000-1718157605000
//2> a
//2> a
//2> a
//Window 的开始和结束时间=>1718157600000-1718157605000
//1> b
//1> b
//1> b
//Window 的开始和结束时间=>1718157600000-1718157605000
//1> c
//1> c
//1> c
//
//c,1718157605001
//
//2、DeltaEvictor:接收 DeltaFunction 和 threshold 参数,计算最后一个元素与窗口缓存中所有元素的差值,并移除差值大于或等于 threshold 的元素。
//a,1718157600000
//b,1718157600000
//c,1718157600000
//
//a,1718157602000
//b,1718157602000
//c,1718157602000
//
//a,1718157604000
//b,1718157604000
//c,1718157604000
//
//a,1718157605001-最后的元素
//b,1718157605001-最后的元素
//
//first=>(a,1718157600000),last=>(a,1718157604000)
//first=>(a,1718157602000),last=>(a,1718157604000)
//first=>(a,1718157604000),last=>(a,1718157604000)
//first=>(b,1718157600000),last=>(b,1718157604000)
//first=>(b,1718157602000),last=>(b,1718157604000)
//first=>(b,1718157604000),last=>(b,1718157604000)
//first=>(c,1718157600000),last=>(c,1718157604000)
//first=>(c,1718157602000),last=>(c,1718157604000)
//first=>(c,1718157604000),last=>(c,1718157604000)
//
//Window 的开始和结束时间=>1718157600000-1718157605000
//Window 的开始和结束时间=>1718157600000-1718157605000
//Window 的开始和结束时间=>1718157600000-1718157605000
//2> a
//2> a
//2> a
//c,1718157605001-最后的元素
//
//3、TimeEvictor:接收 interval 参数,以毫秒表示,它会找到窗口中元素的最大 timestamp `max_ts`,并移除比 `max_ts - interval` 小的所有元素
//a,1718157600000
//b,1718157600000
//c,1718157600000
//
//a,1718157602000
//b,1718157602000
//c,1718157602000
//
//a,1718157604000
//b,1718157604000
//c,1718157604000
//
//a,1718157605001
//b,1718157605001
//
//Window 的开始和结束时间=>1718157600000-1718157605000
//2> a
//Window 的开始和结束时间=>1718157600000-1718157605000
//1> b
//Window 的开始和结束时间=>1718157600000-1718157605000
//1> c
//
//c,1718157605001
watermarks.keyBy(e -> e.f0)
.window(TumblingEventTimeWindows.of(Duration.ofSeconds(5)))
.trigger(EventTimeTrigger.create())
.evictor(TimeEvictor.of(Duration.ofSeconds(1), false))
// .evictor(CountEvictor.of(1, true))
// .evictor(DeltaEvictor.of(1.0, new DeltaFunction<Tuple2<String, Long>>() {
// @Override
// public double getDelta(Tuple2<String, Long> first, Tuple2<String, Long> last) {
// System.out.println("first=>"+first+",last=>"+last);
// if(first.f0.startsWith("a") && last.f0.startsWith("a")){
// return 0.0;
// }
// return 2.0;
// }
// }, false))
.apply(new WindowFunction<Tuple2<String, Long>, String, String, TimeWindow>() {
@Override
public void apply(String s, TimeWindow timeWindow, Iterable<Tuple2<String, Long>> iterable, Collector<String> collector) throws Exception {
System.out.println("Window 的开始和结束时间=>" + timeWindow.getStart() + "-" + timeWindow.getEnd());
for (Tuple2<String, Long> tuple2 : iterable) {
collector.collect(tuple2.f0);
}
}
})
.print();
env.execute();
}
}
648

被折叠的 条评论
为什么被折叠?



