第2节 mapreduce深入学习:10、手机号码进行分区

mac2022-06-30  76

需求三:手机号码分区

在需求一的基础上,继续完善,将不同的手机号分到不同的数据文件的当中去,需要自定义分区来实现,这里我们自定义来模拟分区,将以下数字开头的手机号进行分开

135 开头数据到一个分区文件

136 开头数据到一个分区文件

137 开头数据到一个分区文件

138 开头数据到一个分区文件

139 开头数据到一个分区文件

其他分区

 

注意:

必须到集群上运行,否则报错:

java.lang.Exception: java.io.IOException: Illegal partition for 13726230503 (2) at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:406)Caused by: java.io.IOException: Illegal partition for 13726230503 (2)

 

代码(只列出需求一的基础上修改的部分):

FlowMain: TextInputFormat.addInputPath(job,new Path(args[0])); job.setPartitionerClass(FlowPartition.class); job.setNumReduceTasks(6); TextOutputFormat.setOutputPath(job,new Path(args[1])); // args[0]=/flowCount/in/data_flow.dat// args[1]=/flowCount/3partition// hadoop jar flowCount_partition.jar cn.itcast.demo3.flowCount.FlowMain /flowCount/in/data_flow.dat /flowCount/3partition FlowPartition: package cn.itcast.demo3.flowCount;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) { String num = text.toString(); if(num != null ){ if(num.startsWith("135")) return 0; else if(num.startsWith("136")) return 1; else if(num.startsWith("137")) return 2; else if(num.startsWith("138")) return 3; else if(num.startsWith("139")) return 4; else return 5; } return 5; }}

 

转载于:https://www.cnblogs.com/mediocreWorld/p/11026710.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)