一、实验目的
数据排序是许多实际任务在执行时要完成的一项工作,比如学生成绩评比、数据建议索引等。先对原始数据进行初步处理,为进一步的数据操作打好基础。
二、实例描述
对输入文件中的数据进行排序。输入文件中的每行内容均为一个数字,即一个数据。要求在输出中每行有两个间隔的数字,其中,第二个数字代表原始数据,第一个数字代表这个原始数据在原始数据集中的位次。
样例输入:
file1: file2: file3:
2 5956 26
32 22 54
654 650 6
32 92
15
756
65223
样本输出:
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
三、设计思路
这个实例仅仅要求对输入数据进行排序,熟悉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倍至numPartition-1倍,这样就能保证执行partition后的数据是整体有序的;然后Reduce获得<Key,value-list>之后,根据value-list中元素的个数将输入的Key作为value的输出次数,输出的Key是一个全局变量,用于统计当前Key的位次。需要注意的是,这个程序中没有配置Combiner,也就是说在Mapreduce过程中不使用Combiner。这主要是因为使用Map和Reduce就已经能够完成任务了。
四、程序代码
package HadoopShiZhang2;
import java.io.IOException;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.Partitioner;
/**
* 程序以调试成功
*
*/
public classSort {
static final String INPUT_PATH = "hdfs://chaoren:9000/sort";
static final String OUT_PATH = "hdfs://chaoren:9000/out";
public static void main(String[] args) throws Exception, Exception {
Configurationconf = newConfiguration();
final FileSystem fileSystem =FileSystem.get(new URI(INPUT_PATH),conf);
final Path outPath = new Path(OUT_PATH);
if(fileSystem.exists(outPath)){
fileSystem.delete(outPath,true);
}
final Job job = newJob(conf, Sort.class.getSimpleName());
//打JAR包
job.setJarByClass(Sort.class);
//1.1 指定输入文件路径
FileInputFormat.setInputPaths(job,INPUT_PATH);
//指定哪个类用来格式化输入文件
job.setInputFormatClass(TextInputFormat.class);
//1.2指定自定义的Mapper类
job.setMapperClass(Map.class);
//指定输出<k2,v2>的类型
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(IntWritable.class);
//1.3 指定分区类
//1.4 TODO排序、分区
//1.5 TODO(可选)合并
//job.setCombinerClass(Reduce.class);
//2.2 指定自定义的reduce类
job.setReducerClass(Reduce.class);
job.setPartitionerClass(Partition.class);
//指定输出<k3,v3>的类型
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);
//2.3 指定输出到哪里
FileOutputFormat.setOutputPath(job,outPath);
//设定输出文件的格式化类
job.setOutputFormatClass(TextOutputFormat.class);
//把代码提交给JobTracker执行
job.waitForCompletion(true);
}
//map将输入中的value转化成LongWritable类型,作为输出的key
staticclassMap extendsMapper<Object, Text, IntWritable, IntWritable>{
private staticIntWritable data= newIntWritable();
protected void map(Object key, Textvalue,Context context)
throws IOException,InterruptedException {
final String line = value.toString();
data.set(Integer.parseInt(line));
context.write(data, new IntWritable(1));
};
}
//reduce将输入的key复制到输出的value,然后根据输入的
//value-list中元素的个数决定key的输出次数
//用全局linenum来代表key的位次
staticclassReduce extendsReducer<IntWritable, IntWritable, IntWritable, IntWritable>{
private static IntWritable linenum = new IntWritable(1);
protected 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
publicstaticclassPartition extendsPartitioner <IntWritable,IntWritable> {
@Override
public int getPartition(IntWritable key, IntWritablevalue, intnumPartitions) {
int Maxnumber = 65223;
int bound =Maxnumber/numPartitions + 1;
int keynumber = key.get();
for(int i = 0; i <numPartitions; i++){
if(keynumber < bound*i&& keynumber >= bound*(i-1))
return i-1;
}
return 0;
}
}
}
五、实验结果