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

        }
    }
}
相关推荐
云烟成雨TD14 小时前
Spring AI Alibaba 1.x 系列【6】ReactAgent 同步执行 & 流式执行
java·人工智能·spring
于慨14 小时前
Lambda 表达式、方法引用(Method Reference)语法
java·前端·servlet
swg32132114 小时前
Spring Boot 3.X Oauth2 认证服务与资源服务
java·spring boot·后端
gelald14 小时前
SpringBoot - 自动配置原理
java·spring boot·后端
殷紫川14 小时前
深入理解 AQS:从架构到实现,解锁 Java 并发编程的核心密钥
java
一轮弯弯的明月14 小时前
贝尔数求集合划分方案总数
java·笔记·蓝桥杯·学习心得
chenjingming66614 小时前
jmeter线程组设置以及串行和并行设置
java·开发语言·jmeter
殷紫川14 小时前
深入拆解 Java volatile:从内存屏障到无锁编程的实战指南
java
eddieHoo15 小时前
查看 Tomcat 的堆内存参数
java·tomcat
那个失眠的夜15 小时前
Mybatis延迟加载策略
xml·java·数据库·maven·mybatis