1 public class TopK extends Configured implements Tool {

     public static class TopKMapper extends Mapper<Object, Text, NullWritable, LongWritable> {

         public static final int K = 100;
private TreeMap<Long, Long> tm = new TreeMap<Long, Long>(); @Override
protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
try {
long k = Integer.parseInt(value.toString().substring(0, 9));
tm.put(k, k);
if (tm.size() > K) {
tm.remove(tm.firstKey());
}
} catch (Exception e) {
context.getCounter("TopK", "errorlog").increment(1);
}
} @Override
protected void cleanup(Context context) throws IOException, InterruptedException {
for (Long text : tm.values()) {
context.write(NullWritable.get(), new LongWritable(text));
}
}
} public static class TopKReducer extends Reducer<NullWritable, LongWritable, NullWritable, LongWritable> { public static final int K = 100;
private TreeMap<Long, Long> mt = new TreeMap<Long, Long>(); @Override
protected void reduce(NullWritable key, Iterable<LongWritable> values, Context context)
throws IOException, InterruptedException {
for (LongWritable value : values) {
mt.put(value.get(), value.get());
if (mt.size() > K) {
mt.remove(mt.firstKey());
}
}
for (Long val : mt.descendingKeySet()) {
context.write(NullWritable.get(), new LongWritable(val));
}
} } @Override
public int run(String[] args) throws Exception {
Configuration conf = getConf();
Job job = new Job(conf, "TopKNum");
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(LongWritable.class);
job.setMapperClass(TopKMapper.class);
job.setReducerClass(TopKReducer.class);
job.setJarByClass(TopK.class);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class); return job.waitForCompletion(true) ? 0 : 1;
} public static void main(String[] args) throws IOException, InterruptedException {
try {
if (args.length < 2) {
System.err.println("ERROR: Parameter format length ");
System.exit(0);
}
int ret = ToolRunner.run(new TopK(), args);
System.exit(ret);
} catch (Exception e) {
e.printStackTrace();
}
}
}

上面是求最大100个,如果求最小的100 个数,改map和reduce中的mt.remove(mt.firstKey());为mt.remove(mt.lastKey())

来自:http://blog.csdn.net/liuzhoulong/article/details/11175381

Mapreduce实例-Top Key的更多相关文章

  1. MapReduce实例2(自定义compare、partition)& shuffle机制

    MapReduce实例2(自定义compare.partition)& shuffle机制 实例:统计流量 有一份流量数据,结构是:时间戳.手机号.....上行流量.下行流量,需求是统计每个用 ...

  2. MapReduce实例&YARN框架

    MapReduce实例&YARN框架 一个wordcount程序 统计一个相当大的数据文件中,每个单词出现的个数. 一.分析map和reduce的工作 map: 切分单词 遍历单词数据输出 r ...

  3. MapReduce实例浅析

    在文章<MapReduce原理与设计思想>中,详细剖析了MapReduce的原理,这篇文章则通过实例重点剖析MapReduce 本文地址:http://www.cnblogs.com/ar ...

  4. MapReduce实例-NASA博客数据频度简单分析

    环境: Hadoop1.x,CentOS6.5,三台虚拟机搭建的模拟分布式环境,gnuplot, 数据:http://ita.ee.lbl.gov/html/contrib/NASA-HTTP.htm ...

  5. MapReduce实例

    1.WordCount(统计单词) 经典的运用MapReuce编程模型的实例 1.1 Description 给定一系列的单词/数据,输出每个单词/数据的数量 1.2 Sample a is b is ...

  6. MapReduce实例-基于内容的推荐(一)

    环境: Hadoop1.x,CentOS6.5,三台虚拟机搭建的模拟分布式环境 数据:下载的amazon产品共同采购网络元数据(需FQ下载)http://snap.stanford.edu/data/ ...

  7. MapReduce实例-倒排索引

    环境: Hadoop1.x,CentOS6.5,三台虚拟机搭建的模拟分布式环境 数据:任意数量.格式的文本文件(我用的四个.java代码文件) 方案目标: 根据提供的文本文件,提取出每个单词在哪个文件 ...

  8. MapReduce实例——求平均值,所得结果无法写出到文件的错误原因及解决方案

    1.错误原因 mapreduce按行读取文本,map需要在原有基础上增加一个控制语句,使得读到空行时不执行write操作,否则reduce不接受,也无法输出到新路径. 2.解决方案 原错误代码 pub ...

  9. MapReduce实例(数据去重)

    数据去重: 原理(理解):Mapreduce程序首先应该确认<k3,v3>,根据<k3,v3>确定<k2,v2>,原始数据中出现次数超过一次的数据在输出文件中只出现 ...

随机推荐

  1. [Visual Studio] 重置默认设置 还原默认设置

    恢复默认设置的2种方法 如果VS出现问题或设置变乱,可以通过恢复默认设置使之回到安装成功时的状态,从而解决出现的问题.VS恢复默认设置的方法有2种,分别是:通过“导入和导出设置”实现和通过命令实现. ...

  2. GIT(2)----入门资料,分支管理,冲突解决

    最近一直使用者GIT发现使用起来确实很不错,最近做些整理总结,发现了一些很不错的资料,收集在这里,以备忘. GIT入门挺简单的,之前有些过一篇文章,关于GIT的,但是都是一些生硬的操作,并没有系统的学 ...

  3. 【原】MyBatis执行DDL:create table,drop table等等

    [前言] 对MyBatis一直停留在仅仅会用的阶段,常用的场景就是通过MyBatis对表数据进行DML(insert, delete, update等)操作,从来没有想过通过MyBatis对数据库 进 ...

  4. html div 宽度随着浏览器自动适应

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Http请求之基于HttpUrlConnection,支持Header,Body传值,支持Multipart上传文件:

    Http请求之基于HttpUrlConnection,支持Header,Body传值,支持Multipart上传文件: public static String post(String actionU ...

  6. unity 质量设置 Quality Settings

    Unity allows you to set the level of graphical quality it will attempt to render. Generally speaking ...

  7. [翻译] MCProgressView 使用自定义图片做进度显示

    MCProgressView 使用自定义图片做进度显示 https://github.com/Baglan/MCProgressView Progress bar view with custom i ...

  8. 通过修改基表(link$)让非public dblink变为public

    有些朋友创建了一个非public的dblink,现在该数据库的其他用户需要去使用该dblink,在正常情况下无访问权限,需要重新建一个dblink,或者将原dblink修改为public.但是由于忘记 ...

  9. c++反汇编与逆向分析 小结

    第一章  熟悉工作环境和相关工具 1.1 熟悉OllyDBG  操作技巧 1.2 反汇编静态分析工具 IDA(最专业的逆向工具)     快捷键    功能     Enter     跟进函数实现 ...

  10. dlib landmark+人面识别

    #include "stdafx.h" #include <dlib/image_processing/frontal_face_detector.h> #includ ...