ApplyAndWindowFunction

apply中的WindowFunction使用方法

.keyby(0,1)
.timeWindow(Time.seconds(60))
                            输入tuple4                                    输出tuple6
.apply(new WindowFunction<Tuple4<String, String, Double, Long>, Tuple6<String, String, Double, Double, 
 Tuple里面有keyby的数据 TimeWindow可以get出窗口开始和结束时间
Double, Long>, Tuple, TimeWindow>() {
                    @Override
                    具体的逻辑                                       此处是keyby进行分组后的一组数据
                    public void apply(Tuple tuple, TimeWindow window, Iterable<Tuple4<String, String, Double, Long>> input, Collector<Tuple6<String, String, Double, Double, Double, Long>> out) throws Exception {
                        将数据迭代出来进行逻辑处理
                        Iterator<Tuple4<String, String, Double, Long>> it = input.iterator();
                        List<Tuple4<String, String, Double, Long>> dataList = new ArrayList<>();

                        Long count = 0L;
                        Double sum = 0.0;
                        while (it.hasNext()) {
                            Tuple4<String, String, Double, Long> next = it.next();
                            sum += next.f2;
                            count++;
                            dataList.add(next);
                        }
                        Collections.sort(dataList, new Comparator<Tuple4<String, String, Double, Long>>() {
                            @Override
                            public int compare(Tuple4<String, String, Double, Long> o1, Tuple4<String, String, Double, Long> o2) {
                                return o2.f2.compareTo(o1.f2);
                            }
                        });

                        double avg = sum / count;
                        double max = dataList.get(0).f2;
                        double min = dataList.get(dataList.size() - 1).f2;

                        String devId = tuple.getField(0);
                        String metric = tuple.getField(1);
                         将数据发射输出
                        out.collect(Tuple6.of(devId, metric, max, min, avg, window.getStart()));

                    }
                });
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 3,694评论 0 4
  • JavaScript语言精粹 前言 约定:=> 表示参考相关文章或书籍; JS是JavaScript的缩写。 本书...
    微笑的AK47阅读 665评论 0 3
  •   引用类型的值(对象)是引用类型的一个实例。   在 ECMAscript 中,引用类型是一种数据结构,用于将数...
    霜天晓阅读 1,232评论 0 1
  • 函数和对象 1、函数 1.1 函数概述 函数对于任何一门语言来说都是核心的概念。通过函数可以封装任意多条语句,而且...
    道无虚阅读 4,968评论 0 5
  • 对象的创建与销毁 Item 1: 使用static工厂方法,而不是构造函数创建对象:仅仅是创建对象的方法,并非Fa...
    孙小磊阅读 2,186评论 0 3

友情链接更多精彩内容