FLink17--聚合函数-AggWindowApp
一、依赖
二、代码
package net.xdclass.class11;
import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.AggregateFunction;
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.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import net.xdclass.model.VideoOrder;
import net.xdclass.source.VideoOrderSourceV2;
/**
* 增量聚合 aggregate(new AggregateFunction){});
* @desc Agg增量聚合函数,把窗口数据和存储器数据累加,不能获取全窗口数据
* 逻辑复杂的话需要用这种方式对数据做加工,简单的话直接用基本算子就可以
* @menu
*/
public class FLink17AggWindowApp {
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();
}
});
DataStream<VideoOrder> aggregate = keyByDs.window(
TumblingProcessingTimeWindows.of(Time.seconds(5)))
//输入,聚合结果,输出
.aggregate(new AggregateFunction<VideoOrder, VideoOrder, VideoOrder>() {
//初始化累加器,每批都会初始化对象
@Override
public VideoOrder createAccumulator() {
// System.out.println("初始化对象");
VideoOrder videoOrder = new VideoOrder();
return videoOrder;
}
//videoOrder存储累加内容,value新数据
@Override
public VideoOrder add(VideoOrder value, VideoOrder videoOrder) {
videoOrder.setMoney(value.getMoney() + videoOrder.getMoney());
videoOrder.setTitle(value.getTitle());
if (videoOrder.getCreateTime() == null) {
videoOrder.setCreateTime(value.getCreateTime());
}
if (videoOrder.getTitle() == null) {
videoOrder.setTitle(value.getTitle());
}
return videoOrder;
}
//获取结果
@Override
public VideoOrder getResult(VideoOrder videoOrder) {
return videoOrder;
}
//合并操作,可以不写
@Override
public VideoOrder merge(VideoOrder a, VideoOrder b) {
VideoOrder videoOrder = new VideoOrder();
videoOrder.setMoney(a.getMoney() + b.getMoney());
videoOrder.setTitle(a.getTitle());
return videoOrder;
}
});
aggregate.print();
//DataStream需要调用execute,可以取个名称
env.execute("Sailing Window job");
}
}