Flink个人学习整理-Process侧输出和定时器篇(七)
一、侧输出流demo
public class Flink_Process_SideOutPut {
public static void main(String[] args) throws Exception {
// 获取运行时环境
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
SingleOutputStreamOperator<Sensor> localhostDS = env.socketTextStream("localhost", 9999)
.map(new MapFunction<String, Sensor>() {
@Override
public Sensor map(String value) throws Exception {
String[] strings = value.split(",");
return new Sensor(
strings[0],
Long.parseLong(strings[1]),
Integer.parseInt(strings[2])
);
}
});
// 使用Process将数据分流
SingleOutputStreamOperator<Sensor> process = localhostDS.process(new SplitProcessFunction());
// 打印数据
process.print("主流");
process.getSideOutput(new OutputTag<Tuple2<String,Integer>>("side"){}).print("process侧输出流");
env.execute();
}
public static class SplitProcessFunction extends ProcessFunction<Sensor,Sensor>{
@Override
public void processElement(Sensor value, Context ctx, Collector<Sensor> out) throws Exception {
// 取出进行判断的字段数据
Integer vc = value.getVc();
// 判断数据,进行分流
if (vc >= 30){
out.collect(value);
}else {
ctx.output(new OutputTag<Tuple2<String,Integer>>("side"){},Tuple2.of(value.getId(),vc));
}
}
}
}
二、定时器
// 定时间只能用在KeyedStream
public class Flink_Process_OnTimer {
public static void main(String[] args) throws Exception {
// 获取运行时环境
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
KeyedStream<Sensor, String> localhostDS = env.socketTextStream("localhost", 9999)
.map(new MapFunction<String, Sensor>() {
@Override
public Sensor map(String value) throws Exception {
String[] strings = value.split(",");
return new Sensor(
strings[0],
Long.parseLong(strings[1]),
Integer.parseInt(strings[2])
);
}
})
.keyBy(new KeySelector<Sensor, String>() {
@Override
public String getKey(Sensor value) throws Exception {
return value.getId();
}
});
// 使用ProcessFunction定时器功能
localhostDS.process(new KeyedProcessFunction<String, Sensor, Sensor>() {
@Override
public void processElement(Sensor value, Context ctx, Collector<Sensor> out) throws Exception {
// 获取定时器服务,获取当前处理时间
long ts = ctx.timerService().currentProcessingTime();
System.out.println("当前处理时间:"+ts);
// 注册定时器 +5s
ctx.timerService().registerProcessingTimeTimer(ts + 5000L);
out.collect(value);
}
@Override
public void onTimer(long timestamp, OnTimerContext ctx, Collector<Sensor> out) throws Exception {
// timestamp:定时器触发时间 ctx:侧输出流 out:主流
System.out.println("定时器触发时间:"+timestamp);
// 循环制定定时器
long ts = ctx.timerService().currentProcessingTime();
ctx.timerService().registerProcessingTimeTimer(ts + 5000L);
}
}).print();
env.execute();
}
}