大数据-Hadoop生态(18)-MapReduce框架原理-WritableComparable排序和GroupingComparator分组

 1.排序概述

2.排序分类

 

3.WritableComparable案例

这个文件,是

13470253144    180    180    360 13509468723    7335    110349    117684 13560439638    918    4938    5856 13568436656    3597    25635    29232 13590439668    1116    954    2070 13630577991    6960    690    7650 13682846555    1938    2910    4848 13729199489    240    0    240 13736230513    2481    24681    27162 13768778790    120    120    240 13846544121    264    0    264 13956435636    132    1512    1644 13966251146    240    0    240 13975057813    11058    48243    59301 13992314666    3008    3720    6728 15043685818    3659    3538    7197 15910133277    3156    2936    6092 15959002129    1938    180    2118 18271575951    1527    2106    3633 18390173782    9531    2412    11943 84188413    4116    1432    5548
复制代码

字段含义分别为手机号,上行流量,下行流量,总流量

需求是根据总流量进行排序

 

Bean对象,需要实现序列化,反序列化和Comparable接口

复制代码
package com.nty.writableComparable;  import org.apache.hadoop.io.WritableComparable;  import java.io.DataInput; import java.io.DataOutput; import java.io.IOException;  /**  * author nty  * date time 2018-12-12 16:33  *//**  * 实现WritableComparable接口  * 原先将bean序列化时,需要实现Writable接口,现在再实现Comparable接口  *   * public interface WritableComparable<T> extends Writable, Comparable<T>  *   * 所以我们可以实现Writable和Comparable两个接口,也可以实现WritableComparable接口  */public class Flow implements WritableComparable<Flow> {    private long upflow;   private long downflow;   private long total;      public long getUpflow() {         return upflow;     }      public void setUpflow(long upflow) {         this.upflow = upflow;     }      public long getDownflow() {         return downflow;     }      public void setDownflow(long downflow) {         this.downflow = downflow;     }      public long getTotal() {         return total;     }      public void setTotal(long total) {         this.total = total;     }      //快速赋值    public void setFlow(long upflow, long downflow){         this.upflow = upflow;         this.downflow = downflow;         this.total = upflow + downflow;     }      @Override     public String toString() {         return upflow + "\t" + downflow + "\t" + total;     }      //重写compareTo方法    @Override     public int compareTo(Flow o) {         return Long.compare(o.total, this.total);     }      //
                        
关键字:
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信