MapReduce之自定義partitioner

停不下的腳步發表於2015-02-02
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);
	}
}

結果:


相關文章