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. 打印倒序NxN乘法表

    一.实验要求: 给定任意一个字符N(N>0),然后打印NxN的倒序乘法表. 二.解决问题: #/!bin/bash# #define functionNxN_fun(){ local i=$1 ...

  2. Spring MVC学习一

    SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是DispatcherServlet,DispatcherServlet负责转发每一个Request请 ...

  3. LeetCode OJ--Permutations *

    https://oj.leetcode.com/problems/permutations/ 写出一列数的全排列 #include <iostream> #include <vect ...

  4. Codeforces Gym101502 H.Eyad and Math-换底公式

    H. Eyad and Math   time limit per test 2.0 s memory limit per test 256 MB input standard input outpu ...

  5. echarts判断点击参数类型,series为有效,markPoint 无效

    https://www.w3cschool.cn/echarts_tutorial/echarts_tutorial-7o3u28yh.html 可以设置如果点击的是markPoint,直接返回

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

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

  7. assigning to uiimagepickercontrollerdelegate from incompatible type

    I have added a UIImagePickerController to a UIViewController. I have assigned the UIImagePickerContr ...

  8. 邁向IT專家成功之路的三十則鐵律 鐵律十九:IT人待業之道-寬心

    說來很多人可能不相信,筆者從來不把失業當作是一件嚴重的事,相反的我會把它當作是一個很好的轉機.針對一個隨時做好準備的IT人,三個月或半年沒有上班完全沒有甚麼好擔心的.只是如何善用待業的時間,說實在的真 ...

  9. tar命令中的-C作用

    一直不知道解压命令如何指定文件夹,今天学到了一个 -C 参数 tar zxvf test.tar.gz -C test 注释:上面的命令将 test.tar.gz 这个压缩包解压到当前目录下的 tes ...

  10. 验证loadrunner对Ajax内容的校验

    前一阵和开发的同事一起測试某个系统的性能.此系统是发送Ajax请求到后台,再调用第三方的某项服务. 第三方服务的性能由不得我们控制.因此开发者做了一下改进.超时则直接返回. 于是在loadrunner ...