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()));
}
}
}
}
mr-----topn的用法
shjita2026-01-20 11:40
相关推荐
Wang's Blog13 小时前
Java 接入Redis: 服务启动停止与后台运行配置此时不提桶,更待何时13 小时前
01-04-B-垃圾回收面试与生产事故实战鹿角片ljp15 小时前
KV Cache 解析captain37615 小时前
网络原理(9)-数据链路层惊讶的猫16 小时前
意图识别置信度和检索重排的分数caoerzhong16 小时前
仓库管理数字化趋势解读:JeeWMS 开源 Java WMS 视角下的五个演进方向程序员JerrySUN16 小时前
Jetson Edge AI 实战01:Nano、Xavier、Orin 怎么选?Jetson 硬件选型详解【视频讲解】Joy T17 小时前
Spring AI 2.0 Agent 进阶:Memory、State 与 Context Engineering 常见技术全景估值探索者17 小时前
【Python量化系统工程化 #08】关了 SSH 就停?systemd 让脚本开机自启 + 异常自动拉起