MapReduce 重要组件——Partitioner组件

(1)Partitioner组件可以让Map对Key进行分区,从而可以根据不同的key来分发到不同的reduce中去处理;
(2)你可以自定义key的一个分发股则,如数据文件包含不同的省份,而输出的要求是每个省份输出一个文件;
(3)提供了一个默认的HashPartitioner。

自定义Partitioner:
(1)继承抽象类Partitioner,实现自定义的getPartition()方法;
(2)通过job.setPartitionerClass()来设置自定义的Partitioner;

Partitioner类见org.apache.hadoop.mapreduce.Partitioner类。

Partitioner应用场景:
需求:分别统计每种商品的周销售情况
site1的周销售清单( a.txt):
shoes     20
hat       10
stockings 30
clothes   40
......

site2的周销售清单( b.txt):
shoes     15
hat       1
stockings 90
clothes   80
......

汇总结果:
shoes     35
hat       11
stockings 120
clothes   120

新建项目TestPartitioner,包com.Partitioner,
源代码MyMapper.java:
package com.partitioner;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class MyMapper extends Mapper {

@Override
protected void map(LongWritable key, Text value,Contextcontext)
throws IOException, InterruptedException {
String[] s = value.toString().split("\\s+");
context.write(new Text(s[0]), newIntWritable(Integer.parseInt(s[1])));
}

}


源代码MyReduer.java:
package com.partitioner;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class MyReduer extends Reducer {

@Override
protected void reduce(Text key, Iterable value,Contextcontext)
throws IOException, InterruptedException {
int sum = 0;
for(IntWritable val:value){
sum += val.get();
}
context.write(key, new IntWritable(sum));
}

}


源代码MyPartitioner.java:
package com.partitioner;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

public class MyPartitioner extends Partitioner {

@Override
public int getPartition(Text key, IntWritable value, intnumPartitons) {
        if(key.toString().equals("shoes"))  // 转发给4个不同的reducer
        return 0;
        if(key.toString().equals("hat"))
        return 1;
        if(key.toString().equals("stockings"))
        return 2;
return 3;
}
   
}


源代码TestPartitioner.java:
package com.partitioner;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
importorg.apache.hadoop.mapreduce.lib.input.FileInputFormat;
importorg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;


public class TestPartitioner {
    public static voidmain(String[] args) throws IOException, ClassNotFoundException,InterruptedException{
  Configuration conf = newConfiguration();
    String[] otherArgs = newGenericOptionsParser(conf, args).getRemainingArgs();
    if (otherArgs.length !=2) {
      System.err.println("Usage: wordcount ");
      System.exit(2);
    }
    Job job = new Job(conf,"word count");
    job.setJarByClass(TestPartitioner.class);
    job.setMapperClass(MyMapper.class);
    job.setReducerClass(MyReduer.class);
    job.setPartitionerClass(MyPartitioner.class);
    job.setNumReduceTasks(4);// 设置4个reducer
   
   
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job, newPath(otherArgs[0]));
    FileOutputFormat.setOutputPath(job, newPath(otherArgs[1]));
    System.exit(job.waitForCompletion(true) ? 0 :1);
    }
}

将此项目打包为TestPartitioner.jar,并将上面提到的a.txt和b.txt上传到hdfs上的/input文件夹下,
运行TestPartitioner.jar:




可以看到四个reducer产生的文件:



其中内容,可见统计结果分开了:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值