Hadoop之分区,排序以及combiner

117 阅读1分钟

1. 自定义分区步骤

  1. 继承partitioner,重写getPartition()方法

image.png 2. 在job驱动中,设置自定义partitioner

job.setPartitionerClass(MyPartitioner.class);

  1. 自定义partitioner之后,需要设置响应数量ReduceTask

job.setNumReduceTasks(5);

2. 分区总结

image.png


3. WritableComparable排序

MapTask和ReduceTask,均会对key进行排序,这是Hadoop的默认行为,任何应用程序都会排序,不管应用是否需要。且默认都是对key按照字典顺序排序,实现方式是快排序。

排序概述:

image.png

排序分类

image.png

全排序

  1. 设置只有一个Reduce
  2. 自定义排序方法,key和value调换位置,

image.png

4. combiner

image.png

5. 自定义combiner

  1. 继承Reducer,实现reduce方法
public class WordcountCombiner extends Reducer<Text, IntWritable, Text,IntWritable>{

	@Override
	protected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {

        // 1 汇总操作
		int count = 0;
		for(IntWritable v :values){
			count += v.get();
		}

        // 2 写出
		context.write(key, new IntWritable(count));
	}
}
  1. job设置驱动类
job.setCombinerClass(WordcountCombiner.class);