需求三: 手机号码分区
在需求一的基础上,继续完善,将不同的手机号分到不同的数据文件的当中去,需要自定义分区来实现,这里我们自定义来模拟分区,将以下数字开头的手机号进行分开
135 开头数据到一个分区文件
136 开头数据到一个分区文件
137 开头数据到一个分区文件
其他分区
自定义分区
package cn.learn.mapreduce_flowcount_partition;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
public class FlowPartition extends Partitioner<Text,FlowBean> {
@Override
public int getPartition(Text text, FlowBean flowBean, int i) {
//判断手机号以哪个数字开头然后返回不同的分区编号
if(text.toString().startsWith("135")){
return 0;
}else if(text.toString().startsWith("136")){
return 1;
}else if(text.toString().startsWith("137")){
return 2;
}else{
return 3;
}
}
}
作业运行添加分区设置
job.setPartitionerClass(FlowPartition.class);
修改输入输出路径, 并放入集群运行
TextInputFormat.addInputPath(job,new
Path("hdfs://node01:8020/partition_flow/"));
TextOutputFormat.setOutputPath(job,new
Path("hdfs://node01:8020/partition_out"));