温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

flink中如何使用set实时计算当天网站uv

发布时间:2021-11-09 18:53:13 阅读:277 作者:柒染 栏目:大数据
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

flink中如何使用set实时计算当天网站uv,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

背景

对于web网站,我们一般会有这样的需求,实时的计算出来当天网站的uv,尽可能快的展示出来。今天我们就讲一下基于java的set集合做一下实时uv的统计。

简易需求:

  • 实时计算出当天零点截止到当前时间各个端(android,ios,h6)下的uv
  • 每秒钟更新一次统计结果
 

案例讲解

 

模拟source

首先我们模拟生成一下最简单的数据,生成一个flink的二元组Tuple2.分别表示分类和用户id

 public static class MySource implements SourceFunction<Tuple2<String,Integer>>{  private volatile boolean isRunning = true;  String category[] = {"Android""IOS""H5"};  @Override  public void run(SourceContext<Tuple2<String,Integer>> ctx) throws Exception{   while (isRunning){    Thread.sleep(10);    //具体是哪个端的用户    String type = category[(int) (Math.random() * (category.length))];    //随机生成10000以内的int类型数据作为userid    int userid = (int) (Math.random() * 10000);    ctx.collect(Tuple2.of(type, userid));   }  }  @Override  public void cancel(){   isRunning = false;  } }
   

定义窗口

接下来我们定义一个周期是一天的滑动窗口,因为我们要每秒钟输出窗口的数据,所以我们紧接着窗口定义了一个1秒的触发器。

DataStream<Tuple2<String,Integer>> dataStream = env.addSource(new MySource());    dataStream.keyBy(0).window(TumblingProcessingTimeWindows.of(Time.days(1), Time.hours(-8)))              .trigger(ContinuousProcessingTimeTrigger.of(Time.seconds(1)))              .aggregate(new MyAggregate(),new WindowResult())              .print();
   

自定义聚合算子

接下来我们自定义一个聚合算子来实现该功能。

对于聚合算子的理解可以参考这个文章:

https://mp.weixin.qq.com/s/ZCWexNGzhSchRpxipa1x-g

 public static class MyAggregate   implements AggregateFunction<Tuple2<String,Integer>,Set<Integer>,Integer>{  @Override  public Set<IntegercreateAccumulator(){   return new HashSet<>();  }  @Override  public Set<Integeradd(Tuple2<String,Integer> value, Set<Integer> accumulator){   accumulator.add(value.f1);   return accumulator;  }  @Override  public Integer getResult(Set<Integer> accumulator){   return accumulator.size();  }  @Override  public Set<Integermerge(Set<Integer> a, Set<Integer> b){   a.addAll(b);   return a;  } }
   

处理输出结果

我们这里将结果输出到控制台,实际的生产中我们可以将数据写入redis或者hbase等。

1> Result{, dateTime='2020-06-21 19:23:30'type='IOS', uv=136}2> Result{, dateTime='2020-06-21 19:23:30'type='Android', uv=150}1> Result{, dateTime='2020-06-21 19:23:30'type='H5', uv=134}1> Result{, dateTime='2020-06-21 19:23:31'type='IOS', uv=164}2> Result{, dateTime='2020-06-21 19:23:31'type='Android', uv=177}1> Result{, dateTime='2020-06-21 19:23:31'type='H5', uv=167}2> Result{, dateTime='2020-06-21 19:23:32'type='Android', uv=205}1> Result{, dateTime='2020-06-21 19:23:32'type='IOS', uv=193}1> Result{, dateTime='2020-06-21 19:23:32'type='H5', uv=198}

关于flink中如何使用set实时计算当天网站uv问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://my.oschina.net/u/4596020/blog/4439373

AI

开发者交流群×