Mapreduce编程TOP N
来源:互联网 发布:mac地址修改 编辑:程序博客网 时间:2024/06/02 03:38
通过treeset 取出TOP N的数据,下面的程序是有缺陷的,因为tree set本身不支持相同数据。
另外使用了cleanup方法, setup 和 cleanup在mapreduce只会跑一次, 从 input读取数据之后,map默认按照行来一行一行读取,也就是循环的,直到读完数据,所以一些初始化工作可以放到setup里去做, cleanup用来清理一些变量,既然执行一次,那我就通过在cleanup里使用方法去除多余数据,只去我要的TOP N,再发送给reduce, 实际这个程序要不要reduce无所谓。
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import org.apache.commons.net.nntp.NewsgroupInfo;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.SplitLineReader;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class TopTenOrder {
public static class TokenizerMapper extends Mapper<Object, Text, NullWritable, IntWritable> {
private TreeSet<Integer> top10 = new TreeSet<Integer>();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
top10.add(Integer.parseInt(value.toString()));
}
public void cleanup(Context context) throws IOException, InterruptedException {
while(top10.size() > 10){
top10.remove(top10.first());
}
Iterator iterator = top10.iterator();
while(iterator.hasNext()){
context.write(NullWritable.get(), new IntWritable(Integer.parseInt(iterator.next().toString())));
}
}
}
public static class IntSumReducer extends Reducer<NullWritable, IntWritable, NullWritable, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> value, Context context)
throws IOException, InterruptedException {
for(IntWritable val : value){
context.write(NullWritable.get(), val);
}
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "TopTenOrder");
job.setJarByClass(TopTenOrder.class);
job.setMapperClass(TokenizerMapper.class);
// job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(IntWritable.class);
job.setNumReduceTasks(1);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
阅读全文
0 0
- Mapreduce编程TOP N
- Top N的MapReduce程序MapReduce for Top N items
- MapReduce算法形式五:TOP—N
- MapReduce功能实现三---Top N
- Top N之MapReduce程序加强版Enhanced MapReduce for Top N items
- mapreduce编程实例(6)-求TOP 10
- [MapReduce]Top N 任务的mapper
- 分组Top N问题(二) - Hadoop MapReduce实现
- top n
- top n
- mapreduce top K实现
- mapreduce实现Top K
- mapreduce Top K算法
- Mapreduce实例-Top Key
- SELECT TOP N 问题
- oracle top n 问题
- oracle top n 问题
- Hiberate top n
- 【Angular】refresher刷新器
- jq 第二次选中没效果
- JAVA——设计模式之单例模式
- codevs 1152 细胞分裂
- C++程序员是如何评价GO的
- Mapreduce编程TOP N
- 在MySQL中如何让结果集返回行号
- Android StatusBar 状态栏颜色设置
- java中的基本数据类型一定存储在栈中吗?
- bootbox模态框自定义dialog,fonfirm,alert控件
- JMS简介与ActiveMQ实战
- 如何将TOMCAT注册成系统服务
- ELF相关文章
- 王者荣耀的帧同步机智