TopN案例
准备三份数据
t1 2067
t2 2055
t3 2055
t4 1200
t5 2367
t6 255
t7 2555
t8 12100
t9 20647
t10 245
t11 205
t12 100
t111 1067
t112 2155
t113 2065
t114 1290
t115 237
t116 25
t117 15
t118 1
t119 10647
t110 2995
t111 2057
t112 10044
t211 67
t212 55
t213 65
t214 90
t215 37
t216 425
t217 155
t218 189
t219 1047
t210 295
t211 27
t212 144
定义Mapper类
package com.hadoop.TopN;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
import java.util.TreeMap;
public class TopMapper extends Mapper<Object, Text, NullWritable, Text> {
private TreeMap<Integer, Text> map = new TreeMap<>();
@Override
protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String[] words = value.toString().split(" ");
String number = words[1];
map.put(Integer.parseInt(number), new Text(value)); //此处必须new Text,不然数组越界,大坑!
if (map.size() > 10) {
map.remove(map.firstKey());
}
}
@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
for (Text text : map.values()) {
context.write(NullWritable.get(),text);
}
}
}
定义Reducer类
package com.hadoop.TopN;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
import java.util.TreeMap;
public class TopReducer extends Reducer<NullWritable, Text, NullWritable, Text> {
private TreeMap<Integer, Text> map = new TreeMap<>();
@Override
protected void reduce(NullWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
for (Text value : values) {
String[] strs = value.toString().split(" ");
map.put(Integer.parseInt(strs[1]),new Text(value));
if (map.size() >10){
map.remove(map.firstKey());
}
}
for (Text text:map.values()){
context.write(NullWritable.get(),text);
}
}
}
编写Driver类
package com.hadoop.TopN;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class TopDriver {
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(TopDriver.class);
job.setMapperClass(TopMapper.class);
job.setReducerClass(TopReducer.class);
job.setNumReduceTasks(1); //重点
job.setMapOutputKeyClass(NullWritable.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(Text.class);
FileInputFormat.setInputPaths(job,new Path("input"));
FileOutputFormat.setOutputPath(job,new Path("output/topn"));
job.waitForCompletion(true);
}
}
输出结果part-r-00000
t113 2065
t1 2067
t112 2155
t5 2367
t7 2555
t110 2995
t112 10044
t119 10647
t8 12100
t9 20647
TopN案例的更多相关文章
- MapReduce TopN(自主复习)
1.MyTopN 主程序 package com.littlepage.topn; import org.apache.hadoop.conf.Configuration; import org.a ...
- spark 源码分析之十九 -- DAG的生成和Stage的划分
上篇文章 spark 源码分析之十八 -- Spark存储体系剖析 重点剖析了 Spark的存储体系.从本篇文章开始,剖析Spark作业的调度和计算体系. 在说DAG之前,先简单说一下RDD. 对RD ...
- 大数据技术之Hadoop(MapReduce)
第1章 MapReduce概述 1.1 MapReduce定义 1.2 MapReduce优缺点 1.2.1 优点 1.2.2 缺点 1.3 MapReduce核心思想 MapReduce核心编程思想 ...
- spark源码分析以及优化
第一章.spark源码分析之RDD四种依赖关系 一.RDD四种依赖关系 RDD四种依赖关系,分别是 ShuffleDependency.PrunDependency.RangeDependency和O ...
- Hadoop - MapReduce学习笔记(详细)
第1章 MapReduce概述 定义:是一个分布式运算程序的编程框架 优缺点:易于编程.良好的扩展性.高容错性.适合PB级以上数据的离线处理 核心思想:MapReduce 编程模型只能包含一个Map ...
- QL查询案例:取得分组 TOP-N
[转]SQL查询案例:取得分组 TOP-N CREATE TABLE TopnTest ( name VARCHAR(10), --姓名 procDate DATETIME, ...
- Scala进阶之路-统计商家id的标签数以及TopN示例案例分析
Scala进阶之路-统计商家id的标签数以及TopN示例案例分析 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.项目需求 将“temptags.txt”中的数据进行分析,统计出 ...
- 取分组TOPN好理解案例
- 阿基米德项目ALS矩阵分解算法应用案例
转自:https://github.com/ceys/jdml/wiki/ALS 阿基米德项目ALS矩阵分解算法应用案例 编写人:ceys/youyis 最后更新时间:2014.5.12 一.算法描述 ...
随机推荐
- maven:私服的相关配置
添加到settings.xml中 <server> <id>releases</id> <username>admin</username> ...
- gulp 入门使用
gulp 入门使用 使用场景 相信大家在传统的开发模式下 都是 html + css + js 然后静态文件不经过任何处理 部署到服务器,这样会有很多漏洞例如: 1.在网站上查看F12 就可以看到 ...
- struts2 --xml配置
struts2.5 官方文档介绍 这个是struts2.5给出的web.xml配置方式,核心包已经被挪到了ng的上一层 <web-app id="MyStrutsApp" v ...
- <el-upload></el-upload>组件上传图片到七牛云
[01]搭建好页面结构.定义数据与接口 <el-upload method="post" ref="upload" :action="domai ...
- react native 封装TextInput组件
上一篇 react-native文章提到了TextInput组件对安卓的适配问题,因此对该组件进行封装很有必要. 文章地址 react native定报预披项目知识点总结 TextInput介绍 官 ...
- DFA确定有限状态自动机
DFA 在计算理论中,确定有限状态自动机或确定有限自动机(英语:deterministic finite automaton, DFA)是一个能实现状态转移的自动机.对于一个给定的属于该自动机的状态和 ...
- Mysql 查询当月时间数据
SELECTDATE_FORMAT(CURDATE(), '%Y%m'), DATE_FORMAT(t.transactiontime, '%Y%m'),t.*FROM ttransactions t ...
- plsql 永久注册码适用个版本
注册码:Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqzserial Number:601769 password:xs374ca
- Vue(服务端渲染)
一.前言 1.服务端渲染图解 2.简介服务端渲染 ...
- 人生苦短,Let's Go目录
目录 GO语言系列(一)- 初识go语言 GO语言系列(二)- 基本数据类型和操作符 Go语言系列(三)- 基础函数和流程控制 GO语言系列(四)- 内置函数.闭包与高级数据类型 GO语言系列(五)- ...