我们知道Mapreduce框架在feed数据给reducer之前会对map output key排序,这种排序机制保证了每一个reducer局部有序,hadoop 默认的partitioner是HashPartitioner,它依赖于output key的hashcode,使得相同key会去相同reducer,但是不保证全局有序,如果想要获得全局排序结果(比如获取top N, bottom N),就需要用到TotalOrderPartitioner了,它保证了相同key去相同reducer的同时也保证了全局有序。

  1. public class HashPartitioner<K, V> extends Partitioner<K, V> {
  2. /** Use {@link Object#hashCode()} to partition. */
  3. public int getPartition(K key, V value,
  4. int numReduceTasks) {
  5. return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks;
  6. }
  7. }
  1. /**
  2. * Partitioner effecting a total order by reading split points from
  3. * an externally generated source.
  4. */
  5. @InterfaceAudience.Public
  6. @InterfaceStability.Stable
  7. public class TotalOrderPartitioner<K extends WritableComparable<?>,V>
  8. extends Partitioner<K,V> implements Configurable {
  9. // by construction, we know if our keytype
  10. @SuppressWarnings("unchecked") // is memcmp-able and uses the trie
  11. public int getPartition(K key, V value, int numPartitions) {
  12. return partitions.findPartition(key);
  13. }
  14. }

TotalOrderPartitioner依赖于一个partition file来distribute keys,partition file是一个实现计算好的sequence file,如果我们设置的reducer number是N,那么这个文件包含(N-1)个key分割点,并且是基于key comparator排好序的。TotalOrderPartitioner会检查每一个key属于哪一个reducer的范围内,然后决定分发给哪一个reducer。

InputSampler类的writePartitionFile方法会对input files取样并创建partition file。有三种取样方法:

1. RandomSampler  随机取样

2. IntervalSampler  从s个split里面按照一定间隔取样,通常适用于有序数据

3. SplitSampler  从s个split中选取前n条记录取样

paritition file可以通过TotalOrderPartitioner.setPartitionFile(conf, partitionFile)来设置,在TotalOrderPartitioner instance创建的时候会调用setConf函数,这时会读入partition file中key值,如果key是BinaryComparable(可以认为是字符串类型)的话会构建trie,时间复杂度是O(n), n是树的深度。如果是非BinaryComparable类型就构建BinarySearchNode,用二分查找,时间复杂度O(log(n)),n是reduce数

  1. boolean natOrder =
  2. conf.getBoolean(NATURAL_ORDER, true);
  3. if (natOrder && BinaryComparable.class.isAssignableFrom(keyClass)) {
  4. partitions = buildTrie((BinaryComparable[])splitPoints, 0,
  5. splitPoints.length, new byte[0],
  6. // Now that blocks of identical splitless trie nodes are
  7. // represented reentrantly, and we develop a leaf for any trie
  8. // node with only one split point, the only reason for a depth
  9. // limit is to refute stack overflow or bloat in the pathological
  10. // case where the split points are long and mostly look like bytes
  11. // iii...iixii...iii . Therefore, we make the default depth
  12. // limit large but not huge.
  13. conf.getInt(MAX_TRIE_DEPTH, 200));
  14. } else {
  15. partitions = new BinarySearchNode(splitPoints, comparator);
  16. }

示例程序

  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.fs.Path;
  3. import org.apache.hadoop.io.Text;
  4. import org.apache.hadoop.mapreduce.Job;
  5. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  6. import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
  7. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  8. import org.apache.hadoop.mapreduce.lib.partition.InputSampler;
  9. import org.apache.hadoop.mapreduce.lib.partition.InputSampler.RandomSampler;
  10. import org.apache.hadoop.mapreduce.lib.partition.TotalOrderPartitioner;
  11.  
  12. public class TotalSortMR {
  13.  
  14. public static int runTotalSortJob(String[] args) throws Exception {
  15. Path inputPath = new Path(args[0]);
  16. Path outputPath = new Path(args[1]);
  17. Path partitionFile = new Path(args[2]);
  18. int reduceNumber = Integer.parseInt(args[3]);
  19.  
  20. // RandomSampler第一个参数表示key会被选中的概率,第二个参数是一个选取samples数,第三个参数是最大读取input splits数
  21. RandomSampler<Text, Text> sampler = new InputSampler.RandomSampler<Text, Text>(0.1, 10000, 10);
  22.  
  23. Configuration conf = new Configuration();
  24. // 设置partition file全路径到conf
  25. TotalOrderPartitioner.setPartitionFile(conf, partitionFile);
  26.  
  27. Job job = new Job(conf);
  28. job.setJobName("Total-Sort");
  29. job.setJarByClass(TotalSortMR.class);
  30. job.setInputFormatClass(KeyValueTextInputFormat.class);
  31. job.setMapOutputKeyClass(Text.class);
  32. job.setMapOutputValueClass(Text.class);
  33. job.setNumReduceTasks(reduceNumber);
  34.  
  35. // partitioner class设置成TotalOrderPartitioner
  36. job.setPartitionerClass(TotalOrderPartitioner.class);
  37.  
  38. FileInputFormat.setInputPaths(job, inputPath);
  39. FileOutputFormat.setOutputPath(job, outputPath);
  40. outputPath.getFileSystem(conf).delete(outputPath, true);
  41.  
  42. // 写partition file到mapreduce.totalorderpartitioner.path
  43. InputSampler.writePartitionFile(job, sampler);
  44.  
  45. return job.waitForCompletion(true)? 0 : 1;
  46.  
  47. }
  48.  
  49. public static void main(String[] args) throws Exception{
  50. System.exit(runTotalSortJob(args));
  51. }
  52. }

上面的例子是采用InputSampler来创建partition file,其实还可以使用mapreduce来创建,可以自定义一个inputformat来取样,将output key输出到一个reducer

ps:hive 0.12实现了parallel ORDER BY(https://issues.apache.org/jira/browse/HIVE-1402),也是基于TotalOrderPartitioner,非常靠谱的new feature啊

MapReduce TotalOrderPartitioner 全局排序的更多相关文章

  1. mapreduce实现全局排序

    直接附代码,说明都在源码里了. package com.hadoop.totalsort; import java.io.IOException; import java.util.ArrayList ...

  2. 一起学Hadoop——TotalOrderPartitioner类实现全局排序

    Hadoop排序,从大的范围来说有两种排序,一种是按照key排序,一种是按照value排序.如果按照value排序,只需在map函数中将key和value对调,然后在reduce函数中在对调回去.从小 ...

  3. MapReduce怎么优雅地实现全局排序

    思考 想到全局排序,是否第一想到的是,从map端收集数据,shuffle到reduce来,设置一个reduce,再对reduce中的数据排序,显然这样和单机器并没有什么区别,要知道mapreduce框 ...

  4. 三种方法实现Hadoop(MapReduce)全局排序(1)

    我们可能会有些需求要求MapReduce的输出全局有序,这里说的有序是指Key全局有序.但是我们知道,MapReduce默认只是保证同一个分区内的Key是有序的,但是不保证全局有序.基于此,本文提供三 ...

  5. Mapreduce的排序(全局排序、分区加排序、Combiner优化)

    一.MR排序的分类 1.部分排序:MR会根据自己输出记录的KV对数据进行排序,保证输出到每一个文件内存都是经过排序的: 2.全局排序: 3.辅助排序:再第一次排序后经过分区再排序一次: 4.二次排序: ...

  6. 大数据mapreduce全局排序top-N之python实现

    a.txt.b.txt文件如下: a.txt hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop ...

  7. Hadoop对文本文件的快速全局排序

    一.背景 Hadoop中实现了用于全局排序的InputSampler类和TotalOrderPartitioner类,调用示例是org.apache.hadoop.examples.Sort. 但是当 ...

  8. MapReduce分区和排序

    一.排序 排序: 需求:根据用户每月使用的流量按照使用的流量多少排序 接口-->WritableCompareable 排序操作在hadoop中属于默认的行为.默认按照字典殊勋排序. 排序的分类 ...

  9. Hadoop学习笔记—11.MapReduce中的排序和分组

    一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排 ...

随机推荐

  1. Elasticsearch 安装与集群配置

    一.软件版本 操作系统:CentOS-6.5-x86_64 ES版本:5.0 主机:192.168.63.246 主机: 192.168.63.242 二.部署环境规划:   1. 需求:jdk版本: ...

  2. 使用Memcache在PHP中调试方法的介绍及应用

    使用Memcache在PHP中调试方法的介绍及应用 如果我们在网络开发中,特别是大访问量的web项目开发中,为了提高响应速度,减少数据查询运算,那么我们都会选用memcahce.首先我们必须要安装,接 ...

  3. IOS反地理编码取得城市名称

    // 获取当前所在的城市名 CLGeocoder *reverseGeocoder=[[CLGeocoder alloc] init]; [reverseGeocoder reverseGeocode ...

  4. 并行任务task

    http://msdn.microsoft.com/zh-cn/library/dd537609(v=vs.110).aspx http://www.cnblogs.com/yangecnu/p/So ...

  5. mysql binlog参数设置

    1.mysql有许多系统变量,可以设置,系统变量设置不同,不同的系统将导致执行状态. 故mysql提供两组命令,分别查看系统设置和执行状态. 1.系统设置: SHOW [GLOBAL | SESSIO ...

  6. Swift Strings and Characters

    String 是一个有序的字符集合,例如 "hello, world", "albatross".Swift 字符串通过 String 类型来表示,也可以表示为 ...

  7. Java中的内存泄漏问题

    今天来谈谈Java语言中的内存泄漏问题,可能还有人不知道什么是内存泄漏,先来说下内存泄漏的概念. 内存泄漏:比较正式的说法是,不再使用的对象,却不能被Java垃圾回收机回收.用我的话来说,就是Java ...

  8. Java并发编程学习笔记 深入理解volatile关键字的作用

    引言:以前只是看过介绍volatile的文章,对其的理解也只是停留在理论的层面上,由于最近在项目当中用到了关于并发方面的技术,所以下定决心深入研究一下java并发方面的知识.网上关于volatile的 ...

  9. RocketMQ与Kafka对比(18项差异)评价版

    此文是rocketmq作者vintage.wang所写,对于每项对比,后面都增加了我的观点,有不对的地方,请各位指出. 淘宝内部的交易系统使用了淘宝自主研发的Notify消息中间件,使用Mysql作为 ...

  10. Model注解的后台原理

    Asp.net MVC的验证特性是由模型绑定器.模型元数据.模型验证器和模型状态组成的协调系统的一部分. 1.验证和模型绑定 默认情况下,Asp.net MVC框架在模型绑定石执行验证逻辑,在操作方法 ...