在排序的时候,对每一次排序都是默认的按照key进行字典排序的,要实现自定义排序的方式,就是自定对象实现writablecomparable接口实现compareto方法,从而实现自定义排序。
如:
@Override
public int compareTo(FlowBean o) {
int result;
// 按照总流量大小,倒序排列
if (sumFlow > bean.getSumFlow()) {
result = -1;
}else if (sumFlow < bean.getSumFlow()) {
result = 1;
}else {
result = 0;
}
return result;
}
对手机号总流量实现全排序的实例:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CvjG1UM3-1603712991906)(https://s1.ax1x.com/2020/10/25/BewHc6.png)]
实例代码:
FlowBean
package com.mr.sort;
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 long upFlow;
private long downFlow;
private long sumFlow;
public FlowBean() {
super();//反射调用
}
public FlowBean(long upFlow, long downFlow) {
super();
this.upFlow = upFlow;
this.downFlow = downFlow;
sumFlow = upFlow + downFlow;
}
@Override
public int compareTo(FlowBean bean) {
int res;
//从大到小倒序排序
if (sumFlow>bean.getSumFlow()) {
res = -1;
} else if (sumFlow<bean.getSumFlow()) {
res = 1;
} else {
res = 0;
}
return res;
}
//序列化方法
@Override
public void write(DataOutput dataOutput) throws IOException {
dataOutput.writeLong(upFlow);
dataOutput.writeLong(downFlow);
dataOutput.writeLong(sumFlow);
}
//反序列化方法
@Override
public void readFields(DataInput dataInput) throws IOException {
upFlow = dataInput.readLong();
downFlow = dataInput.readLong();
sumFlow = dataInput.readLong();
}
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 getSumFlow() {
return sumFlow;
}
public void setSumFlow(long sumFlow) {
this.sumFlow = sumFlow;
}
@Override
public String toString() {
return upFlow + "\t" + downFlow + "\t" + sumFlow;
}
}
Mapper
package com.mr.sort;
import com.sun.tools.javac.comp.Flow;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;