FLink15--滑动窗口--SlidingWindiwApp
一、依赖
https://www.cnblogs.com/robots2/p/16048648.html
二、代码
概念:滑动窗口有两个参数,窗口大小20和滑动大小6,6秒滑一次,统计近20秒数据
前言:滚动窗口一个时间传参,滑动窗口两个时间传参
window算子用做有keyBy情况,没有使用keyBy做分组的话用windowAll
package net.xdclass.class10;
import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import net.xdclass.model.VideoOrder;
import net.xdclass.source.VideoOrderSourceV2;
/**
* @desc 滑动窗口
* @menu
*/
public class FLink15SlidingWindiwApp {
public static void main(String[] args) throws Exception{
//WebUi方式运行
// final StreamExecutionEnvironment env =
// StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//设置运行模式为流批一体
env.setRuntimeMode(RuntimeExecutionMode.AUTOMATIC);
//并行度
env.setParallelism(1);
//设置为自定义source
// DataStream<VideoOrder> ds = env.addSource(new VideoOrderSourceV2());
DataStream<VideoOrder> ds = env.addSource(new VideoOrderSourceV2());
KeyedStream<VideoOrder, Object> keyByDs = ds.keyBy(new KeySelector<VideoOrder, Object>() {
@Override
public Object getKey(VideoOrder videoOrder) throws Exception {
return videoOrder.getTitle();
}
});
//window用做有keyBy情况,没有的话用windowAll
//新加个滚动窗口,做5秒开窗。其它字段为创建最早的信息
//滚动和滑动都使用window设置,滑动窗口有两个参数,窗口大小20和滑动大小6,6秒滑一次
//6秒滑一次,统计近20秒数据
DataStream<VideoOrder> sumDs = keyByDs
.window(SlidingProcessingTimeWindows.of(Time.seconds(20),Time.seconds(6)))
.sum("money");
sumDs.print();
//DataStream需要调用execute,可以取个名称
env.execute("Sailing Window job");
}
}