MapReduce 实现全排序的方式

默认情况下,mr只对key排序。我们所说的全排序,即对key的全排序。

1、使用一个reducer

这个是最容易想到的思路,优点是实现简单,缺点也很明显,一个reduce有可能比较慢。


2、重写Partioner类。

通过重写Partition类,把key在一个范围内的发往一个固定的reduce,这样在一个reduce内key是全排序的,在reduce之间按照序号也是排好序的。比如key代表的是一个年龄。我们可以把数据输出到10个reduer。1-10岁之间发往第0个reduce,11-20发往第2个reduce,以此类推。

缺点是这种划分可能不均匀。

3,、使用TotalOrderPartition


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

[java]  view plain copy print ?
  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. }  

[java]  view plain copy print ?
  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数

[java]  view plain copy print ?
  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. }  


示例程序

[java]  view plain copy print ?
  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.11000010);  
  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啊


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值