Hadoop 之 Secondary Sort介绍<转>

Hadoop 之 Secondary Sort介绍

---------------------------

我们知道,在reduce之前,MP框架会对收到的<K,V>对按K进行排序,而对于一个特定的K来说,它的List<V>是没有被排过序的,就是说这些V是无序的,因为它们来自不同的Map端,而且很多应用也不依赖于K所对应的list<V>的顺序,但是有一些应用就要就要依赖于相同K的V的顺序,而且还要把他们聚合在一起,下面会提出这样一个问题,是参考Hadoop The Defiinitive Guide的第八章。

1. 问题的提出

对于如下数据,我们要计算出每一年的最高温度值:
[html]  view plain copy
  1. (1900,34)  
  2. (1900,32)  
  3. ....  
  4. (1950, 0)  
  5. (1950, 22)  
  6. (1950, −11)  
  7. (1949, 111)  
  8. (1949, 78)  

计算结果可能如下:

[html]  view plain copy
  1. 1901 317  
  2. 1902 244  
  3. 1903 289  
  4. 1904 256  
  5. ...  
  6. 1949 111  

我们一般的方法是把key设置成年份,把value设置成温度,在reduce的时候去遍历所有相同key的value值,找出最大的那个值,在Reduce返回的时候,只collect这个最大的值,这是一种办法,但是这种办法在效率上相对比较差,也不够灵活,下面我们来看看怎么使用Secondary Sort来解决这个问题


2. Secondary Sort

Secondary Sort 实际上就是一种对Value进行二次排序,然后按key的特定部分进行聚合的方法,这里用到了一个组合Key的概念,就是把K与要排序的Value组合在一起,生成一个新的Key值,上面的例子中,新的组合key为<1900,32>,也就是<年份,温度>的组合,(1900, 35°C),(1900, 34°C),这样组合以后,生成一个新的key,但是这样组合以后,它们会被切分到不同的Reduce上,所以我们这里要写一个根据新组合的key的第一个参数(年份)来进行相应的partitioner,在JobConf中可以使用setPartitionerClass来进行设置,这样可以解决相同年份的key会被聚合在同一个Reduce上,但是还没有解析在同一个Reduce上,把部分key相同的记录聚合(group)在一起,所以这里我们要设置一个group的比较器,这样就可以把相同年份的记录聚合在一起,但对于相同key(这里是指key中第一个参数相同)的排序问题,我们要使用一个KeyComparator比较器来做,就是在group中对key进行二次排序,在上面例子中就是按key中第二个参数温度来降序排序,这里要注意的是这里的输入是key,而不是value,这就是我们为什么把value组合在key一起的原因,而写这个比较方法的时候,还要注意一定要符合Group方法的原因,如果group是按key的第一个参数来得,那这里key的比较就要在第一个参数相同的情况下,才能会第二个参数(value)进行比较,我想这里解释了为什么这种排序叫Secondary Sort的原因吧,在上面的例子中,key的比较是先比较第一个参数(年份),如果第一个参数相同,再比较第二个参数(温度),按第二个参数降序排列。


所以一般要使用Secondary Sort,在JobConf要配置这三个参数
  • setPartitionerClass                // 这个是用来设置key的切分,上面例子中是按key中的第一个参数来切分
  • setOutputValueGroupingComparator   // 这里设置group,就是按key的哪一个参数进行聚合,上面的例子中也是按第一个参数年份进行聚合
  • setOutputKeyComparatorClass        // 这个是设置key的比较器,设置聚合的key的一个二次排序方法


3. 代码分析

[html]  view plain copy
  1. Example 8-9. Application to find the maximum temperature by sorting temperatures in the key  
  2.   
  3.   
  4. public class MaxTemperatureUsingSecondarySort extends Configured implements Tool {  
  5.   
  6.   
  7.   // Map任务  
  8.   static class MaxTemperatureMapper extends MapReduceBase implements Mapper<LongWritable, Text, IntPair, NullWritable> {  
  9.   private NcdcRecordParser parser = new NcdcRecordParser();  
  10.   public void map(LongWritable key, Text value,  
  11.       OutputCollector<IntPair, NullWritable> output, Reporter reporter)  
  12.       throws IOException {  
  13.     parser.parse(value);   // 解析输入的文本  
  14.     if (parser.isValidTemperature()) {  
  15.     // 这里把年份与温度组合成一个key,value为空  
  16.       output.collect(new IntPair(parser.getYearInt(),+ parser.getAirTemperature()), NullWritable.get());  
  17.     }  
  18.   }  
  19. }  
  20.   
  21.   
  22. // Reduce任务  
  23. static class MaxTemperatureReducer extends MapReduceBase  
  24.   implements Reducer<IntPair, NullWritable, IntPair, NullWritable> {  
  25.   public void reduce(IntPair key, Iterator<NullWritable> values,  
  26.       OutputCollector<IntPair, NullWritable> output, Reporter reporter)  
  27.       throws IOException {  
  28.     // 输出聚合的key值,这里的key是先按年份进行聚合,所我们会看到相同所有年份相同的key会聚合在一起,而这些聚合后的key按温度进行降序按列  
  29.     // 所以聚合中第一个key为温度最高的,所以这里输出的key为这一年中温度最高的值  
  30.     output.collect(key, NullWritable.get());  
  31.   }  
  32. }  
  33.   
  34.   
  35. // 切分器,这里是按年份* 127 % reduceNum来进行切分的  
  36. public static class FirstPartitioner  
  37.   implements Partitioner<IntPair, NullWritable> {  
  38.   @Override  
  39.   public void configure(JobConf job) {}  
  40.   @Override  
  41.   public int getPartition(IntPair key, NullWritable value, int numPartitions) {  
  42.     return Math.abs(key.getFirst() * 127) % numPartitions;  
  43.   }  
  44. }  
  45.   
  46.   
  47. // 聚合key的一个比较器  
  48. public static class KeyComparator extends WritableComparator {  
  49.   protected KeyComparator() {  
  50.     super(IntPair.class, true);  
  51.   }  
  52.   @Override  
  53.   public int compare(WritableComparable w1, WritableComparable w2) {  
  54.     IntPair ip1 = (IntPair) w1;  
  55.     IntPair ip2 = (IntPair) w2;  
  56.     // 这里要注意的是,一定要在聚合参数相同的情况下,再比较另一个参数  
  57.     // 这里是先比较年份,再比较温度,按温度降序排序  
  58.     int cmp = IntPair.compare(ip1.getFirst(), ip2.getFirst());  
  59.     if (cmp != 0) {  
  60.       return cmp;  
  61.     }  
  62.     return -IntPair.compare(ip1.getSecond(), ip2.getSecond()); //reverse  
  63.   }  
  64. }  
  65.   // 设置聚合比较器  
  66.   public static class GroupComparator extends WritableComparator {  
  67.     protected GroupComparator() {  
  68.       super(IntPair.class, true);  
  69.     }  
  70.     @Override  
  71.     public int compare(WritableComparable w1, WritableComparable w2) {  
  72.       IntPair ip1 = (IntPair) w1;  
  73.       IntPair ip2 = (IntPair) w2;  
  74.     // 这里是按key的第一个参数来聚合,就是年份  
  75.       return IntPair.compare(ip1.getFirst(), ip2.getFirst());  
  76.     }  
  77.   }  
  78.   @Override  
  79.   public int run(String[] args) throws IOException {  
  80.     JobConf conf = JobBuilder.parseInputAndOutput(this, getConf(), args);  
  81.     if (conf == null) {  
  82.       return -1;  
  83.     }  
  84.     conf.setMapperClass(MaxTemperatureMapper.class);  
  85.     conf.setPartitionerClass(FirstPartitioner.class);     // 设置切分器  
  86.     conf.setOutputKeyComparatorClass(KeyComparator.class); // 设置key的比较器  
  87.     conf.setOutputValueGroupingComparator(GroupComparator.class); // 设置聚合比较器  
  88.     conf.setReducerClass(MaxTemperatureReducer.class);  
  89.     conf.setOutputKeyClass(IntPair.class);  // 设置key的一个组合类型,如里这个类型实现了WritableComparable<T>的话,那就不要设置setOutputKeyComparatorClass了.  
  90.     conf.setOutputValueClass(NullWritable.class);  // 输出的value为NULL,因为这里的实际value已经组合到了key中  
  91.     JobClient.runJob(conf);  
  92.     return 0;  
  93.   }  
  94.   
  95.   
  96.   public static void main(String[] args) throws Exception {  
  97.     int exitCode = ToolRunner.run(new MaxTemperatureUsingSecondarySort(), args);  
  98.     System.exit(exitCode);  
  99.   }  
  100. }  

输出结果如下:

[html]  view plain copy
  1. % hadoop jar job.jar MaxTemperatureUsingSecondarySort input/ncdc/all \  
  2. > output-secondarysort  
  3. % hadoop fs -cat output-secondarysort/part-* | sort | head  
  4. 1901    317  
  5. 1902    244  
  6. 1903    289  
  7. 1904    256  
  8. 1905    283  
  9. 1906    294  
  10. 1907    283  
  11. 1908    289  

要注意的是有时候用户会存储这些value,所以你要在Map的时候输出这些value,而不是上面的NULL,而输出的这些value是经过排序的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值