`

Hadoop的Map端sort, partition, combiner以及Group

阅读更多
Mapreduce在执行的时候首先会解析成KV键值对传送到Map方法里面, 在Mapper类的run里面有这么一段代码:
while (context.nextKeyValue()) {
        map(context.getCurrentKey(), context.getCurrentValue(), context);
      }


就是只要还有KV键值对, 就不停的调用Map方法。

在Map端处理完成后我们通过Context.write输出, 然后到reducer端相同的Key的value组成到迭代器里面通过reduce代码处理。

这是默认最简单的方式, 在MR中还提供 Map端Sort, partition, combine以及Group

1.Map端的sort:

一般是通过Job的 job.setSortComparatorClass(MapSortClass) 来实现, 主要功能就是Map端执行完成在reduce端输入前根据key的值进行排序。

MapSortClass是需要继承WritableComparator的, 一个例子如下:
	public static class MapSortClass extends WritableComparator {

		public  MapSortClass(){
			
			super(LongWritable.class, true);
		}
		
		@Override
		public int compare(WritableComparable a, WritableComparable b) {
			
			LongWritable LongA = (LongWritable) a;
			LongWritable LongB = (LongWritable) b;
			
		    return -LongA.compareTo(LongB);
		}
		
		
		
		
	}


在排序的时候会调用compare方法 (序列化之前), 所以直接都是输入的object而不是二进制流。 我们要重写compare方法, 这样在输出到reduce前会根据我们写的方法重新排序。 我的例子里面就在前面加个一个 “-”号, 就是按照正常排序的倒序。

2. partition
我们可以自定义partition, 通过job.setPartitionerClass(MyPartitioner) 来设置。 默认会用HashPartitioner:
public class HashPartitioner<K, V> extends Partitioner<K, V> {

  /** Use {@link Object#hashCode()} to partition. */
  public int getPartition(K key, V value,
                          int numReduceTasks) {
    return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks;
  }

}


其实就是根据key的hashcode来分区, 我们可以自己写partitioner, 继承Partitioner即可, 我这边就根据取进来Key的值平均分成三份:
	public static class MyPartitioner extends Partitioner<LongWritable, Text>{

		@Override
		public int getPartition(LongWritable key, Text value, int numReduce) {
			
			if((Long.parseLong(key.toString()) % numReduce) == 0){
				return 0;
			}else if((Long.parseLong(key.toString()) % numReduce) == 1){
				return 1;
			}else {
				return 2;
			}
			
		}
		
	}


3.Group
Map到reduce前会根据Key做group, 一样的key就在同一个group里面, 如果我们要自定义group的话就要通过job.setGroupingComparatorClass(MapGroupClass.class)来设置, 比如说自定义的对象, 里面带两个LongWritable的对象, 但是我们其实只需要根据其中一个对象做Group, 那么我们就要自己写一个编组方法了。 我这边就对一个对象的LongWritable做group:
	public static class MyGroup implements RawComparator<LongText> {

		@Override
		public int compare(LongText o1, LongText o2) {
			return (int)(o1.getA() - o2.getA());
		}

		@Override
		public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
			
			return WritableComparator.compareBytes(b1, s1, 8, b2, s2, 8);
		}
		
	}

貌似group的时候会调用compare序列化对象的方法 , 所以只读8byte, 就是一个long的值。 这里到reduce那边后即使Key不一样, 只要第一个LongWritable对象的值一样的话, 那么就会把他们的value放到同一个Iterator里面, 在reduce端处理.

4.Combiner
combiner其实就是在map端的reducer。 有些时候我们map出来1w对KV的值, 但是我们其实只需要最大的值就行了, 没有combiner的情况下 shuffle传输到reducer那边就要传输1w对, 这样对带宽浪费比较大, 如果在map端对每个key的value都只取最大值, 那么就可以大大的省下传输的对象数量, 自然就节省了很多带宽。 
combiner可以通过job.setCombinerClass(IntSumReducer.class) 来设定

示例:
  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);
    }
  }



0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics