MapReduce- traffic statistics summation - sort -FlowBean write

Two requirements: upstream traffic reverse order (descending order)

Analysis, a demand to output data as input data sorting, custom FlowBean to FlowBean to map output key, mobile phone number as the output value Map, as key MapReduce program will sort the output stage Map

Step 1: define FlowBean achieve WritableComparable achieve more sorting

Java's compareTo method Description:

compareTo method parameters for the current object and method are compared.
If the parameter is equal to the specified number of 0 returns.
If the number is less than the specified parameters -1.
If the number is greater than the specified parameters returned.

For example: o1.compareTo (o2); returns a positive number, then the current object (the object invokes the compareTo method O1) to be the comparison target row (target parameter passing compareTo o2) behind returns negative, then, on the front

package cn.learn.mapreduce_sort;

import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class FlowBean implements WritableComparable<FlowBean>{
    private Integer upFlow;
    private Integer  downFlow;
    private Integer upCountFlow;
    private Integer downCountFlow;


    public Integer getUpFlow() {
        return upFlow;
    }

    public void setUpFlow(Integer upFlow) {
        this.upFlow = upFlow;
    }

    public Integer getDownFlow() {
        return downFlow;
    }

    public void setDownFlow(Integer downFlow) {
        this.downFlow = downFlow;
    }

    public Integer getUpCountFlow() {
        return upCountFlow;
    }

    public void setUpCountFlow(Integer upCountFlow) {
        this.upCountFlow = upCountFlow;
    }

    public Integer getDownCountFlow() {
        return downCountFlow;
    }

    public void setDownCountFlow(Integer downCountFlow) {
        this.downCountFlow = downCountFlow;
    }

    @Override
    public String toString() {
        return
                upFlow +
                        "\t" + downFlow +
                        "\t" + upCountFlow +
                        "\t" + downCountFlow;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeInt(upFlow);
        dataOutput.writeInt(downFlow);
        dataOutput.writeInt(upCountFlow);
        dataOutput.writeInt(downCountFlow);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.upFlow = dataInput.readInt();
        this.downFlow = dataInput.readInt();
        this.upCountFlow = dataInput.readInt();
        this.downCountFlow = dataInput.readInt();
    }

    @Override
    public int compareTo(FlowBean other) {
        return  this.getUpFlow().compareTo(other.getUpFlow()) * -1;  //按照上行数据包进行
排序
    }
}

 

Released 2209 original articles · won praise 50 · Views 150,000 +

Guess you like

Origin blog.csdn.net/Leon_Jinhai_Sun/article/details/104569119