1、数据准备

使用MapReduce计算age.txt中年龄最大、最小、均值
name,min,max,count
Mike,35,20,1
Mike,5,15,2
Mike,20,13,1
Steven,40,20,10
Ken,28,68,1
Ken,14,198,10
Cindy,32,31,100

2、预期结果
Mike 5 20 4
Steven,40,20,10
Ken   14 198 11
Cindy,32,31,100

3、需要加入自定义输出类型MinMaxCountTuple

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException; import org.apache.hadoop.io.Writable; public class MinMaxCountTuple implements Writable {
private int min;
private int max;
private int count; public int getMin() {
return min;
} public void setMin(int min) {
this.min = min;
} public int getMax() {
return max;
} public void setMax(int max) {
this.max = max;
} public int getCount() {
return count;
} public void setCount(int count) {
this.count = count;
} public void readFields(DataInput in) throws IOException {
min = in.readInt();
max = in.readInt();
count = in.readInt();
} public void write(DataOutput out) throws IOException {
out.writeInt(min);
out.writeInt(max);
out.writeInt(count);
} @Override
public String toString() {
return min + "\t" + max + "\t" + count;
}
}

4、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 Age {
public static class AgeMap extends
Mapper<Object, Text, Text, MinMaxCountTuple> { private Text userName = new Text();
private MinMaxCountTuple outTuple = new MinMaxCountTuple(); @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[] splits = content.split(",");
String name = splits[0];
int min = Integer.valueOf(splits[1]);
int max = Integer.valueOf(splits[2]);
int count = Integer.valueOf(splits[3]);
outTuple.setMin(min);
outTuple.setMax(max);
outTuple.setCount(count);
userName.set(name);
context.write(userName, outTuple);
}
}
} public static class AgeReduce extends
Reducer<Text, MinMaxCountTuple, Text, MinMaxCountTuple> {
private MinMaxCountTuple result = new MinMaxCountTuple(); public void reduce(Text key, Iterable<MinMaxCountTuple> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
result.setMax(0);
result.setMin(Integer.MAX_VALUE);
for (MinMaxCountTuple tmp : values) {
if (tmp.getMin() < result.getMin()) {
result.setMin(tmp.getMin());
}
if (tmp.getMax() > result.getMax()) {
result.setMax(tmp.getMax());
}
sum += tmp.getCount();
}
result.setCount(sum);
context.write(key, result);
}
} 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(Age.class);
job.setMapperClass(AgeMap.class);
job.setCombinerClass(AgeReduce.class);
job.setReducerClass(AgeReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(MinMaxCountTuple.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

Hadoop实战-MapReduce之分组(group-by)统计(七)的更多相关文章

  1. Hadoop实战-MapReduce之max、min、avg统计(六)

    1.数据准备: Mike,35 Steven,40 Ken,28 Cindy,32 2.预期结果 Max 40 Min 28 Avg      33 3.MapReduce代码如下 import ja ...

  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. Hadoop实战训练————MapReduce实现PageRank算法

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

  8. MySQL数据库Group by分组之后再统计数目Count(*)与不分组直接统计数目的区别

    简述问题“统计最新时刻处于某一状态的设备的数量” 1. 首先子查询结果,可以看到每个设备最新的状态信息 2.1 在子查询的基础上,对设备状态进行分组,进行统计每个状态的设备数量 2.1.1 可以看到处 ...

  9. Hadoop实战课程

    Hadoop生态系统配置Hadoop运行环境Hadoop系统架构HDFS分布式文件系统MapReduce分布式计算(MapReduce项目实战)使用脚本语言Pig(Pig项目实战)数据仓库工具Hive ...

随机推荐

  1. Method and apparatus for verification of coherence for shared cache components in a system verification environment

    A method and apparatus for verification of coherence for shared cache components in a system verific ...

  2. Mac 下安装Ant

    转自:http://blog.csdn.net/crazybigfish/article/details/18215439 如果你不知道什么是ant,请不要浪费你的时间继续读下去了.或者你对ant是什 ...

  3. Memcached简单介绍

    Memcached简单介绍 简介:Memcached是一个自由开源的,高性能,分布式内存对象缓存系统.================================================= ...

  4. 1076. Wifi密码 (15)【模拟】

    1076. Wifi密码 (15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 下面是微博上流传的一张照片:“ ...

  5. SQL SERVER 工具

    http://www.cnblogs.com/fygh/archive/2012/04/25/2469563.html

  6. js文件/图片从电脑里面拖拽到浏览器上传文件/图片

    1.效果展示 2.html 代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <!DOCTYPE html> <html lang=& ...

  7. MySQL 服务无法启动-问题处理

    症状:前一天在MySQL中删除了几个不用的数据库后登陆MySQL出现以下错误:   mysql -u root -p passwd   ERROR 2002 (HY000): Can't connec ...

  8. 关于在iOS设备上探测WIFI,3G,GPRS使用情况的细节

    由于设计的游戏需要有一些联网请求,但有时候在设备未连接网络的情况下,如果对网络情况不加以判断,则可能造成游戏为了等游戏超时,浪费不必要的时间. 所以在游戏启动时检测一下网络状况是很必要的,而且当玩家的 ...

  9. Qt中QVector与QList的应用

    首先來看看QVector 的基本使用方式,建立一個可容納兩個元素的QVector ,並使用索引方式存取元素值:QVector<double> vect(2); vect[0] = 1.0; ...

  10. 偏执的iOS逆向研究员:收集全版本的macOS iOS+越狱+内核调试

    Intro 虽然“只有偏执狂才能够生存”这句话已经被假药停给毁了,但是作为一只有逼格的高大上的iOS逆向分析研究员,难道如果有现成的macOS/iOS全版本镜像可以下载并且无限“漫游”,难道你就不想来 ...