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()));
            }

        }
    }
}
相关推荐
小范馆1 小时前
C++ 编译方法对比:分步编译 vs 一步到位
java·开发语言·c++
ascarl20102 小时前
记录一下Nacos和XXLJOB修复漏洞
java
福娃筱欢2 小时前
通用机KESV8R2-3节点集群缩容为2节点
java·开发语言
LXMXHJ2 小时前
项目之html+javaScript
java·vue
毕设源码-赖学姐2 小时前
【开题答辩全过程】以 高校竞赛试题库管理平台为例,包含答辩的问题和答案
java
南山乐只2 小时前
Java并发原生工具:原子类 (Atomic Classes)
java·开发语言·后端
一颗青果2 小时前
C++下的atomic | atmoic_flag | 内存顺序
java·开发语言·c++
木叶子---2 小时前
pdf生成排查记录与解决方案
java·pdf
Sylvia-girl2 小时前
Java之异常
java·开发语言