FLink16--计数窗口--CountWindiwApp
一、依赖
https://www.cnblogs.com/robots2/p/16048648.html
二、代码
概念:窗口来多少条计算一次,存在滚动和滑动两种情况
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.time.Time;
import net.xdclass.model.VideoOrder;
import net.xdclass.source.VideoOrderSourceV2;
/**
* @desc 窗口来多少条计算一次,存在滚动和滑动两种情况
* @menu
*/
public class FLink16CountWindowApp {
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();
}
});
//count window,分组后,组内超过6个则进行一次统计。比如java课程数超过6个触发一次java课程的计算
//分为滚动和滑动方式
//滚动方式,分组后组内数据超过5个则触发
// DataStream<VideoOrder> sumDs = keyByDs
// .countWindow(6)
// .sum("money");
//滑动方式,分组后组内数据超过3个则触发,统计过去5个的数据
DataStream<VideoOrder> sumDs = keyByDs
.countWindow(5,3)
.sum("money");
sumDs.print();
//DataStream需要调用execute,可以取个名称
env.execute("Sailing Window job");
}
}