Hadoop实战-MapReduce之max、min、avg统计(六)
1、数据准备:
Mike,35
Steven,40
Ken,28
Cindy,32
2、预期结果
Max 40
Min 28
Avg 33
3、MapReduce代码如下
import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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;
import org.apache.hadoop.util.GenericOptionsParser; public class AgeMapReduce { public static class WordCountMapper extends
Mapper<Object, Text, Text, Text> {
private Text nameKey = new Text();
private Text ageValue = new Text(); @Override
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
String content = itr.nextToken();
String[] nameAndAge = content.split(",");
//String name = nameAndAge[0];
String age = nameAndAge[1];
nameKey.set("only you");
ageValue.set(age);
context.write(nameKey, ageValue);
}
}
} public static class WordCountReduce extends Reducer<Text, Text, Text, Text> {
private int min = Integer.MAX_VALUE;
private int max = 0;
private int sum = 0;
private int count = 0; @Override
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
for (Text tmpAge : values) {
int age = Integer.valueOf(tmpAge.toString());
if (age < min) {
min = age;
}
if (age > max) {
max = age;
}
sum += age;
count++;
}
//String resultStr = min + "\t" + max + "\t" + (sum / count);
//result.set(resultStr);
context.write(new Text("Max"), new Text(String.valueOf(min)));
context.write(new Text("Min"), new Text(String.valueOf(max)));
context.write(new Text("Avg"), new Text(String.valueOf(sum/count)));
}
} public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args)
.getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: MinMaxCountDriver <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "StackOverflow Comment Date Min Max Count");
job.setJarByClass(AgeMapReduce.class);
job.setMapperClass(WordCountMapper.class);
// job.setCombinerClass(MusicReduce.class);
job.setReducerClass(WordCountReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
// user/joe/wordcount/input
FileInputFormat.addInputPath(job, new Path(otherArgs[0])); // user/joe/wordcount/output
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
4、注意事项
因为输出的结果和Key没有关系,所以在map阶段要固定一个Key即可。
Hadoop实战-MapReduce之max、min、avg统计(六)的更多相关文章
- Hadoop实战-MapReduce之分组(group-by)统计(七)
1.数据准备 使用MapReduce计算age.txt中年龄最大.最小.均值name,min,max,countMike,35,20,1Mike,5,15,2Mike,20,13,1Steven,40 ...
- Hadoop实战-MapReduce之倒排索引(八)
倒排索引 (就是key和Value对调的显示结果) 一.需求:下面是用户播放音乐记录,统计歌曲被哪些用户播放过 tom LittleApple jack YesterdayO ...
- Hadoop实战-MapReduce之WordCount(五)
环境介绍: 主服务器ip:192.168.80.128(master) NameNode SecondaryNameNode ResourceManager 从服务器ip:192.168.80.1 ...
- 深入浅出Hadoop实战开发(HDFS实战图片、MapReduce、HBase实战微博、Hive应用)
Hadoop是什么,为什么要学习Hadoop? Hadoop是一个分布式系统基础架构,由Apache基金会开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力高速运 ...
- 升级版:深入浅出Hadoop实战开发(云存储、MapReduce、HBase实战微博、Hive应用、Storm应用)
Hadoop是一个分布式系统基础架构,由Apache基金会开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力高速运算和存储.Hadoop实现了一个分布式文件系 ...
- 王家林的“云计算分布式大数据Hadoop实战高手之路---从零开始”的第十一讲Hadoop图文训练课程:MapReduce的原理机制和流程图剖析
这一讲我们主要剖析MapReduce的原理机制和流程. “云计算分布式大数据Hadoop实战高手之路”之完整发布目录 云计算分布式大数据实战技术Hadoop交流群:312494188,每天都会在群中发 ...
- 6.组函数(avg(),sum(),max(),min(),count())、多行函数,分组数据(group by,求各部门的平均工资),分组过滤(having和where),sql优化
1组函数 avg(),sum(),max(),min(),count()案例: selectavg(sal),sum(sal),max(sal),min(sal),count(sal) from ...
- Hadoop实战训练————MapReduce实现PageRank算法
经过一段时间的学习,对于Hadoop有了一些了解,于是决定用MapReduce实现PageRank算法,以下简称PR 先简单介绍一下PR算法(摘自百度百科:https://baike.baidu.co ...
- group by与avg(),max(),min(),sum()函数的关系
数据库表: create table pay_report( rdate varchar(8), --日期 region_id varchar(4), --地市 ...
随机推荐
- AForge.NET 设置摄像头分辨率
AForge.NET 老版本在预览摄像头时可通过设置DesiredFrameSize 属性,设置摄像头支持的分辨率,新版本提示已过期: 解决办法: 获取VideoCapabilities属性集合,选中 ...
- JVM, JRE 和JDK
JVM -- java virtual machine A Java virtual machine (JVM) is a process virtual machine that can execu ...
- logging模块配置共享以及使用文件配置
1.配置共享 如果每个文件都配置logging,那就太繁琐了,logging提供了父子模块共享配置的机制, 会根据Logger的名称来自动加载父模块的配置.首先定义一个 main.py 文件: imp ...
- 利用例子来理解spring的面向切面编程(使用@Aspect)
上篇的例子,自动装配和自动检测Bean是使用注解的方式处理的,而面向切面编程是使用aop标签处理的,给我感觉就像中西医参合一样. 现在就来优化优化,全部使用注解的方式处理. 1.工程图:
- Springboot 集成 Thymeleaf 及常见错误
Thymeleaf模板引擎是springboot中默认配置,与freemarker相似,可以完全取代jsp,在springboot中,它的默认路径是src/main/resources/templat ...
- 邁向IT專家成功之路的三十則鐵律 鐵律七:IT人效率之道-時間管理
彷間有許多與時間管理方面的相關書籍與實務課程,但是究竟對於一位IT專業人士來說,甚麼樣的時間管理法則才是最有效率的呢?過去有許多IT朋友私下請教顧大俠這個問題,而顧大俠始終沒有很完整的分享這方面的經驗 ...
- Jstl indexOf 参考
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> < ...
- 本机上使用Three.js载入纹理
怎样载入纹理 // 首先, 创建一个纹理 var mapUrl = "../images/molumen_small_funny_angry_monster.jpg"; var m ...
- ZipOutputStream 用法 小计
ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile)); 构造函数之后 文件就已经创建出来了 只是 0kb s.Write(bu ...
- do export method of oracle all database tables with dmp files.
usually we need to export the database tables to backup and others use. So we must know what to do e ...