MapReduce输入文件排序

 

 

import java.io.IOException;



import org.apache.hadoop.conf.Configuration;

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.output.FileOutputFormat;



public class Sort {



    //map

    public static class SortMapper extends Mapper<Object,Text,IntWritable,IntWritable>{

        public void map(Object key,Text value,Context context)throws IOException,InterruptedException{

            String text = value.toString();

            int t = Integer.parseInt(text);

            context.write(new IntWritable(t),new IntWritable(1));

        }
    }


    //reduce

    public static class SortReducer extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable>{

        int row=1;

        public void reduce(IntWritable key,Iterable<IntWritable> values,Context context)throws IOException,InterruptedException{

            for(IntWritable value:values) {

                context.write(new IntWritable(row),key);

                row=row+1;

            }
        }
    }

    //main

    public static void main(String[] args)throws Exception{

        Configuration conf=new Configuration();

        Job job=Job.getInstance(conf,"sort");

        job.setJarByClass(Sort.class);

        job.setMapperClass(SortMapper.class);

        job.setReducerClass(SortReducer.class);

        job.setOutputKeyClass(IntWritable.class);

        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.addInputPath(job,new Path("input"));

        FileOutputFormat.setOutputPath(job,new Path("output"));

        System.exit(job.waitForCompletion(true)?0:1);

    }

}

 代码部分完成后对文件进行打包,打包步骤详细内容在上一篇文章MapReduce文件合并与去重中。

按照要求在usr/local/hadoop目录下创建两个文本文件A.txt  B.txt ,之后进行如下操作:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值