• java flink之eventTime和window


    import org.apache.flink.api.common.functions.MapFunction;
    import org.apache.flink.api.common.functions.ReduceFunction;
    import org.apache.flink.api.java.tuple.Tuple;
    import org.apache.flink.api.java.tuple.Tuple2;
    import org.apache.flink.streaming.api.TimeCharacteristic;
    import org.apache.flink.streaming.api.datastream.DataStreamSource;
    import org.apache.flink.streaming.api.datastream.*;
    import org.apache.flink.streaming.api.environment.*;
    import org.apache.flink.streaming.api.functions.timestamps.*;
    import org.apache.flink.streaming.api.windowing.assigners.*;
    import org.apache.flink.streaming.api.windowing.time.Time;
    import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
    
    
    /**
     * # _*_ coding:utf-8 _*_
     * # Author:xiaoshubiao
     * # Time : 2020/5/21 14:37
     **/
    public class event_watermaker {
        public static void main(String[] args) throws Exception {
            StreamExecutionEnvironment executionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment();
            executionEnvironment.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
            DataStreamSource<String> localhost = executionEnvironment.socketTextStream("localhost", 9999);
            SingleOutputStreamOperator<String> stringSingleOutputStreamOperator = localhost.assignTimestampsAndWatermarks(
                    new BoundedOutOfOrdernessTimestampExtractor<String>(Time.milliseconds(0)) {
                        @Override
                        public long extractTimestamp(String s) {
                            System.out.println("event_time输出" +s);
                            return Integer.valueOf(s.split(" ")[0]);
                        }
                    }
            );
            KeyedStream<Tuple2<String, Integer>, Tuple> tuple2TupleKeyedStream = stringSingleOutputStreamOperator.map(
                    new MapFunction<String, Tuple2<String, Integer>>() {
                        @Override
                        public Tuple2<String, Integer> map(String s) throws Exception {
                            System.out.println("map转换输出" + s);
                            return new Tuple2<>(s.split(" ")[1], 1);
                        }
                    }
            ).keyBy(0);
            tuple2TupleKeyedStream.window(TumblingEventTimeWindows.of(Time.seconds(5))).reduce(
                    new ReduceFunction<Tuple2<String, Integer>>() {
                        @Override
                        public Tuple2<String, Integer> reduce(Tuple2<String, Integer> t0, Tuple2<String, Integer> t1) throws Exception {
    
                            return new Tuple2<String, Integer>(t0.f0,t0.f1+t1.f1);
                        }
                    }
            ).print();
            executionEnvironment.execute();
        }
    }
  • 相关阅读:
    从原生web组件到框架组件源码(二)
    从原生web组件到框架组件源码(一)
    拖拽滚动视图(一)
    SVG研究之路(一)下
    运算符
    编码
    格式化输出
    循环语句
    条件语句
    Python基础
  • 原文地址:https://www.cnblogs.com/7749ha/p/12935310.html
Copyright © 2020-2023  润新知