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

        }
    }
}
相关推荐
二月夜5 小时前
剖析Java正则表达式回溯问题
java·正则表达式
xuhaoyu_cpp_java6 小时前
项目学习(三)分页查询
java·经验分享·笔记·学习
程序员二叉6 小时前
【Java】集合面试全套精讲|HashMap/ArrayList高频考点完整版
java·面试·哈希算法
cfm_29146 小时前
JVM GC垃圾回收初步了解
java·开发语言·jvm
心之伊始7 小时前
LangChain4j RAG 实战:Java 后端如何把本地文档接入 Embedding 检索链路
java·架构·源码分析·csdn
许彰午7 小时前
17_synchronized关键字深度解析
java·开发语言
Xzh04238 小时前
AI Agent 学习路线(Java 后端方向)
java·人工智能·学习
艾利克斯冰9 小时前
Java 设计模式-行为型模式(更新中)
java·开发语言·设计模式
倒霉蛋小马9 小时前
Java新特性:record关键字
java·开发语言
折哥的程序人生 · 物流技术专研10 小时前
《Java 100 天进阶之路》第95篇:消息队列基础(RocketMQ/Kafka)(2026版)
java·面试·kafka·rocketmq·java-rocketmq·求职招聘