03Hadoop的TopN的问题
TopN的问题分为两种:一种是建是唯一的,还有是建非唯一。我们这边做的就是建是唯一的。
这里的建指得是:下面数据的第一列。
有一堆数据,想根据第一列找出里面的Top10.
如下:

关键:在map和reduce阶段都使用了TreeMap这个数据结构,他有从小到大的排序功能,所以排第一的最小,依次增大。限定大小为10 ,只要超过十,就把排在第一个的值给删除。
代码如下:
package com.book.topn; import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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; public class TopN { static class Mapper1 extends Mapper<LongWritable, Text, NullWritable, Text> {
public SortedMap<Double, Text> top10cats = new TreeMap<Double, Text>();
public int N = 10; @Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, NullWritable, Text>.Context context)
throws IOException, InterruptedException { String[] lines = value.toString().split(",");
Double weight = Double.parseDouble(lines[0]);
// 一行读完,然后把数据
top10cats.put(weight, new Text(value)); // 如果Map
if (top10cats.size() > N) {
top10cats.remove(top10cats.firstKey());
}
} // 待执行完map的读取比较操作后,就把TreeMap里面的数据打印出来。
@Override
protected void cleanup(Mapper<LongWritable, Text, NullWritable, Text>.Context context)
throws IOException, InterruptedException { Set<Double> set = top10cats.keySet(); Iterator<Double> iterator = set.iterator(); while (iterator.hasNext()) { context.write(NullWritable.get(), top10cats.get(iterator.next()));
} } } static class reduce1 extends Reducer<NullWritable, Text, NullWritable, Text> { SortedMap<Double, Text> finalTop = new TreeMap<Double, Text>();
private int N = 10; @Override
protected void reduce(NullWritable arg0, Iterable<Text> values,
Reducer<NullWritable, Text, NullWritable, Text>.Context context)
throws IOException, InterruptedException { for (Text value : values) { String[] finalresult = value.toString().split(","); finalTop.put(Double.parseDouble(finalresult[0]), new Text(value));
if (finalTop.size() > N) {
finalTop.remove(finalTop.firstKey());
}
; } Set<Double> set = finalTop.keySet(); Iterator<Double> iterator = set.iterator(); // 依次写入到文件中
while (iterator.hasNext()) { context.write(NullWritable.get(), finalTop.get(iterator.next()));
} } } public static void main(String[] args) throws Exception, IOException { Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(TopN.class); job.setMapperClass(Mapper1.class);
job.setReducerClass(reduce1.class); job.setMapOutputKeyClass(NullWritable.class);
job.setMapOutputValueClass(Text.class); job.setOutputValueClass(NullWritable.class);
job.setOutputKeyClass(Text.class); // 指定输入的数据的目录
FileInputFormat.setInputPaths(job, new Path("/Users/mac/Desktop/TopN.txt")); FileOutputFormat.setOutputPath(job, new Path("/Users/mac/Desktop/flowresort")); boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1); } }
结果:

注意点:

上面的注意点一定要切记。
03Hadoop的TopN的问题的更多相关文章
- storm入门(二):关于storm中某一段时间内topN的计算入门
刚刚接触storm 对于滑动窗口的topN复杂模型有一些不理解,通过阅读其他的博客发现有两篇关于topN的非滑动窗口的介绍.然后转载过来. 下面是第一种: Storm的另一种常见模式是对流式数据进行所 ...
- 【mysql】一维数据TopN的趋势图
创建数据表语句 数据表数据 对上述数据进行TopN排名 select severity,sum(count) as sum from widgt_23 where insertTstamp>=' ...
- 【转载】使用LFM(Latent factor model)隐语义模型进行Top-N推荐
最近在拜读项亮博士的<推荐系统实践>,系统的学习一下推荐系统的相关知识.今天学习了其中的隐语义模型在Top-N推荐中的应用,在此做一个总结. 隐语义模型LFM和LSI,LDA,Topic ...
- QL查询案例:取得分组 TOP-N
[转]SQL查询案例:取得分组 TOP-N CREATE TABLE TopnTest ( name VARCHAR(10), --姓名 procDate DATETIME, ...
- 使用LFM(Latent factor model)隐语义模型进行Top-N推荐
最近在拜读项亮博士的<推荐系统实践>,系统的学习一下推荐系统的相关知识.今天学习了其中的隐语义模型在Top-N推荐中的应用,在此做一个总结. 隐语义模型LFM和LSI,LDA,Topic ...
- 大数据算法设计模式(1) - topN spark实现
topN算法,spark实现 package com.kangaroo.studio.algorithms.topn; import org.apache.spark.api.java.JavaPai ...
- topN 算法 以及 逆算法(随笔)
topN 算法 以及 逆算法(随笔) 注解:所谓的 topN 算法指的是 在 海量的数据中进行排序从而活动 前 N 的数据. 这就是所谓的 topN 算法.当然你可以说我就 sort 一下 排序完了直 ...
- pyspark进行词频统计并返回topN
Part I:词频统计并返回topN 统计的文本数据: what do you do how do you do how do you do how are you from operator imp ...
- TOP-N类查询
Top-N查询 --Practices_29:Write a query to display the top three earners in the EMPLOYEES table. Displa ...
随机推荐
- 03爬虫 爬取hfutxc成绩
#-*- coding:utf-8 -*- # -*- coding: utf-8 -*- #encoding:utf-8 import urllib import urllib2 import co ...
- java加载类的顺序
一.什么时候会加载类?使用到类中的内容时加载:有三种情况1.创建对象:new StaticCode();2.使用类中的静态成员:StaticCode.num=9; StaticCode.show() ...
- Egret引擎的常用倒计时
直接上代码, private timeControl() { let timer: egret.Timer = segret.Timer(); timer.addEventListener(egret ...
- 逻辑回归与神经网络还有Softmax regression的关系与区别
本文讨论的关键词:Logistic Regression(逻辑回归).Neural Networks(神经网络) 之前在学习LR和NN的时候,一直对它们独立学习思考,就简单当做是机器学习中的两个不同的 ...
- Word2vec的Skip-Gram 系列1
转自雷锋网的一篇很棒的文章,写的通俗易懂.自己消化学习了.原文地址是 https://www.leiphone.com/news/201706/PamWKpfRFEI42McI.html 这次的分享主 ...
- google 地址
http://ec2-54-250-200-50.ap-northeast-1.compute.amazonaws.com/ http://www.joesauve.com/async-dapper- ...
- JAVA自学笔记11
JAVA自学笔记11 1:Eclipse的安装 2:用Eclipse写一个HelloWorld案例,最终在控制台输出你的名字 A:创建项目 B:在src目录下创建包.cn.itcast C:在cn.i ...
- HTML5 学习07——Video(视频)Audio(音频)
<video> 元素:提供了 播放.暂停和音量控件来控制视频. width 和 height 属性:控制视频的尺寸 <video> 与</video> 标签之间插入 ...
- 演练:调试 Windows 窗体
Windows 窗体是最常见的托管应用程序之一. Windows 窗体创建标准的 Windows 应用程序. 你可以完成此演练使用 Visual Basic 中, C#,或 c + +. 首先,您必须 ...
- 通过Pdf预览Excel或者word或者Powerpoint (C#将Office转换为PDF)
下面代码是Excel转换为PDF using System; using System.Collections.Generic; using System.Linq; using System.Web ...