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统计(六)的更多相关文章

  1. Hadoop实战-MapReduce之分组(group-by)统计(七)

    1.数据准备 使用MapReduce计算age.txt中年龄最大.最小.均值name,min,max,countMike,35,20,1Mike,5,15,2Mike,20,13,1Steven,40 ...

  2. Hadoop实战-MapReduce之倒排索引(八)

    倒排索引 (就是key和Value对调的显示结果) 一.需求:下面是用户播放音乐记录,统计歌曲被哪些用户播放过 tom        LittleApple jack       YesterdayO ...

  3. Hadoop实战-MapReduce之WordCount(五)

    环境介绍: 主服务器ip:192.168.80.128(master)  NameNode  SecondaryNameNode ResourceManager 从服务器ip:192.168.80.1 ...

  4. 深入浅出Hadoop实战开发(HDFS实战图片、MapReduce、HBase实战微博、Hive应用)

    Hadoop是什么,为什么要学习Hadoop?     Hadoop是一个分布式系统基础架构,由Apache基金会开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力高速运 ...

  5. 升级版:深入浅出Hadoop实战开发(云存储、MapReduce、HBase实战微博、Hive应用、Storm应用)

          Hadoop是一个分布式系统基础架构,由Apache基金会开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力高速运算和存储.Hadoop实现了一个分布式文件系 ...

  6. 王家林的“云计算分布式大数据Hadoop实战高手之路---从零开始”的第十一讲Hadoop图文训练课程:MapReduce的原理机制和流程图剖析

    这一讲我们主要剖析MapReduce的原理机制和流程. “云计算分布式大数据Hadoop实战高手之路”之完整发布目录 云计算分布式大数据实战技术Hadoop交流群:312494188,每天都会在群中发 ...

  7. 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 ...

  8. Hadoop实战训练————MapReduce实现PageRank算法

    经过一段时间的学习,对于Hadoop有了一些了解,于是决定用MapReduce实现PageRank算法,以下简称PR 先简单介绍一下PR算法(摘自百度百科:https://baike.baidu.co ...

  9. group by与avg(),max(),min(),sum()函数的关系

    数据库表: create table pay_report(     rdate varchar(8),     --日期     region_id varchar(4),    --地市      ...

随机推荐

  1. Scrapy学习-16-动态网页技术

    Selenium浏览器自动化测试框架 简介 Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样. 支持的浏览器包括IE(7, 8, ...

  2. LeetCode OJ——Minimum Depth of Binary Tree

    http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ 贡献了一次runtime error,因为如果输入为{}即空的时候,出现了c ...

  3. js-input file 文件上传(照片,视频,音频)

    在此做一个笔记 <input type="file" accept="image/*" capture="camera"> &l ...

  4. c实现的trim函数

    功能:去掉字符串首尾的空格,换行符等空白. 代码: #include <string.h> #include <stdio.h> #include <ctype.h> ...

  5. mac下安装pyQt4

    1.首先安装QT,同时要有gcc 2.然后就是先安装sip,然后安装pyqt4 python configure.py -q /usr/bin/qmake-4.8 -d /Library/Python ...

  6. C# 生成二维码(带Logo)

    C# 生成二维码(带Logo) 第一种方式 我们需要引用 ThoughtWorks.QRCode.dll  生成带logo二维码(framework4.0以上) 下载地址:https://pan.ba ...

  7. 继承LIst 的类JSON序列化,无法序列化属性的问题

    /// <summary> /// Paged list /// </summary> /// <typeparam name="T">T< ...

  8. SpringMVC整合fastdfs-client-java实现web文件上传下载

    原文:http://blog.csdn.net/wlwlwlwl015/article/details/52682153 本篇blog主要记录一下SpringMVC整合FastDFS的Java客户端实 ...

  9. 【翻译自mos文章】当并行事务恢复进程在执行时,禁用并行事务恢复的方法

    当并行事务恢复进程在执行时,禁用并行事务恢复的方法 How to Disable Parallel Transaction Recovery When Parallel Txn Recovery is ...

  10. makefile 与android.mk中加信息打印

    makefile里面加打印: [table]@echo ' zImage - Compressed kernel image' android.mk里面加信息打印: $(warning TEXT... ...