PartitionerMapper:
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class PartitionerMapper extends Mapper<LongWritable,Text,Text,NullWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
/**
* 通过NullWritable.get() 获取一个空值
*/
context.write(value,NullWritable.get());
}
}
PartitionerOwn:
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
public class PartitionerOwn extends Partitioner<Text,NullWritable> {
/**
* 这个方法就是我们自己来定义如何分区
* @param text key2那一行数据
* @param nullWritable v2
* @param i reduceTask的数量
* @return
*/
@Override
public int getPartition(Text text, NullWritable nullWritable, int i) {
String[] split = text.toString().split("\t");
String gameResult = split[5];
if(null != gameResult && "" != gameResult){
//判断如果开奖结果大于15,那么这些大于15的数据,都去到0号分区里面
if(Integer.parseInt(gameResult) >15){
return 0;
}else{
//开奖结果小于等于15的数据,都去往一号分区里面去
return 1;
}
}
return 0;
}
}
PartitionerReducer:
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class PartitionerReducer extends Reducer<Text,NullWritable,Text,NullWritable> {
/**
* reduce阶段不做任何处理,直接把我们的数据给输出
* @param key
* @param values
* @param context
* @throws IOException
* @throws InterruptedException
*/
@Override
protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
context.write(key,NullWritable.get());
}
}
PartitionerMain:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class PartitionerMain extends Configured implements Tool{
@Override
public int run(String[] args) throws Exception {
//获取一个job类,用于组装我们的MR任务
Job job = Job.getInstance(super.getConf(), PartitionerMain.class.getSimpleName());
//打包运行必须要的
job.setJarByClass(PartitionerMain.class);
//第一步:读取文件,解析成key,value对
job.setInputFormatClass(TextInputFormat.class);
//注意,分区的案例,不能使用本地模式运行 使用file:///不好使报错,只能打包到集群上面去运行
TextInputFormat.addInputPath(job,new Path(args[0]));
//第二步:自定义map逻辑
job.setMapperClass(PartitionerMapper.class);
//设置我们key2 value2 的类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class);
//第三步:设置我们的分区类,使用我们自定义的分区类来进行分区
job.setPartitionerClass(PartitionerOwn.class);
//第四步:排序 第五步:规约 第六步:分组
//第七步:自定义reduce逻辑
job.setReducerClass(PartitionerReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
//设置我们reduceTask的个数
job.setNumReduceTasks(2);
//第八步:设置输出类
job.setOutputFormatClass(TextOutputFormat.class);
//我们的输出路径不写死,通过参数传递进来
TextOutputFormat.setOutputPath(job,new Path(args[1]));
//提交任务
boolean b = job.waitForCompletion(true);
return b?0:1;
}
public static void main(String[] args) throws Exception {
int run = ToolRunner.run(new Configuration(), new PartitionerMain(), args);
System.exit(run);
}
}
读取的数据:
1 0 1 2017-07-31 23:10:12 837255 6 4+1+1=6 小,双 0 0.00 0.00 1 0.00 1 1
2 0 1 2017-07-31 23:15:03 837256 14 4+7+3=14 大,双 0 0.00 0.00 1 0.00 4 1
3 0 1 2017-07-31 23:20:12 837257 17 6+9+2=17 大,单 0 0.00 0.00 1 0.00 3 1
4 0 1 2017-07-31 23:25:12 837258 22 5+8+9=22 大,双 0 0.00 0.00 1 0.00 2 1
5 0 1 2017-07-31 23:30:18 837259 1 1+0+0=1 小,单 0 0.00 0.00 1 0.00 2 1
6 0 2 2017-07-31 23:17:22 2170779 4 2+0+2=4 小,双 0 0.00 0.00 1 0.00 2 1
输出的数据:根据读出的数据的索引5,大于15的数据放在一个文件,小于等于15的数据放在一个文件。