Flink11--FliterAndKeyBy算子
一、导入依赖
参考本人下博客
二、代码
FLink11FilterApp.java
package net.xdclass.class9;
import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import net.xdclass.source.VideoOrderSourceV2;
import net.xdclass.model.VideoOrder;
/**
* @desc filter算子
* @menu
*/
public class FLink11FilterApp {
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());
SingleOutputStreamOperator<VideoOrder> filterDs = ds.filter(new FilterFunction<VideoOrder>() {
@Override
public boolean filter(VideoOrder videoOrder) throws Exception {
return videoOrder.getMoney() > 10;
}
});
// KeyedStream<VideoOrder, Object> videoKeyBy = filterDs.keyBy(new KeySelector<VideoOrder, Object>() {
// @Override
// public Object getKey(VideoOrder videoOrder) throws Exception {
// return videoOrder.getTitle();
// }
// });
// SingleOutputStreamOperator<VideoOrder> videoKeySum = videoKeyBy.sum("money");
SingleOutputStreamOperator<VideoOrder> moneyDs = filterDs.keyBy(new KeySelector<VideoOrder, Object>() {
@Override
public Object getKey(VideoOrder videoOrder) throws Exception {
return videoOrder.getTitle();
}
}).sum("money");
moneyDs.print();
//DataStream需要调用execute,可以取个名称
env.execute("keyBy map job");
}
}