MapReduce的全局TopN简单实现

public class TopN {

    public static void main(String[] args) throws Exception {
        //1.获取job对象
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);

        String input = "data/top.data";
        String output = "out/";
        FileUtils.deleteTarget(output, configuration);

        //2.获取jar的相关信息
        job.setJobName("WordCount");
        job.setJarByClass(TopN.class);

        //3.设置自定义的Mapper和Reducer
        job.setMapperClass(MyMapper.class);
        job.setReducerClass(MyReducer.class);

        //4.Mapper阶段输出的类型
        job.setMapOutputKeyClass(MyIntwritable.class);
        job.setMapOutputValueClass(Text.class);

        //5.Reducer阶段输出的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(MyIntwritable.class);

        //6.设置输入输出路径
        FileInputFormat.setInputPaths(job, new Path(input));
        FileOutputFormat.setOutputPath(job, new Path(output));

        //7.提交job
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }

    public static final int TOPN = 3;

    /**
     * 为什么要自定义IntWritable继承类?
     * 因为要实现降序
     */
    public static class MyIntwritable extends IntWritable {
        public MyIntwritable() {
        }

        //因为要传入int类型值,必须定义一个有参构造器。
        public MyIntwritable(int value){
            super(value);
        }

        //自定义降序,默认升序
        @Override
        public int compareTo(IntWritable o) {
            return -super.compareTo(o);
        }
    }

    public static class MyMapper extends Mapper<LongWritable, Text, MyIntwritable, Text> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] splits = value.toString().split(",");
            String product = splits[0];
            int price = Integer.parseInt(splits[1]);
            context.write(new MyIntwritable(price), new Text(product));
        }
    }

    public static class MyReducer extends Reducer<MyIntwritable, Text, Text, MyIntwritable> {
        int index = 0;
        @Override
        protected void reduce(MyIntwritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            for (Text value : values)
            {
                if (index < TOPN){
                    context.write(value, key);
                }
                index++;
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值