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
相关推荐
别动我齐刘海9 小时前
C++ 工程化学习code2cat10 小时前
Java进阶篇之AtomicLongFieldUpdater:把累计额度留在对象字段中WeiXin_DZbishe10 小时前
基于springboot城市公交实时运营与调度系统-计算机毕设 附源码【课程设计】66173泡茶喝茶写代码10 小时前
A股量化数据工程:从 REST 接口到策略信号(第 18 篇):放量缩量异动扫描wewowilling10 小时前
认识微服务代码什么用11 小时前
Java Web 7-10蜗牛互联网12 小时前
GPT-6 与 Intelligent UI:从回答到可审计操作的产品迁移SL_staff13 小时前
运维救火实录:钉钉考勤API变更引发工资计算异常,我们用JVS-Logic可视化逻辑引擎实现分钟级热修复SunnyDays101113 小时前
Java 查找并高亮 Word 文字:精确查找及正则表达式模糊查找