前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MapReduce排序输出

MapReduce排序输出

作者头像
用户3003813
发布2018-09-06 14:02:09
5450
发布2018-09-06 14:02:09
举报
文章被收录于专栏:个人分享个人分享

 hadoop的map是具有输出自动排序功能的~继续学习~

代码语言:javascript
复制
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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.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.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;


public class Sort extends Configured implements Tool {

  //这里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();
            System.out.println("line" + line);
            data.set(Integer.parseInt(line));
            context.write(data, new IntWritable(1));
        }
    }
  //reduce将输入的key复制到输出的value上,然后根据输入的value-list中的元素的个数决定key的输出次数
    public static class Reduce extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable> {
    //全局linenum来代表key的位次
        private static IntWritable linenum = new IntWritable(1);

        public void reduce(IntWritable key,Iterable<IntWritable> values,Context context) throws
                IOException,InterruptedException{
            for(IntWritable val : values){
                context.write(linenum,key);
                System.out.println(linenum+"    "+key);
                linenum = new IntWritable(linenum.get()+1);
            }
        }
    }

    public  int run(String[] args) throws Exception{
        Configuration aaa = new Configuration();
        Job job = Job.getInstance(aaa);
        String InputPaths = "/usr/local/idea-IC-139.1117.1/Hadoop/out/datainput/sort.txt";
        String OutputPath = "/usr/local/idea-IC-139.1117.1/Hadoop/out/dataout/";

        job.setJarByClass(Sort.class);
        job.setJobName("Sort");

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        job.setMapperClass(Map.class);
        job.setReducerClass(Reduce.class);
        FileInputFormat.setInputPaths(job, new Path(InputPaths));
        FileOutputFormat.setOutputPath(job, new Path(OutputPath));
        job.setOutputKeyClass(IntWritable.class);
        job.setOutputValueClass(IntWritable.class);

        boolean success = job.waitForCompletion(true);
        return success ? 0 : 1;


    }

    public static void main(String[] args) throws Exception{
        int ret = ToolRunner.run(new Sort(),args);
        System.exit(ret);
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-05-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档