FLink09的RichFlatMap和RichMap使用
一、数据源配置
pom文件:https://www.cnblogs.com/robots2/p/16048648.html
二、RichFlatMap代码,输入单行输出多行
package net.xdclass.class9.app;
import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;
import net.xdclass.class6.source.VideoOrderSource;
import net.xdclass.model.VideoOrder;
/**
* @desc map算子
* @menu
*/
public class FLink09RichFlatMapApp {
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(2);
// //设置为自定义source
// DataStream<VideoOrder> ds = env.addSource(new VideoOrderSource());
// //转换transformation,标题转成1
// DataStream<Tuple2<String,Integer>> mapDS = ds.map(new MapFunction<VideoOrder, Tuple2<String,Integer>>() {
// @Override
// public Tuple2<String, Integer> map(VideoOrder videoOrder) throws Exception {
// return new Tuple2<>(videoOrder.getTitle(),1);
// }
// });
//设置为固定数据源
DataStreamSource<String> ds = env.fromElements("test1,test2", "test3,test4", "test5,test6");
//String,String是入参类型和出参类型,RichFlatMapFunction 比FlatMapFunction多些功能,
// 可以设置并行度,打开关闭资源
SingleOutputStreamOperator<String> flatMapDS = ds.flatMap(new RichFlatMapFunction<String, String>() {
@Override
public void open(Configuration parameters) throws Exception {
System.out.println("========open");
}
@Override
public void close() throws Exception {
System.out.println("========close");
}
@Override
public void flatMap(String s, Collector<String> out) throws Exception {
String[] arr = s.split(",");
for (String str:arr){
//放到收集器里面,这样可以发送给下个算子
System.out.println(str);
out.collect(str);
}
}
});
flatMapDS.print();
//DataStream需要调用execute,可以取个名称
env.execute("flat map job");
}
}
四、RichMap使用,输入单行输出单行
package net.xdclass.class9.app;
import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;
import net.xdclass.class6.source.VideoOrderSource;
import net.xdclass.model.VideoOrder;
/**
* @desc map算子
* @menu
*/
public class FLink09RichMapApp {
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(3);
//设置为自定义source
DataStream<VideoOrder> ds = env.addSource(new VideoOrderSource());
//用的RichMapFunction,比MapFunction多了打开关闭方法
DataStream<Tuple2<String,Integer>> mapDS = ds.map(new RichMapFunction<VideoOrder, Tuple2<String,Integer>>() {
@Override
public void open(Configuration parameters) throws Exception {
System.out.println("========open");
}
@Override
public void close() throws Exception {
System.out.println("========close");
}
@Override
public Tuple2<String, Integer> map(VideoOrder value) throws Exception {
return new Tuple2<>(value.getTitle(),1);
}
});
mapDS.print();
//DataStream需要调用execute,可以取个名称
env.execute("flat map job");
}
}