MapReduce 按照Value值进行排序输出

文件输入:

A    1
B    5
C    4
E    1
D    3
W    9
P    7
Q    2

文件输出:

W    9
P    7
B    5
C    4
D    3
Q    2
E    1
A    1

代码如下:

 

package comparator;

import java.io.IOException;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.IntWritable.Comparator;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
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 comparator{

    /**
     * @param args
     * @throws IOException 
     * @throws IllegalArgumentException 
     * @throws InterruptedException 
     * @throws ClassNotFoundException 
     */
    public static class myComparator extends Comparator {
        @SuppressWarnings("rawtypes")
        public int compare( WritableComparable a,WritableComparable b){
            return -super.compare(a, b);
        }
        public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
            return -super.compare(b1, s1, l1, b2, s2, l2);
        }
    }
    
    public static class Map extends Mapper<Object,Text,IntWritable,Text>{
        public void map(Object key,Text value,Context context) throws NumberFormatException, IOException, InterruptedException{
            String[] split = value.toString().split("\t");
            context.write(new IntWritable(Integer.parseInt(split[1])),new Text(split[0]) );
        }
    }
    public static class Reduce extends Reducer<IntWritable,Text,Text,IntWritable>{
        public void reduce(IntWritable key,Iterable<Text>values,Context context) throws IOException, InterruptedException{
            for (Text text : values) {
                context.write( text,key);
            }
        }
    }
    public static void main(String[] args) throws IllegalArgumentException, IOException, ClassNotFoundException, InterruptedException {
        // TODO Auto-generated method stub

           Job job = new Job();
           job.setJarByClass(comparator.class);
           
           job.setNumReduceTasks(1);   //设置reduce进程为1个,即output生成一个文件
           
           job.setMapperClass(Map.class);      
           job.setReducerClass(Reduce.class);
           
           job.setMapOutputKeyClass(IntWritable.class);
            job.setMapOutputValueClass(Text.class);
           
           job.setOutputKeyClass(Text.class);    //为job的输出数据设置key类
           job.setOutputValueClass(IntWritable.class);   //为job的输出设置value类
           
           job.setSortComparatorClass( myComparator.class);           //自定义排序
           
           FileInputFormat.addInputPath(job, new Path(args[0]));   //设置输入文件的目录
           FileOutputFormat.setOutputPath(job,new Path(args[1])); //设置输出文件的目录
           
           System.exit(job.waitForCompletion(true)?0:1);   //提交任务
    }

}

 


搜索与推荐Wiki

扫一扫 关注微信公众号!号主 专注于搜索和推荐系统,尝试使用算法去更好的服务于用户,包括但不局限于机器学习,深度学习,强化学习,自然语言理解,知识图谱,还不定时分享技术,资料,思考等文章!


                             【技术服务】,详情点击查看:https://mp.weixin.qq.com/s/PtX9ukKRBmazAWARprGIAg


外包服务

 

  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值