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. UIAutomator2.0初始

    1. 先直接上样例,谷歌官方Demo: https://github.com/googlesamples/android-testing 2. 一句话说明改动思路 Most importantly, ...

  2. Mac安装homebrew安装到指定目录

    第一种直接安装在/usr/local目录下 mac 打开终端输入 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebr ...

  3. 在MyEclipse上安装svn插件

    最近需要用到myeclipse做一个商城的项目开发,用svn作为项目的版本控制软件.但是在myeclipse上安装svn插件就是装不好,反复折腾了好几次都安装不成功.网上提供的安装办法有两种,一是:在 ...

  4. Oracle 11g 错误:ORA-28002: the password will expire within 7 days 解决方法

    ERROR:ORA-28002: the password will expire within 7 days 错误是提示password快过期了,有两个办法解决问题. 一. 改动已经报错用户的pas ...

  5. Extjs DateField onchange

    1 开发思路: 在日期值变化的事件中获得选择后的日期值,传给后台,然后从后台加载相应的数据 2 问题:在查看extjs2.2 的api的官方说明文档,文档对datefield组件的change事件说明 ...

  6. 经典数独游戏+数独求解器—纯C语言实现

    "心常乐数独小游戏"(下面简称"本软件")是一款windows平台下的数独游戏软件. 本软件是开源.免费软件. 本软件使用纯C语言编写,MinGW编译,NSIS ...

  7. git 经常使用操作集锦

    创建仓库 新建普通仓库: jxdong@ubuntu-server:~/workspace/git$ git init Reinitialized existing Git repository in ...

  8. int to string

    int i=9; stringstream stream; stream << i;stream.str()

  9. Common Internet File System

    CIFS (Common Internet File System) is a protocol that gained popularity around the year 2000, as ven ...

  10. spark读取 kafka nginx网站日志消息 并写入HDFS中(转)

    原文链接:spark读取 kafka nginx网站日志消息 并写入HDFS中 spark 版本为1.0 kafka 版本为0.8 首先来看看kafka的架构图 详细了解请参考官方 我这边有三台机器用 ...