Hadoop MapReduce 学习笔记(九) MapReduce实现类似SQL的order by/排序 正确写法

   本博客属原创文章,转载请注明出处:http://guoyunsky.iteye.com/blog/1235949

  

       请先阅读:           

           1.Hadoop MapReduce 学习笔记(一) 序言和准备

           2.Hadoop MapReduce 学习笔记(二) 序言和准备 2

                3.Hadoop MapReduce 学习笔记(八) MapReduce实现类似SQL的order by/排序

 

    下一篇: Hadoop MapReduce 学习笔记(九) MapReduce实现类似SQL的order by/排序 正确写法

 

      Hadoop MapReduce 学习笔记(八) MapReduce实现类似SQL的order by/排序 写法会触发内存溢出,因为将数据都放到一个内存容器中.这是我一开始所想到的,后来触发了OOM.于是想到了借用map输出的自己的排序,网上找资料以及hadoop/examples下也是这种写法.代码如下:

 

package com.guoyun.hadoop.mapreduce.study;


import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
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.output.FileOutputFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 对具有多列数据中的一列进行从小到大排序,并返回结果
 * 如果想实现从大到小,或者多列数据排序,请自己实现 @WritableComparable
 * 需要实现对多列排序,例如SQL:
 * SELECT * FROM TABLE ORDER BY COL1 ASC,COL2 ASC 请查看 @OrderByMultiMapReduceTest
 */
public class OrderBySingleMapReduceFixTest extends MyMapReduceMultiColumnTest {
  
  public static final Logger log=LoggerFactory.getLogger(OrderBySingleMapReduceFixTest.class);
  
  public OrderBySingleMapReduceFixTest(long dataLength) throws Exception {
    super(dataLength);
    // TODO Auto-generated constructor stub
  }

  public OrderBySingleMapReduceFixTest(String outputPath) throws Exception {
    super(outputPath);
    // TODO Auto-generated constructor stub
  }

  public OrderBySingleMapReduceFixTest(long dataLength, String inputPath,
      String outputPath) throws Exception {
    super(dataLength, inputPath, outputPath);
    // TODO Auto-generated constructor stub
  }
  
  /**
   * Map,to get the source datas
   */
  private static class MyMapper extends Mapper<LongWritable,Text,LongWritable,Text>{
    private final LongWritable writeKey=new LongWritable(0);
    private Text writeValue=new Text();
    
    @Override
    protected void map(LongWritable key, Text value, Context context)
        throws IOException, InterruptedException {
      log.debug("begin to map");
      String[] splits=null;
      
      try {
        splits=value.toString().split("\\t");
        if(splits!=null&&splits.length==2){
          writeValue.set(splits[0]);
          writeKey.set(Long.parseLong(splits[1].trim()));
        }
      } catch (NumberFormatException e) {
        log.error("map error:"+e.getMessage());
      }
      
      context.write(writeKey, writeValue);
    }
  }
  
  private static class MyReducer 
    extends Reducer<LongWritable,Text,LongWritable,Text>{

    @Override
    protected void reduce(LongWritable key, Iterable<Text> values,
        Context context) throws IOException, InterruptedException {
      for(Text value:values){
        context.write(key, value);
      }
    }
    
  }
  
  /**
   * @param args
   */
  public static void main(String[] args) {
    MyMapReduceTest mapReduceTest=null;
    Configuration conf=null;
    Job job=null;
    FileSystem fs=null;
    Path inputPath=null;
    Path outputPath=null;
    long begin=0;
    String input="testDatas/mapreduce/MRInput_Single_OrderBy_Fix";
    String output="testDatas/mapreduce/MROutput_Single_OrderBy_Fix";
    
    
    try {
      mapReduceTest=new OrderBySingleMapReduceFixTest(100000,input,output);
      
      inputPath=new Path(mapReduceTest.getInputPath());
      outputPath=new Path(mapReduceTest.getOutputPath());
      
      conf=new Configuration();
      job=new Job(conf,"OrderBy");
      
      fs=FileSystem.getLocal(conf);
      if(fs.exists(outputPath)){
        if(!fs.delete(outputPath,true)){
          System.err.println("Delete output file:"+mapReduceTest.getOutputPath()+" failed!");
          return;
        }
      }
      
      
      job.setJarByClass(OrderBySingleMapReduceFixTest.class);
      job.setMapOutputKeyClass(LongWritable.class);
      job.setMapOutputValueClass(Text.class);
      job.setOutputKeyClass(LongWritable.class);
      job.setOutputValueClass(Text.class);
      job.setMapperClass(MyMapper.class);
      job.setReducerClass(MyReducer.class);
      
      job.setNumReduceTasks(2);
      
      FileInputFormat.addInputPath(job, inputPath);
      FileOutputFormat.setOutputPath(job, outputPath);
      
      begin=System.currentTimeMillis();
      job.waitForCompletion(true);
      
      System.out.println("===================================================");
      if(mapReduceTest.isGenerateDatas()){
        System.out.println("The maxValue is:"+mapReduceTest.getMaxValue());
        System.out.println("The minValue is:"+mapReduceTest.getMinValue());
      }
      System.out.println("Spend time:"+(System.currentTimeMillis()-begin));
      // Spend time:13361
      
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    

  }

}

 

更多技术文章、感悟、分享、勾搭,请用微信扫描:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值