博客园 首页 私信博主 显示目录 隐藏目录 管理 动画

练习 : Flink 水位线 Flink_Watermark_Test

 1 import org.apache.flink.api.common.functions.MapFunction;
 2 import org.apache.flink.streaming.api.TimeCharacteristic;
 3 import org.apache.flink.streaming.api.datastream.DataStreamSource;
 4 import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
 5 import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
 6 import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
 7 import org.apache.flink.streaming.api.windowing.time.Time;
 8 
 9 public class Flink_Watermark_Test {
10     public static void main(String[] args) {
11             StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
12             env.setParallelism(1);
13             //时间语义:EvenTime
14             env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
15             //套接字
16             DataStreamSource<String> stream = env.socketTextStream("hadoop106", 9999);
17             //封装成传感器对象
18             SingleOutputStreamOperator<Sensor> map = stream.map(new MapFunction<String, Sensor>() {
19                 @Override
20                 public Sensor map(String s) throws Exception {
21                     String[] split = s.split(",");
22                     return new Sensor(split[0], Long.valueOf(split[1]), Double.valueOf(split[2]));
23                 }
24             });
25             //水位线允许3s延迟
26             SingleOutputStreamOperator<Sensor> datas = map.assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor<Sensor>(Time.seconds(3)) {
27                 @Override
28                 public long extractTimestamp(Sensor sen) {
29                     return sen.getSensor_timeStamp() ;
30                 }
31             });
32             //滑动窗口 大小 10s 步长 5s 输出结果
33             datas.keyBy("sensor_id").timeWindow(Time.seconds(10),Time.seconds(5)).sum("sensor_Temp").print();
34 
35             try {
36                 env.execute();
37             } catch (Exception e) {
38                 e.printStackTrace();
39             }
40         }
41     }

 

posted @ 2022-03-04 21:41  CHANG_09  阅读(52)  评论(0编辑  收藏  举报