求平均数是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实例--求平均值的更多相关文章

  1. MapReduce实例——求平均值,所得结果无法写出到文件的错误原因及解决方案

    1.错误原因 mapreduce按行读取文本,map需要在原有基础上增加一个控制语句,使得读到空行时不执行write操作,否则reduce不接受,也无法输出到新路径. 2.解决方案 原错误代码 pub ...

  2. Linux下的计算命令和求和、求平均值、求最值命令梳理

    在Linux系统下,经常会有一些计算需求,那么下面就简单梳理下几个常用到的计算命令 (1)bc命令bc命令是一种支持任意精度的交互执行的计算器语言.bash内置了对整数四则运算的支持,但是并不支持浮点 ...

  3. PAT-乙级-1054. 求平均值 (20)

    1054. 求平均值 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题的基本要求非常简单:给定N个实 ...

  4. excl剔除不合格数据求平均值

    excl剔除不合格数据求平均值 trimmean函数 正态分布: CONFIDENCE.NORM 函数

  5. python 录入姓名和成绩, 并且求平均值

    lst = []while 1: a = input("请输入学生的姓名和成绩(姓名_成绩), 输入Q退出录入:") if a.upper() == "Q": ...

  6. 深度学习原理与框架-图像补全(原理与代码) 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 ...

  7. JS创建一个数组1.求和 2.求平均值 3.最大值 4.最小值 5.数组逆序 6.数组去重 0.退出

    rs = require("readline-sync"); let arr = []; console.log("请输入数组的长度:"); let arr_l ...

  8. PAT 乙级 1054 求平均值 (20) C++版

    1054. 求平均值 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题的基本要求非常简单:给定N个实 ...

  9. C# Linq to Entity Lamda方式分组并求和求平均值

    1.单字段分组并求和: var list = data.GroupBy(g => g.GoodsId).Select(e => new { GoodsId = e.Key, Qty = e ...

随机推荐

  1. sqlmap工具的简单使用

    0x00 sqlmap简介:sqlmap是一款针对sql漏洞的自动化注入工具,有一个非常棒的特性,即对检测与利用的自动化处理(数据库指纹.访问底层文件系统.执行命令). 官方网站下载http://sq ...

  2. PyQt(Python+Qt)学习随笔:QAbstractScrollArea的viewPort(视口)理解

    老猿Python博文目录 老猿Python博客地址 在学习Designer中ItemViews类部件时,这些部件都有个父类QAbstractScrollArea,该父类是Qt滚动区域的低级抽象.该区域 ...

  3. Docker 安装-在centos7下安装Docker(二)

    参考docker安装的方式: http://www.runoob.com/docker/centos-docker-install.html Docker中文官网安装步骤:https://docs.d ...

  4. 小程序editor篇-基本使用图片上传

    今天小程序项目内,要弄一个editor,富文本编辑功能,支持图文并茂,前几天刚好看了小程序的demo应用,刚好看到editor这个东东,那就安排! 官网示例git地址 大概看了下文档,拉下官方示例,看 ...

  5. Codeforces Round #631 (Div. 1) A-C

    在 \(\text{Div. 2/3}\) 混了一个多月后,四个号终于都上紫了,也没用理由不打 \(\text{Div. 1}\) 了.这是我人生中的第一场 \(\text{Div .1}\) ,之前 ...

  6. 笔记-[ZJOI2007]仓库建设

    笔记-[ZJOI2007]仓库建设 [ZJOI2007]仓库建设 \(f_i\) 到第 \(i\) 个工厂并且建设了仓库. \[\begin{split} f_i=&\min\{f_j+\su ...

  7. 算法——n皇后问题

    n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 给定一个整数 n,返回所有不同的 n 皇后问题的解决方案. 每一种解法包含一个明确的 n 皇后问题的棋 ...

  8. oracle 时间段查询

    <select id="selectByRzrq" resultMap="BaseResultMap" parameterType="java. ...

  9. Struts2 S2-061(CVE-2020-17530)漏洞复现

    0x00 漏洞简介 Apache Struts2框架是一个用于开发Java EE网络应用程序的Web框架.Apache Struts于2020年12月08日披露 S2-061 Struts 远程代码执 ...

  10. Jmeter(1)下载和安装

    一.Jmeter工具安装 1.jmeter安装包下载地址:http://jmeter.apache.org/,下载Binaries包,使用jmeter需要先安装jdk 2.解压后打开/bin目录下的j ...