1)需求
要求每个省份手机号输出的文件中按照总流量内部排序。
数据准备
13926251106 240 0 240
13826544101 264 0 264
13480253104 180 180 360
13926435656 132 1512 1644
15989002119 1938 180 2118
18211575961 1527 2106 3633
13602846565 1938 2910 4848
84138413 4116 1432 5548
15920133257 3156 2936 6092
13922314466 3008 3720 6728
15013685858 3659 3538 7197
13660577991 6960 690 7650
13560439658 2034 5892 7926
18320173382 9531 2412 11943
13726230503 2481 24681 27162
13560436666 3597 25635 29232
13925057413 11058 48243 59301
13502468823 7335 110349 117684
2)分析:
基于需求3,增加自定义分区类即可。
3)案例实操
(1)增加自定义分区类
package com.lzz.mapreduce.flowsort;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
public class FlowSortProvincePartitioner extends Partitioner<FlowBean, Text>{
@Override
public int getPartition(FlowBean key, Text value, int numPartitions) {
//1获取手机号码
String preNum=value.toString().substring(0,3);
int partition=4;
if ("136".equals(preNum)) {
partition=0;
} else if("137".equals(preNum)){
partition=1;
}else if("138".equals(preNum)){
partition=2;
}else if("139".equals(preNum)){
partition=3;
}
return partition;
}
}
(2)在驱动类中添加分区类
运行结果