需求:求多门课程的平均值。

样板:math.txt

zhangsan 90

lisi 88

wanghua 80

china.txt

zhangsan 80
lisi 90
wanghua 88

输出:zhangsan 85

lisi 89

wanghua 84

分析部分:

mapper部分分析:

1、<k1,v1>k1代表:一行数据的编号位置,v1代表:一行数据。

2、<k2,v2>k2代表:名字,v2代表:分数。

reduce部分分析:

3、<k3,v3>k3代表:相同key(名字),v3代表:list<int>。

4、统计输出<k4,v4>k4代表:名字,v4代表:平均值。

程序部分:

AverageMapper类:

package com.cn.average;

import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; public class AverageMapper extends Mapper<Object, Text, Text, IntWritable> {
@Override
protected void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String [] strings = new String[2];
int i = 0;
String line = value.toString();
StringTokenizer tokenizerVal = new StringTokenizer(line);
while (tokenizerVal.hasMoreElements()) {
strings[i] = tokenizerVal.nextToken();
i++;
}
context.write(new Text(strings[0]), new IntWritable(Integer.parseInt(strings[1])));
}
}

AverageReduce类:

package com.cn.average;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class AverageReduce extends Reducer<Text, IntWritable, Text, IntWritable>{
@Override
protected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {
int sum = 0;
int i = 0;
for(IntWritable value : values){
sum += value.get();
i++;
}
context.write(key, new IntWritable(sum/i));
}
}

DataAverage类:

package com.cn.average;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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;
import org.apache.hadoop.util.GenericOptionsParser; /**
* 平均值
* @author root
*
*/
public class DataAverage {
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: DataAverage ");
System.exit(2);
}
//创建一个job
Job job = new Job(conf, "Data Average");
job.setJarByClass(DataAverage.class); //设置文件的输入输出路径
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); //设置mapper和reduce处理类
job.setMapperClass(AverageMapper.class);
job.setReducerClass(AverageReduce.class); //设置输出key-value数据类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); //提交作业并等待它完成
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

每天总结一点点,总有不一样的收获。

hadoop程序MapReduce之average的更多相关文章

  1. hadoop程序MapReduce之SingletonTableJoin

    需求:单表关联问题.从文件中孩子和父母的关系挖掘出孙子和爷奶关系 样板:child-parent.txt xiaoming daxiong daxiong alice daxiong jack 输出: ...

  2. hadoop程序MapReduce之DataSort

    需求:对文件中的数据进行排序. 样本:sort.log 10 13 10 20 输出:1 10 2 10 3 13 4 20 分析部分: mapper分析: 1.<k1,v1>k1代表:行 ...

  3. hadoop程序MapReduce之DataDeduplication

    需求:去掉文件中重复的数据. 样板:data.log 2016-3-1 a 2016-3-2 b 2016-3-2 c         2016-3-2 b 输出结果: 2016-3-1 a 2016 ...

  4. hadoop程序MapReduce之MaxTemperature

    需求:求每年当中最高的温度 样本:temp.log 2016080623 2016072330 2015030420 输出结果:2016 30 2015 20 MapReduce分析设计: Mappe ...

  5. hadoop程序MapReduce之WordCount

    需求:统计一个文件中所有单词出现的个数. 样板:word.log文件中有hadoop hive hbase hadoop hive 输出:hadoop 2 hive 2 hbase 1 MapRedu ...

  6. 用PHP编写Hadoop的MapReduce程序

    用PHP编写Hadoop的MapReduce程序     Hadoop流 虽然Hadoop是用Java写的,但是Hadoop提供了Hadoop流,Hadoop流提供一个API, 允许用户使用任何语言编 ...

  7. Hadoop之MapReduce程序应用三

    摘要:MapReduce程序进行数据去重. 关键词:MapReduce   数据去重 数据源:人工构造日志数据集log-file1.txt和log-file2.txt. log-file1.txt内容 ...

  8. 如何在Windows下面运行hadoop的MapReduce程序

    在Windows下面运行hadoop的MapReduce程序的方法: 1.下载hadoop的安装包,这里使用的是"hadoop-2.6.4.tar.gz": 2.将安装包直接解压到 ...

  9. Hadoop之Mapreduce 程序

    package com.gylhaut.hadoop.senior.mapreduce; import java.io.IOException; import java.util.StringToke ...

随机推荐

  1. RTX——第13章 事件标志组

    以下内容转载自安富莱电子: http://forum.armfly.com/forum.php 前面的章节我们已经讲解了任务管理和时间管理,从本章节开始讲解任务间的通信和同步机制.首先讲解任务间的通信 ...

  2. Tomcat配置 设置启动参数,点击startup.bat启动

    catalina.batrem ---------------------------------------------------------------------------rem Set J ...

  3. 【 Linux 】单台服务器上并发TCP连接数

    单台服务器上并发TCP连接数    问题:一台服务器到底能够支持多少TCP并发连接呢? 1. 文件描述符限制:    对于服务器来说,每一个TCP连接都要占用一个文件描述符,一旦文件描述符使用完,新的 ...

  4. drupal7请求异常,执行时间过长的解决方法

    drupal7请求错误,执行时间过长的解决办法 根据你的系统或网络设置Drupal不能读取网页,造成功能缺失.可能是web服务器配置或PHP设置引起的,可用更新.获取更新源.使用OpenID登 录或使 ...

  5. wysiwyg+ckeditor 安装

    1.下载wysiwyg模块  https://drupal.org/project/wysiwyg 2.下载ckeditor 上传/sites/all/libraries 出现问题: 解决方法: 在文 ...

  6. Storm学习笔记——高级篇

    1. Storm程序的并发机制 1.1 概念 Workers (JVMs): 在一个物理节点上可以运行一个或多个独立的JVM 进程.一个Topology可以包含一个或多个worker(并行的跑在不同的 ...

  7. swiper中有视频时,滑动停止后视频停止播放

    我们经常能够看到在图片轮播中,穿插着视频的播放,如下图为淘宝的一个产品轮播图,放个视频能够让顾客对产品有个更全面的认识. 我们可以用swiper实现这个功能.用法就跟放图片一样,只是这里把图片换成视频 ...

  8. css 3 制作水波状进度条

    效果图如下 : 代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  9. js导出execl

    var idTmr; function ExportExcel(tableid) {//整个表格拷贝到EXCEL中 var curTbl = document.getElementById(table ...

  10. Python if-else and while

    if-elif语法 if condition: [tab键] command elif condition: [tab键] command elif condition: [tab键] command ...