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

系统环境
Linux Centos 7
jdk 1.8
hadoop-3.2
IDEA代码编译器
实验任务:
求平均数是MapReduce比较常见的算法,求平均数的算法也比较简单,一种思路是Map端读取数据,在数据输入到Reduce之前先经过shuffle,将map函数输出的key值相同的所有的value值形成一个集合value-list,然后将输入到Reduce端,Reduce端汇总并且统计记录数,然后作商即可。具体原理如下图所示:
商品分类 商品点击次数
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
步骤:
1.切换到指定目录,启动集群
开启hadoop集群,本地安装的为高可用主从二节点的hadoop集群,集成了各项大数据组件。
先开启zookeeper,再开启hdfs,再开启yarn。
倘若本地安装的是普通分布式或伪分布式集群,直接./start-all.sh启动集群即可。
2.在linux将数据集上传到hdfs中
hadoop fs -mkdir -p /mymapreduce4/in
hadoop fs -put /data/mapreduce4/goods_click /mymapreduce4/in
10181 1000481 2010-04-04 16:54:31
20001 1001597 2010-04-07 15:07:52
20001 1001560 2010-04-07 15:08:27
20042 1001368 2010-04-08 08:20:30
20067 1002061 2010-04-08 16:45:33
20056 1003289 2010-04-12 10:50:55
20056 1003290 2010-04-12 11:57:35
20056 1003292 2010-04-12 12:05:29
20054 1002420 2010-04-14 15:24:12
20055 1001679 2010-04-14 19:46:04
20054 1010675 2010-04-14 15:23:53
20054 1002429 2010-04-14 17:52:45
20076 1002427 2010-04-14 19:35:39
20054 1003326 2010-04-20 12:54:44
20056 1002420 2010-04-15 11:24:49
20064 1002422 2010-04-15 11:35:54
20056 1003066 2010-04-15 11:43:01
20056 1003055 2010-04-15 11:43:06
20056 1010183 2010-04-15 11:45:24
20056 1002422 2010-04-15 11:45:49
20056 1003100 2010-04-15 11:45:54
20056 1003094 2010-04-15 11:45:57
20056 1003064 2010-04-15 11:46:04
20056 1010178 2010-04-15 16:15:20
20076 1003101 2010-04-15 16:37:27
20076 1003103 2010-04-15 16:37:05
20076 1003100 2010-04-15 16:37:18
20076 1003066 2010-04-15 16:37:31
20054 1003103 2010-04-15 16:40:14
20054 1003100 2010-04-15 16:40:16
3.创建java工程,将jar包导入进去
为了避免版本冲突,和不必要的麻烦,可将hadoop目录下share/hadoop文件中的所有jar包导入进去。
Mapper代码<<<<
public static class Map extends Mapper<Object , Text , Text , IntWritable>{
private static Text newKey=new Text();
//实现map函数
public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
// 将输入的纯文本文件的数据转化成String
String line=value.toString();
System.out.println(line);
String arr[]=line.split("\t");
newKey.set(arr[0]);
int click=Integer.parseInt(arr[1]);
context.write(newKey, new IntWritable(click));
}
}
Reduce代码<<<<<
public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable>{
//实现reduce函数
public void reduce(Text key,Iterable<IntWritable> values,Context context) throws IOException, InterruptedException{
int num=0;
int count=0;
for(IntWritable val:values){
num+=val.get(); //每个元素求和num
count++; //统计元素的次数count
}
int avg=num/count; //计算平均数 context.write(key,new IntWritable(avg));
}
}
完整代码如下:
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 line=value.toString();
System.out.println(line);
String arr[]=line.split("\t");
newKey.set(arr[0]);
int click=Integer.parseInt(arr[1]);
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+=val.get();
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://localhost:9000/mymapreduce4/in/goods_click");
Path out=new Path("hdfs://localhost:9000/mymapreduce4/out");
FileInputFormat.addInputPath(job,in);
FileOutputFormat.setOutputPath(job,out);
System.exit(job.waitForCompletion(true) ? 0 : 1); }
}
4.执行
执行方式有两种
①直接在本地运行,前提要在本地配置好hadoop环境变量,直接运行即可。
②将此文件打包成jar包,上传到linux中再,用命令运行。
hadoop jar /apps/hadoop/hadoop-mapreduce.jar wordcount /in /out
查看运行结果:
hadoop fs -ls /mymapreduce4/out
hadoop fs -cat /mymapreduce4/out/part-r-00000

Hadoop之WordCount的更多相关文章
- Hadoop中wordcount程序
一.测试过程中 输入命令: 首先需要在hadoop集群中添加文件 可以首先进行查看hadoop集群中文件目录 hadoop fs -ls / hadoop fs -ls -R / hadoop fs ...
- [Linux][Hadoop] 运行WordCount例子
紧接上篇,完成Hadoop的安装并跑起来之后,是该运行相关例子的时候了,而最简单最直接的例子就是HelloWorld式的WordCount例子. 参照博客进行运行:http://xiejiangl ...
- 伪分布式环境下命令行正确运行hadoop示例wordcount
首先确保hadoop已经正确安装.配置以及运行. 1. 首先将wordcount源代码从hadoop目录中拷贝出来. [root@cluster2 logs]# cp /usr/local/h ...
- 一个可以跑的Hadoop的WordCount程序
搭个新环境时总要折腾一下,于是干脆记下来. 程序: package com.my; import java.io.IOException; import java.util.Iterator; imp ...
- hadoop执行wordcount例子
1:下载hadoop.http://mirror.esocc.com/apache/hadoop/common/hadoop-1.2.1/hadoop-1.2.1.tar.gz 2:解压. tar - ...
- 【Hadoop】Hadoop mr wordcount基础
1.基本概念 2.Mapper package com.ares.hadoop.mr.wordcount; import java.io.IOException; import java.util.S ...
- Windows用Eclipse来开发hadoop的WordCount的helloworld
[学习笔记] 2.Win7用Eclipse来开发hadoop的WordCount的helloworld网上下载hadoop-eclipse-plugin-2.7.4.jar,将该jar包拷贝到Ecli ...
- 5行代码怎么实现Hadoop的WordCount?
初学编程的人,都知道hello world的含义,当你第一次从控制台里打印出了hello world,就意味着,你已经开始步入了编程的大千世界,这和第一个吃螃蟹的人的意义有点类似,虽然这样比喻并不恰当 ...
- hadoop:将WordCount打包成独立运行的jar包
hadoop示例中的WordCount程序,很多教程上都是推荐以下二种运行方式: 1.将生成的jar包,复制到hadoop集群中的节点,然后运行 $HADOOP_HOME/bin/hadoop xxx ...
- Eclipse上运行第一个Hadoop实例 - WordCount(单词统计程序)
需求 计算出文件中每个单词的频数.要求输出结果按照单词的字母顺序进行排序.每个单词和其频数占一行,单词和频数之间有间隔. 比如,输入两个文件,其一内容如下: hello world hello had ...
随机推荐
- python批量definition query
import arcpy mxd = arcpy.mapping.MapDocument("current") for lyr in arcpy.mapping.ListLayer ...
- 第15.28节 PyQt(Python+Qt)入门学习:Model/View架构中的便利类QTableWidget详解
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.引言 表格部件为应用程序提供标准的表格显示工具,在表格内可以管理基于行和列的数据项,表格中的最大 ...
- PyQt(Python+Qt)学习随笔:QTableWidgetItem项文本和项对齐的setText、setTextAlignment方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTableWidget部件中的QTableWidgetItem项的文本可以通过text()和set ...
- 第14.14节 爬虫实战准备:csdn博文点赞过程http请求和响应信息分析
如果要对csdn博文点赞,首先要登录CSDN,然后打开一篇需要点赞的文章,如<第14.1节 通过Python爬取网页的学习步骤>按<第14.3节 使用google浏览器获取网站访问的 ...
- csv文件的写操作
import csv sumbmit_csv_path = "submit_have_valid_SGD.csv" with open(sumbmit_csv_path, &quo ...
- redis学习之——五大基本数据类型
redis 键 (key) 基本数据类型:string 字符串 list (列表) set(集合) hash(类似java 中的Map) zset(有序集合) 官方命令doc redis 键 ...
- STL——容器(Set & multiset)的删除 erase
set.clear(); //清除所有元素 set.erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器. set.erase(beg,end ...
- Anno 让微服务、混合编程更简单(Net love Java)
在社区或者QQ群我们经常看到有人争辩编程语言的好坏,只要一提起这个话题常常就能引来很多人参与,往往最后就变成了一群人几个小时的骂战.今天我们要说的是如何让Java和.Net(甚至更多语言)相结合.充分 ...
- centos7下安装iostat命令
[root@node01 yum.repos.d]# yum intall -y sysstat Loaded plugins: fastestmirror No such command: inta ...
- WindowsPhone8.1 开发-- 二维码扫描
随着 WinRT 8.1 API 的发布,Windows 8.1 和 Windows Phone 8.1 (基于 WinRT) 应用程序的开发模型经历了戏剧性的收敛性.与一些特定于平台的考虑,我们现在 ...