Mapreduce实例--求平均值
求平均数是MapReduce比较常见的算法,求平均数的算法也比较简单,一种思路是Map端读取数据,在数据输入到Reduce之前先经过shuffle,将map函数输出的key值相同的所有的value值形成一个集合value-list,然后将输入到Reduce端,Reduce端汇总并且统计记录数,然后作商即可。具体原理如下图所示:

操作环境:
Centos 7
jdk 1.8
hadoop-3.2.0
IDEA2019
实现内容:
将自定义的电商关于商品点击情况的数据文件,包含两个字段(商品分类,商品点击次数),用"\t"分割,类似如下:
商品分类 商品点击次数
52127 5
52120 93
52092 93
52132 38
52006 462
52109 28
52109 43
52132 0
52132 34
52132 9
52132 30
52132 45
52132 24
52009 2615
52132 25
52090 13
52132 6
52136 0
52090 10
52024 347
使用mapreduce统计出每类商品的平均点击次数
商品分类 商品平均点击次数
52006 462
52009 2615
52024 347
52090 11
52092 93
52109 35
52120 93
52127 5
52132 23
52136 0
一、启动Hadoop集群,上传数据集文件到hdfs
hadoop fs -mkdir -p /mymapreduce4/in
hadoop fs -put /data/mapreduce4/goods_click /mymapreduce4/in
二、在IDEA中创建java项目,导入Jar包,如果清楚自己使用的集群的jar包,可使用maven导入指定的jar包。
为了避免Jar包冲突,使用hadoop/share目录下的所有jar包。
三、编写java代码程序
package mapreduce;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class MyAverage{
public static class Map extends Mapper<Object , Text , Text , IntWritable>{
private static Text newKey=new Text();
public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
//将输入的纯文本文件数据转化成string
String line=value.toString();
System.out.println(line);
//将值通过split()方法截取出来
String arr[]=line.split("\t");
newKey.set(arr[0]);
int click=Integer.parseInt(arr[1]);
//将数据和值输入到reduce处理
context.write(newKey, new IntWritable(click));
}
}
public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable>{
public void reduce(Text key,Iterable<IntWritable> values,Context context) throws IOException, InterruptedException{
int num=0;
int count=0;
for(IntWritable val:values){
//每个元素求和num
num+=val.get();
//统计元素的次数count
count++;
}
//统计次数
int avg=num/count;
context.write(key,new IntWritable(avg));
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
Configuration conf=new Configuration();
System.out.println("start");
Job job =new Job(conf,"MyAverage");
job.setJarByClass(MyAverage.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
Path in=new Path("hdfs://172.18.74.137:9000/mymapreduce4/in/goods_click");
Path out=new Path("hdfs://172.18.74.137:9000/mymapreduce4/out");
FileInputFormat.addInputPath(job,in);
FileOutputFormat.setOutputPath(job,out);
System.exit(job.waitForCompletion(true) ? 0 : 1); }
}
四、运行查看结果
hadoop fs -ls /mymapreduce4/out
hadoop fs -cat /mymapreduce4/out/part-r-00000

Mapreduce实例--求平均值的更多相关文章
- MapReduce实例——求平均值,所得结果无法写出到文件的错误原因及解决方案
1.错误原因 mapreduce按行读取文本,map需要在原有基础上增加一个控制语句,使得读到空行时不执行write操作,否则reduce不接受,也无法输出到新路径. 2.解决方案 原错误代码 pub ...
- Linux下的计算命令和求和、求平均值、求最值命令梳理
在Linux系统下,经常会有一些计算需求,那么下面就简单梳理下几个常用到的计算命令 (1)bc命令bc命令是一种支持任意精度的交互执行的计算器语言.bash内置了对整数四则运算的支持,但是并不支持浮点 ...
- PAT-乙级-1054. 求平均值 (20)
1054. 求平均值 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题的基本要求非常简单:给定N个实 ...
- excl剔除不合格数据求平均值
excl剔除不合格数据求平均值 trimmean函数 正态分布: CONFIDENCE.NORM 函数
- python 录入姓名和成绩, 并且求平均值
lst = []while 1: a = input("请输入学生的姓名和成绩(姓名_成绩), 输入Q退出录入:") if a.upper() == "Q": ...
- 深度学习原理与框架-图像补全(原理与代码) 1.tf.nn.moments(求平均值和标准差) 2.tf.control_dependencies(先执行内部操作) 3.tf.cond(判别执行前或后函数) 4.tf.nn.atrous_conv2d 5.tf.nn.conv2d_transpose(反卷积) 7.tf.train.get_checkpoint_state(判断sess是否存在
1. tf.nn.moments(x, axes=[0, 1, 2]) # 对前三个维度求平均值和标准差,结果为最后一个维度,即对每个feature_map求平均值和标准差 参数说明:x为输入的fe ...
- JS创建一个数组1.求和 2.求平均值 3.最大值 4.最小值 5.数组逆序 6.数组去重 0.退出
rs = require("readline-sync"); let arr = []; console.log("请输入数组的长度:"); let arr_l ...
- PAT 乙级 1054 求平均值 (20) C++版
1054. 求平均值 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题的基本要求非常简单:给定N个实 ...
- C# Linq to Entity Lamda方式分组并求和求平均值
1.单字段分组并求和: var list = data.GroupBy(g => g.GoodsId).Select(e => new { GoodsId = e.Key, Qty = e ...
随机推荐
- PyQt学习随笔:Qt事件QEvent.type类型常量及其含义资料汇总详细内容速查
下表是Qt5.11提供的所有已经定义的事件类型常量及其含义说明(其中标蓝色的是老猿认为价值比较大的事件),事件的事件类型通过QEvent.type()来获取.由于老猿没有找到直接粘贴Excel表格的方 ...
- javascript (JS组成、书写位置、基本概念、作用域、内存问题、变量)
1 JavaScript的组成和书写位置 Javascript:运行在客户端(浏览器)的脚本语言,JavaScript的解释器被称为JavaScript引擎,为浏览器的一部分,与java没有直接的关系 ...
- LeetCode初级算法之数组:350 两个数组的交集 II
两个数组的交集 II 题目地址:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/ 给定两个数组,编写一个函数来计算它们的交 ...
- spring框架半自动注解
为了简便我们的开发,让我们一起来学习半自动注解吧. 让Spring管理某些类 1.在需要被SpringIOC容器管理的类上打上相应的注解 @Component:任意组件 @Controller:控制层 ...
- go学习的第7天
不容易啊,坚持7天了呢,今天开始看视频学习 https://www.bilibili.com/video/BV1pt41127FZ?from=search&seid=4441824587572 ...
- 最简 Spring AOP 源码分析!
前言 最近在研究 Spring 源码,Spring 最核心的功能就是 IOC 容器和 AOP.本文定位是以最简的方式,分析 Spring AOP 源码. 基本概念 上面的思维导图能够概括了 Sprin ...
- 题解-FJOI2014 树的重心
FJOI2014 树的重心 \(Q\) 组测试数据.给一棵树大小为 \(n\),求有多少个子树与其重心相同.重心可能有多个. 数据范围:\(1\le Q\le 50\),\(1\le n\le 200 ...
- dp斜率优化
算法-dp斜率优化 前置知识: 凸包 斜率优化很玄学,凭空讲怎么也讲不好,所以放例题. [APIO2014]序列分割 [APIO2014]序列分割 给你一个长度为 \(n\) 的序列 \(a_1,a_ ...
- js实现转盘抽奖
大转盘抽奖,主要通过css3的"transform:rotate(0deg)"属性来控制元素的旋转角度来实现. 通常,抽奖的过程需要渐进的效果,所以直接通过旋转属性写比较繁琐. 这 ...
- rman catalog配置
1.创建表空间 create tablespace rman_tbs datafile '/u01/app/oracle/oradata/PROD1/rman_tbs01.dbf' size 200m ...