hadoop排序可以分为以下四种排序:
1、普通排序
1)MapReduce本身自带排序功能
2)IntWritable,LongWritable,Text等实现了WritableComparable类型的对象,都是可以排序的。
2、部分排序
map和reduce处理过程中包含了默认对key的排序,那么如果不要求全排序,可以直接把结果输出,每个输出文件中包含的就是按照key进行排序的结果
3、全局排序
1)Hadoop平台没有提供全局数据排序,而在大规模数据处理中进行数据的全局排序是非常普遍的需求。
2)使用hadoop进行大量的数据排序最直观的方法是把文件所有内容给map之后,map不做任何处理,直接输出给一个reduce,利用hadoop的自己的shuffle机制,对所有数据进行排序,而后由reduce直接输出。
3)主要思路就是将数据按照区间进行分割,比如对整数排序,[0,3300)的在partition 0中,(3300,6600]在partition 1中,剩下的在partition 2中
举例如下:
MyMapper.java
package com.sort;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class MyMapper extends Mapper<LongWritable, Text, LongWritable, NullWritable> {
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String[] values = value.toString().split("\\s+") ;
context.write(new LongWritable(Long.valueOf(values[0])), NullWritable.get()) ;
}
}
MyPartitioner.java
package com.sort;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Partitioner;
public class MyPartitioner extends Partitioner<LongWritable, NullWritable> {
@Override
public int getPartition(LongWritable key, NullWritable value, int numPartitions) {
if(key.get() <= 3300){
return 0 % numPartitions ;
}else if(key.get() >3300 && key.get() < 6600){
return 1 % numPartitions ;
}else{
return 2 % numPartitions ;
}
}
}
MyReducer.java
package com.sort;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;
public class MyReducer extends Reducer<LongWritable, NullWritable, LongWritable, NullWritable> {
@Override
protected void reduce(LongWritable key, Iterable<NullWritable> value,Context context)
throws IOException, InterruptedException {
context.write(key,NullWritable.get()) ;
}
}
TestSort.java
package com.sort;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class TestSort {
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 <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "Test Sort");
job.setJarByClass(TestSort.class);
job.setMapperClass(MyMapper.class);
job.setPartitionerClass(MyPartitioner.class) ;
job.setReducerClass(MyReducer.class) ;
job.setNumReduceTasks(3) ;
job.setMapOutputKeyClass(LongWritable.class) ;
job.setMapOutputValueClass(NullWritable.class) ;
job.setOutputKeyClass(LongWritable.class) ;
job.setOutputValueClass(NullWritable.class) ;
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
在数据分布均匀的情况下,每个分区内的数据量基本相同。这种就是比较理想的情况下,但是实际中数据往往分布不均匀,出现了数据倾斜的情况,这时按照之前的分区划分数据就不合适了,此时就需要一定的帮助——采样器。
我们可以归纳出这样一个用hadoop对大量数据排序的步骤:
1) 对待排序数据进行抽样;
2) 对抽样数据进行排序,产生标尺;
3) Map对输入的每条数据计算其处于哪两个标尺之间;将数据发给对应区间ID的reduce
4) Reduce将获得数据直接输出。
4、二次排序
MapReduce框架在把记录到达reducers之前会将记录按照键排序。对于任意一个特殊的键,然而,值是不排序的。甚至是,值在两次执行中的顺序是不一样的,原因是它们是从不同的map中来的,这些不同的map可能在不同的执行过程中结束的先后顺序不确定。通常情况下,大多数的MapReduce程序的reduce函数不会依赖于值的顺序。二次排序是在键排序的基础上,在对值进行排序