1.实例描述
对输入文件中的数据进行排序。输入文件中的每行内容均为一个数字,即一个数据。要求在输出中每行有两个间隔的数字,其中,第二个数字代表原始数据,第一个数字代表这个原始数据在数据集中的位次。
样例输入:
file1.txt:
2
32
654
32
15
756
65223
file2.txt:
5956
22
650
92
file3.txt
26
54
6
样例输出:
1 2
2 6
3 15
4 22
5 26
6 32
7 32
8 54
9 92
10 650
11 654
12 756
13 5956
14 65223
2.设计思路
首先需要了解MapReduce过程中的默认排序跪着。它是按照key值进行排序,如果key为封装的IntWritable类型,那么MapReduce按照数字的大小排序;如果key为封装String的Text类型,那么MapReduce按照字典顺序对字符串排序。需要注意的是,Reduce自动排序的数据仅仅是发送到自己所在节点的数据,使用默认的排序并不能保证全局的顺序,因为在排序还有一个partition的过程,默认无法保证分割后各个Reduce上的数据整体式有序的。所以要想使用默认的排序过程,还必须定义自己的Partition类,保证执行Partition过程之后所有Reduce上的数据在整体上是有序的,然后在对局部Reduce上的数据进行默认排序,这样才能保证所有数据有序。因此首先应该使用封装int的IntWritable型数据结构,也就是将读入的数据在Map中转化成IntWritable型,然后作为key值输出(value任意);其次需要重写partition类,保证整体有序,利用输入数据的最大值除以系统partition数量的商作为分割数据的边界增量,也就是说分割数据的边界为此商的1倍、2倍至numPartition-1倍,这样就能保证执行 partition后的数据时整体有序的;然后Reduce获得<key,value-list>之后,根据value-list中元素的个数将输入的key作为value的输出次数,输出的key是一个全局变量,用于统计当前key的位次。需要注意的是,这个程序中没有配置Combiner,也就是说在MapReduce过程不使用Combiner。
3.程序代码
程序代码如下:
package org.apache.hadoop.examples;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class Sort {
//map将输入中的value化成IntWritable类型,作为输出的key
public static class Map extends Mapper<Object, Text, IntWritable, IntWritable> {
private static IntWritable data = new IntWritable();
public void map(Object key, Text value, Context context) throws IOException,
InterruptedException {
String line = value.toString();
data.set(Integer.parseInt(line));
context.write(data, new IntWritable(1));
}
}
//reduce将输入中的key复制到输出的value上,然后根据输入的
//value-list中的元素的个数决定key的输出次数
//用全局的linenum来代表key的位次
public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
private static IntWritable linenum = new IntWritable(1);
@SuppressWarnings("unused")
public void reduce(IntWritable key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
for (IntWritable val : values) {
context.write(linenum, key);
linenum = new IntWritable(linenum.get() + 1);
}
}
}
//自定义Partition函数,此函数根据输入数据的最大值和MapReduce框架中
//Partition的数据获取将输入数据按照大小分块的边界,然后根据输入数值和
//边界的观关系返回对应的Partition ID
public static class Partition extends Partitioner<IntWritable, IntWritable> {
@Override
public int getPartition(IntWritable key, IntWritable value, int numPartitins) {
int maxNumber = 65223;
int bound = maxNumber / numPartitins + 1;
int keyNumber = key.get();
for (int i = 1; i <= numPartitins; i++) {
if (keyNumber < bound * i && keyNumber >= bound * (i - 1)) {
return i - 1;
}
}
return -1;
}
}
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage WordCount <int> <out>");
System.exit(2);
}
Job job = new Job(conf, "Sort");
job.setJarByClass(Sort.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setPartitionerClass(Partition.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
参考:
1.Hadoop实战第2版 陆嘉恒 著