Hadoop 排序

  数据排序是许多实际任务在执行时要完成的第一项工作,比如学生成绩评比、数据建立索引等。这个实例和数据去重类似,都是先对原始数据进行初步处理,为进一步的数据操作打好基础。

1.实例描述

  对输入文件中的数据进行排序。输入文件中的每行内容均为一个数字,即一个数据。要求在输出中每行有两个间隔的数字,其中,第二个数字代表原始数据,第一个数字这个原始数据在原始数据集中的位次。

  样例输入:

  file1:

  2
  32
  654
  32
  15
  756
  65223

  file2:

  5956
  22
  650
  92

  file3:

  26
  54
  6

  样例输出:

  

2.设计思路

  这个实例仅仅要求对输入数据进行排序,熟悉MapReduce过程的读者很快会想到在MapReduce过程中就有排序。是否可以利用这个默认的排序、而不需要自己在实现具体的排序呢?答案是肯定的。但是在使用之前首先要了解MapReduce过程中的默认排序规则。它是按照key值进行排序,如果key为封装int的IntWritable类型,那么MapReduce按照数字大小对key排序;如果key为封装String的Text类型,那么MapReduce按照字典顺序对字符串排序。需要注意的是,Reduce自动排序的数据仅仅是发送到自己所在节点的数据,使用默认的排序并不能保证全局的顺序,因为在排序前还有一个partition的过程,默认无法保证分割后各个Reduce上的数据整体是有序的。所以要想使用默认的排序过程,还必须定义自己的Partition类,保证执行Partition过程之后所有Reduce上的数据在整体上是有序的,然后再对局部Reduce上的数据进行默认排序,这样才能保证所有数据有序。了解了这个细节,我们就知道,首先应该使用封装int的IntWritable型数据结构,也就是将读入的数据在Map中转化为IntWritable型,然后作为key值输出(value任意);其次需要重写partition,保证整体有序,具体做法是用输入数据的最大值除以系统partition数量的商作为分割数据的边界增量,也就是说分割数据的边界为此商的1倍、2倍至numPartitions-1倍,这样就能保证执行partition后的数据是整体有序的;然后Reduce获得<key,value-list>之后,根据value-list中元素的个数将输入的key作为value的输出次数,输出的key是一个全局变量,用于统计当前key的位次。需要注意的是,这个程序中没有配置Combiner,也就是说在MapReduce过程中不使用Combiner。这主要是因为使用Map和Reduce就已经能够完成任务了。

3.程序代码:

  程序代码如下:

 1 import java.io.IOException;
 2 
 3 import org.apache.hadoop.conf.Configuration;
 4 import org.apache.hadoop.fs.Path;
 5 import org.apache.hadoop.io.IntWritable;
 6 import org.apache.hadoop.io.Text;
 7 import org.apache.hadoop.mapreduce.Job;
 8 import org.apache.hadoop.mapreduce.Mapper;
 9 import org.apache.hadoop.mapreduce.Partitioner;
10 import org.apache.hadoop.mapreduce.Reducer;
11 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
12 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
13 import org.apache.hadoop.util.GenericOptionsParser;
14 
15 
16 
17 public class Sort {
18 
19     // map将输入中的value转化成IntWritable类型,作为输出的key
20     public static class Map extends Mapper<Object, Text, IntWritable, IntWritable>{
21         private static IntWritable data = new IntWritable();
22         @Override
23         protected void map(Object key, Text value,Mapper<Object, Text, IntWritable, IntWritable>.Context context)
24                 throws IOException, InterruptedException {
25             // super.map(key, value, context);
26             String line = value.toString();
27             data.set(Integer.parseInt(line));
28             context.write(data, new IntWritable(1));
29         }
30     }
31     // reduce 将输入的key复制到输出的value上,然后根据输入的 value-list 中元素的个数决定key的输出次数
32     // 用全局linenum来代表key的位次
33     public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable>{
34         private static IntWritable linenum = new IntWritable(1);
35         @Override
36         protected void reduce(IntWritable key,    Iterable<IntWritable> values,Reducer<IntWritable, IntWritable, IntWritable, IntWritable>.Context context)
37                 throws IOException, InterruptedException {
38             // super.reduce(arg0, arg1, arg2);
39             for(IntWritable val : values){
40                 context.write(linenum, key);
41                 linenum = new IntWritable(linenum.get()+1);
42             }
43         }
44     }
45     // 自定义Partition函数,此函数根据输入数据的最大值和 MapReduce 框架中 Partition 的数量获取将输入数据按照大小分块的边界,
46     //然后根据输入数值和边界的关系返回对应的 Partition ID
47     public static class Partition extends Partitioner<IntWritable, IntWritable>{
48         @Override
49         public int getPartition(IntWritable key, IntWritable value, int numPartitions) {
50             int Maxnumber = 65223;
51             int bound = Maxnumber/numPartitions + 1;
52             int keynumber = key.get();
53             for (int i = 0; i < numPartitions; i++) {
54                 if (keynumber < bound * (i+1) && keynumber >= bound *i) {
55                     return i;
56                 }
57             }
58             return -1;
59         }
60     }
61     public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
62         Configuration conf = new Configuration();
63         String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
64         if(otherArgs.length!=2){
65             System.out.println("Usage:Score Avg");
66             System.exit(2);
67         }
68         Job job = new Job(conf,"Sort");
69         job.setJarByClass(Sort.class);
70         job.setMapperClass(Map.class);
71         job.setPartitionerClass(Partition.class);
72         job.setReducerClass(Reduce.class);
73         job.setOutputKeyClass(IntWritable.class);
74         job.setOutputValueClass(IntWritable.class);
75         
76         FileInputFormat.addInputPath(job, new Path(args[0]));
77         FileOutputFormat.setOutputPath(job, new Path(args[1]));
78         
79         System.exit(job.waitForCompletion(true)?0:1);
80     }
81 
82 }

  

原文地址:https://www.cnblogs.com/xiaoyh/p/9322244.html