美文网首页
Flink 窗口小案例, 统计一小时的pv和uv的访问量

Flink 窗口小案例, 统计一小时的pv和uv的访问量

作者: wudl | 来源:发表于2020-11-13 00:20 被阅读0次

    1. 统计一小时的pv 统计访问量

    1.1标题思路就是:[ 数据分类排序----->分类-----> 开窗一小时----->统计]

    1.2 代码如下:

    package com.wudl.examples;
    
    import com.wudl.bean.UserBehavior;
    import org.apache.flink.api.common.functions.MapFunction;
    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.KeyedStream;
    import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
    import org.apache.flink.streaming.api.datastream.WindowedStream;
    import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
    import org.apache.flink.streaming.api.functions.timestamps.AscendingTimestampExtractor;
    import org.apache.flink.streaming.api.windowing.time.Time;
    import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
    
    /**
     * @ClassName : PvHour
     * @Description : 一小时 页面的点击量是多少
     * 实现思路 - 先 设置wartemark 时间, 然后在进行开窗多久(例如一小时), 然后 对一小时中的数据进行统计
     * @Author :wudl
     * @Date: 2020-11-12 22:41
     */
    
    public class PvHour {
        public static void main(String[] args) throws Exception {
            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
            env.setParallelism(1);
            env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
            // 从文件中或者从kafka 中进行读取
            // -- 假如先从文件中读取
            SingleOutputStreamOperator<UserBehavior> operator = env.readTextFile("D:\\ideaWorkSpace\\learning\\Flinklearning\\wudl-flink-java\\input\\UserBehavior.csv").map(new MapFunction<String, UserBehavior>() {
                @Override
                public UserBehavior map(String s) throws Exception {
                    String[] datas = s.split(",");
                    return new UserBehavior(Long.valueOf(datas[0]), Long.valueOf(datas[1]), Integer.valueOf(datas[2]), datas[3], Long.valueOf(datas[4]));
                }
            })
                    //  设置watermark
                    .assignTimestampsAndWatermarks(new AscendingTimestampExtractor<UserBehavior>() {
                        @Override
                        public long extractAscendingTimestamp(UserBehavior element) {
                            // Flink 中都是毫秒 , 所以乘以1000L
                            return element.getTimestamp() * 1000L;
                        }
                    });
    
            // 实现pv 的统计
            // 转化为元祖
            SingleOutputStreamOperator<UserBehavior> userBehaviorFilter = operator.filter(data -> "pv".equals(data.getBehavior()));
            // 转换成 二元组 (pv,1)
            SingleOutputStreamOperator<Tuple2<String, Integer>> pvTuple = userBehaviorFilter.map(new MapFunction<UserBehavior, Tuple2<String, Integer>>() {
                @Override
                public Tuple2<String, Integer> map(UserBehavior userBehavior) throws Exception {
                    return Tuple2.of("pv", 1);
                }
            });
            // 按照第一个位置的元素 分组 => 聚合算子只能在分组之后调用,也就是 keyedStream才能调用 sum
            KeyedStream<Tuple2<String, Integer>, Tuple> tupleKeyedStream = pvTuple.keyBy(0);
            // 开窗
            WindowedStream<Tuple2<String, Integer>, Tuple, TimeWindow> windowedStream = tupleKeyedStream.timeWindow(Time.hours(1));
            // 求和
            SingleOutputStreamOperator<Tuple2<String, Integer>> sum = windowedStream.sum(1);
            // 打印
            sum.print();
            env.execute();
    
        }
    }
    

    1.3执行结果如下:

    QQ截图20201112235929.png

    相关文章

      网友评论

          本文标题:Flink 窗口小案例, 统计一小时的pv和uv的访问量

          本文链接:https://www.haomeiwen.com/subject/iuwmbktx.html