MapReduce排序

排序是MapReduce的核心技术,排序分为部分排序,全排序和二次排序。

部分排序:调用默认的HashPartitioner,不需要操作,每个reduce聚合的key都是有序的。

全排序:对reduce输出的所有的key实现排序

             方法1:设置一个reducde

             方法2:自定义分区类实现全排序

             方法3 :使用采样    

二次排序: key排完序,再次基础上进行二次排序


以统计每年的最高气温为例进行示例:

注意:源文件是一个sequenceFile序列文件<IntWritable, IntWritable>

1、MaxTempMapper

[java]  view plain  copy
  1. package hadoop.mr.sort.total.totalorder;  
  2.   
  3. import org.apache.hadoop.io.IntWritable;  
  4. import org.apache.hadoop.mapreduce.Mapper;  
  5.   
  6. import java.io.IOException;  
  7.   
  8. /** 
  9.  * MaxTempMapper 
  10.  */  
  11. public class MaxTempMapper extends Mapper<IntWritable, IntWritable, IntWritable, IntWritable> {  
  12.   
  13.     protected void map(IntWritable key, IntWritable value, Context context) throws IOException, InterruptedException {  
  14.         context.write(key,value);  
  15.     }  
  16. }  
2、MaxTempReducer
[java]  view plain  copy
  1. package hadoop.mr.sort.total.totalorder;  
  2.   
  3. import org.apache.hadoop.io.IntWritable;  
  4. import org.apache.hadoop.mapreduce.Reducer;  
  5.   
  6. import java.io.IOException;  
  7.   
  8. /** 
  9.  */  
  10. public class MaxTempReducer extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable>{  
  11.     protected void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {  
  12.         int max = Integer.MIN_VALUE ;  
  13.         for(IntWritable iw : values){  
  14.             max = max > iw.get() ? max : iw.get() ;  
  15.         }  
  16.         context.write(key,new IntWritable(max));  
  17.     }  
  18. }  

3、App

[java]  view plain  copy
  1. package hadoop.mr.sort.total.totalorder;  
  2.   
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.fs.FileSystem;  
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.io.IntWritable;  
  7. import org.apache.hadoop.mapreduce.Job;  
  8. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  9. import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;  
  10. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  11. import org.apache.hadoop.mapreduce.lib.partition.InputSampler;  
  12. import org.apache.hadoop.mapreduce.lib.partition.TotalOrderPartitioner;  
  13.   
  14. /** 
  15.  */  
  16. public class App {  
  17.     public static void main(String[] args) throws Exception {  
  18.         args = new String[]{"d:/java/mr/data/temp.seq""d:/java/mr/out"};  
  19.         Configuration conf = new Configuration();  
  20.         FileSystem fs = FileSystem.get(conf);  
  21.         if(fs.exists(new Path(args[1]))){  
  22.             fs.delete(new Path(args[1]),true);  
  23.         }  
  24.   
  25.         Job job = Job.getInstance(conf);  
  26.   
  27.         job.setJobName("maxTemp");  
  28.         job.setJarByClass(App.class);  
  29.   
  30.         job.setMapperClass(MaxTempMapper.class);  
  31.         job.setReducerClass(MaxTempReducer.class);  
  32.   
  33.         FileInputFormat.addInputPath(job,new Path(args[0]));  
  34.         FileOutputFormat.setOutputPath(job,new Path(args[1]));  
  35.         //设置combine输入格式  
  36.         job.setInputFormatClass(SequenceFileInputFormat.class);  
  37.         job.setPartitionerClass(TotalOrderPartitioner.class);  
  38.   
  39.         job.setNumReduceTasks(3);  
  40.   
  41.         job.setMapOutputKeyClass(IntWritable.class);  
  42.         job.setMapOutputValueClass(IntWritable.class);  
  43.   
  44.         job.setOutputKeyClass(IntWritable.class);  
  45.         job.setOutputValueClass(IntWritable.class);  
  46.   
  47.         TotalOrderPartitioner.setPartitionFile(job.getConfiguration(),new Path("file:///d:/java/mr/par.seq"));  
  48.         //随机采样器  
  49.         InputSampler.RandomSampler<IntWritable,IntWritable> r = new InputSampler.RandomSampler<IntWritable, IntWritable>(1f,5,3);  
  50.         //创建分区文件  
  51.         InputSampler.writePartitionFile(job,r);  
  52.   
  53.         job.waitForCompletion(true);  
  54.     }  
  55. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值