mr-----topn的用法

复制代码
package org.one;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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 java.io.IOException;
import java.util.*;

public class CleanMusicDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conn = new Configuration();
        Job job = Job.getInstance(conn);
        job.setJarByClass(CleanMusicDriver.class);
        job.setMapperClass(CleanMusicMapper.class);
        job.setReducerClass(CleanMusicReduce.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

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

        FileInputFormat.addInputPath(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        System.exit(job.waitForCompletion(true)?0:1);
    }

    private static class CleanMusicMapper extends Mapper<LongWritable,Text,Text,IntWritable> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            if(key.get()==0){
                return;
            }
            String[] strs = value.toString().split(",");
            String genre=strs[3];
            context.write(new Text(genre),new IntWritable(1));
        }
    }

    private static class CleanMusicReduce extends Reducer<Text,IntWritable,Text,IntWritable> {
        HashMap<String,Integer> hm = new HashMap<>();
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            int sum=0;
            for (IntWritable value : values) {
                sum++;
            }
            hm.put(key.toString(),sum);
        }

        @Override
        protected void cleanup(Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            ArrayList<Map.Entry<String, Integer>> ety = new ArrayList<>(hm.entrySet());
            Collections.sort(ety, new Comparator<Map.Entry<String, Integer>>() {
                @Override
                public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                    return o2.getValue()-o1.getValue();
                }
            });
            for (int i = 0; i <5;i++) {
                context.write(new Text(ety.get(i).getKey()),new IntWritable(ety.get(i).getValue()));
            }

        }
    }
}
相关推荐
毕设源码李师姐3 小时前
计算机毕设 java 基于 java 的图书馆借阅系统 智能图书馆借阅综合管理平台 基于 Java 的图书借阅与信息管理系统
java·开发语言·课程设计
忆~遂愿3 小时前
Runtime 上下文管理:计算实例的生命周期、延迟最小化与上下文切换优化
java·大数据·开发语言·人工智能·docker
powerfulhell3 小时前
寒假python作业5
java·前端·python
1尢晞13 小时前
Java学习
java·开发语言
阿杰真不会敲代码3 小时前
Mybatis-plus入门到精通
java·tomcat·mybatis
木井巳3 小时前
【递归算法】二叉搜索树中第K小的元素
java·算法·leetcode·深度优先·剪枝
qq_297574673 小时前
【实战】POI 实现 Excel 多级表头导出(含合并单元格完整方案)
java·spring boot·后端·excel
星辰_mya3 小时前
Elasticsearch线上问题之慢查询
java·开发语言·jvm
南极星10053 小时前
我的创作纪念日--128天
java·python·opencv·职场和发展
郝学胜-神的一滴3 小时前
超越Spring的Summer(一): PackageScanner 类实现原理详解
java·服务器·开发语言·后端·spring·软件构建