FLink13--KeyByMaxByApp
一、依赖
https://www.cnblogs.com/robots2/p/16048648.html
二、代码
package net.xdclass.class9;
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 net.xdclass.model.VideoOrder;
/**
* @desc reduce算子,和sum类似,sum做简单聚合,reduce做复杂聚合
* aggregate支持更复杂聚合
*
* max和maxBy,max存在一些坑,max不确定哪个key被选中了。并行度不为1的话,可能出现错误
*
* @menu
*/
public class FLink13KeyByMaxByApp {
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.fromElements(
new VideoOrder("1", "springboot", 10,1001, new Date()),
new VideoOrder("2", "springboot", 11,1005, new Date()),
new VideoOrder("3", "springboot", 12,1009, new Date()),
new VideoOrder("4", "微服务SpringCloud", 20,1010, new Date()),
new VideoOrder("5", "微服务SpringCloud", 21,1011, new Date()),
new VideoOrder("6", "Redis教程", 30,1001, new Date()),
new VideoOrder("7", "Redis教程", 31,1020, new Date()),
new VideoOrder("8", "Redis教程", 32,1030, new Date()),
new VideoOrder("9", "Linux教程", 40,1001, new Date()));
//根据title做分组
KeyedStream<VideoOrder, Object> keyByDs = ds.keyBy(new KeySelector<VideoOrder, Object>() {
@Override
public Object getKey(VideoOrder videoOrder) throws Exception {
return videoOrder.getTitle();
}
});
SingleOutputStreamOperator<VideoOrder> moneyMaxBy = keyByDs.maxBy("money");
moneyMaxBy.print();
//DataStream需要调用execute,可以取个名称
env.execute("money map job");
}
}