MapReduce之自定义partitioner

partitioner定义:
partitioner的作用是将mapper(如果使用了combiner的话就是combiner)输出的key/value拆分为分片(shard),每个reducer对应一个分片。默认情况下,partitioner先计算key的散列值(通常为md5值)。然后通过reducer个数执行取模运算:key.hashCode%(reducer个数)。这种方式不仅能够随机地将整个key空间平均分发给每个reducer,同时也能确保不同mapper产生的相同key能被分发到同一个reducer。

目的:
如果对数据的整体有很好的了解,可以使用自定义Partitioner来达到reducer的负载均衡,提高效率。

适用范围:
需要非常注意的是:必须提前知道有多少个分区。比如自定义Partitioner会返回5个不同int值,而reducer number设置了小于5,那就会报错。所以我们可以通过运行分析任务来确定分区数。例如,有一堆包含时间戳的数据,但是不知道它能追朔到的时间范围,此时可以运行一个作业来计算出时间范围。

注意:
在自定义partitioner时一定要注意防止数据倾斜。

下面来看给wordCount加了partitioner的示例:
输入数据如下:
zhangsan lisi wangwu renliu
zhangsi liwu wangliu renqi
zhangwu liliu wangqi renba
package com.mr.partitioner;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configurable;
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;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import com.util.MRUtil;

public class PartitionerTest {

	public static class TokenizerMapper extends
			Mapper<Object, Text, Text, IntWritable> {

		private final static IntWritable one = new IntWritable(1);
		private Text word = new Text();

		public void map(Object key, Text value, Context context)
				throws IOException, InterruptedException {
			StringTokenizer itr = new StringTokenizer(value.toString());
			while (itr.hasMoreTokens()) {
				word.set(itr.nextToken());
				context.write(word, one);
			}
		}
	}

	public static class IntSumReducer extends
			Reducer<Text, IntWritable, Text, IntWritable> {
		private IntWritable result = new IntWritable();

		public void reduce(Text key, Iterable<IntWritable> values,
				Context context) throws IOException, InterruptedException {
			int sum = 0;
			for (IntWritable val : values) {
				sum += val.get();
			}
			result.set(sum);
			context.write(key, result);
		}
	}

	public static class MyPartitioner extends Partitioner<Text, IntWritable>
			implements Configurable {

		private Configuration conf = null;

		@Override
		public Configuration getConf() {
			return conf;
		}

		@Override
		public void setConf(Configuration conf) {
			this.conf = conf;
		}

		@Override
		public int getPartition(Text arg0, IntWritable arg1, int arg2) {
			String str = arg0.toString();
			if (str.startsWith("zh")) {
				return 0;
			} else if (str.startsWith("l")) {
				return 1;
			} else if (str.startsWith("w")) {
				return 2;
			} else if (str.startsWith("r")) {
				return 3;
			} else {
				return 4;
			}
		}

	}

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);
		MRUtil.removeOutput(conf, "hdfs://192.168.40.194:9000/");
		job.setJarByClass(PartitionerTest.class);
		job.setMapperClass(TokenizerMapper.class);
		job.setCombinerClass(IntSumReducer.class);
		<span style="color:#ff0000;">job.setPartitionerClass(MyPartitioner.class);</span>
		job.setReducerClass(IntSumReducer.class);
		<span style="color:#ff0000;">job.setNumReduceTasks(5);</span>
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		FileInputFormat.addInputPath(job, new Path(
				"hdfs://192.168.40.194:9000/input/partitioner"));
		FileOutputFormat.setOutputPath(job, new Path(
				"hdfs://192.168.40.194:9000/output"));
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}

结果:
MapReduce之自定义partitioner_第1张图片

你可能感兴趣的:(MapReduce之自定义partitioner)