自定义分区实现:
抽象类
要自定义分区规则,就必须继承并且重写。
设置分区数量
driver类:
java
job.setNumReduceTasks(3);
job.setPartitionerClass(FlowPartitioner.class);
分区方法类,直接用编号代替就好了
java
public class FlowPartitioner extends Partitioner<Text, FlowBean> {
@Override
public int getPartition(Text text, FlowBean flowBean, int numPartitions) {
int partitions;
String phoneNum = text.toString();
if(phoneNum.startsWith("136")){
partitions = 0;
} else if (phoneNum.startsWith("137")) {
partitions = 1;
}else if (phoneNum.startsWith("138")) {
partitions = 2;
}else if (phoneNum.startsWith("139")) {
partitions = 3;
}else {
partitions = 4;
}
return partitions;
}
}