FLink14--核心窗口--TumblingWindiwApp
一、依赖
https://www.cnblogs.com/robots2/p/16048648.html
二、 代码
前言:window用作有keyBy情况,前面没有使用keyBy的话用windowAll,并行度会慢
package net.xdclass.class10;
import java.util.Date;
import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.ReduceFunction;
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.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingAlignedProcessingTimeWindows;
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 FLink14TumblingWindiwApp {
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设置
DataStream<VideoOrder> sumDs = keyByDs
.window(TumblingProcessingTimeWindows.of(Time.seconds(6)))
.sum("money");
sumDs.print();
//DataStream需要调用execute,可以取个名称
env.execute("Tumbling Window job");
}
}