写在前面:WordCount的功能是统计输入文件中每个单词出现的次数。基本解决思路就是将文本内容切分成单词,将其中相同的单词聚集在一起,统计其数量作为该单词的出现次数输出。

1.MapReduce之wordcount的计算模型

1.1 WordCount的Map过程

假设有两个输入文本文件,输入数据经过默认的LineRecordReader被分割成一行行数据,再经由map()方法得到<key, value>对,Map过程如下:

得到map方法输出的< key,value>对后,Mapper会将它们按照key值进行排序,并执行Combine过程,将key值相同的value值累加,得到Mapper的最终输出结果,如图所示:

1.2 WordCount的Reduce过程

Reducer对从Mapper端接收的数据进行排序,之后由reduce()方法进行处理,将相同主键下的所有值相加,得到新的<key, value>对作为最终的输出结果,如图所示:

2. 打包运行WordCount程序

通过Eclipse来编译打包运行自己写的MapReduce程序(基于Hadoop 2.6.0)。

2.1 下载所需的驱动包

下载地址Group: org.apache.hadoop下载对应版本的驱动包:

  • hadoop-common-2.6.0.jar
  • hadoop-mapreduce-client-core-2.6.0.jar
  • hadoop-test-1.2.1.jar

2.2 创建新的工程

  1. 使用Eclipse创建名为WordCount的Java Project;
  2. Project Properties -> Java Build Path -> Libraries -> Add External Jars 添加第一步所下载Jar包, 点击OK;
  3. 创建WordCount.java源文件:
  4. 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.IntWritable;
    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;
    public class WordCount {
    public static class TokenizerMapper
    extends Mapper<Object, Text, Text, IntWritable>{
    private final static IntWritable one = new IntWritable();
    private Text word = new Text();
    /*
    * LongWritable 为输入的key的类型
    * Text 为输入value的类型
    * Text-IntWritable 为输出key-value键值对的类型
    */
    public void map(Object key, Text value, Context context
    ) throws IOException, InterruptedException {
    StringTokenizer itr = new StringTokenizer(value.toString()); // 将TextInputFormat生成的键值对转换成字符串类型
    while (itr.hasMoreTokens()) {
    word.set(itr.nextToken());
    context.write(word, one);
    }
    }
    }
    public static class IntSumReducer
    extends Reducer<Text,IntWritable,Text,IntWritable> {
    private IntWritable result = new IntWritable();
    /*
    * Text-IntWritable 来自map的输入key-value键值对的类型
    * Text-IntWritable 输出key-value 单词-词频键值对
    */
    public void reduce(Text key, Iterable<IntWritable> values,
    Context context
    ) throws IOException, InterruptedException {
    int sum = ;
    for (IntWritable val : values) {
    sum += val.get();
    }
    result.set(sum);
    context.write(key, result);
    }
    }
    public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration(); // job的配置
    Job job = Job.getInstance(conf, "word count"); // 初始化Job
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job, new Path(args[])); // 设置输入路径
    FileOutputFormat.setOutputPath(job, new Path(args[])); // 设置输出路径
    System.exit(job.waitForCompletion(true) ? : );
    }
    }

2.3 打包源文件

  1. 在Eclipse -> File ->Export -> Java ->JAR file ->next
  2. 选中新建的WordCount工程,设置相应的输出路径和文件名(这里的输出路径一定要记下来,后面会用到),FInish
  3. 在设置的输出路径处生成了WordCount.jar,至此,打包完毕。

2.4 启动HDFS服务

打开Terminal,进入目录/usr/local/Cellar/hadoop/2.6.0/sbin

$ start-dfs.sh  #启动HDFS
$ jps #验证是否启动成功

  1666

  2503 SecondaryNameNode

  2920 Jps

  2317 NameNode

  2399 DataNode

成功启动服务后, 可以直接在浏览器中输入http://localhost:50070/访问Hadoop页面

2.5 将文件上传到HDFS

进入目录/usr/local/Cellar/hadoop/2.6.0/bin

#在HDFS上创建输入/输出文件夹
$ hdfs dfs -mkdir /user
$ hdfs dfs -mkdir /user/input
$ hdfs dfs -ls /user
#上传本地file中文件到集群的input目录下
$ hdfs dfs -put /Users/&&&&&&&&/Downloads/test* /user/input
#查看上传到HDFS输入文件夹中到文件
$ hadoop fs -ls /user/input
#输出结果

  -rw-r--r--   1 &&&&&& supergroup        666 2015-04-06 10:49 /user/input/test01.html

  -rw-r--r--   1 &&&&&& supergroup       9708 2015-04-06 14:25 /user/input/test02.html

2.6 运行JAR文件

#在当前文件夹创建一个工作目录
$ mkdir WorkSpace

#下面这句可以不用,只要运行程序时,正确写入jar所在的完整路径即可
#将打包好的Jar复制到当前工作目录下(复制前路径就是你打包Jar时的存储路径)
$ cp /Users/&&&&&/Desktop/WorkCount.jar ./WorkSpace #运行Jar文件,各字段含义:hadoop是运行命令命令,jar WorkSpace/WordCount.jar指定Jar文件,WordCount指定Jar文件入口类,/user/input指定job的HDFS上得输入文件目录,output指定job的HDFS输出文件目录
$ hadoop jar WorkSpace/WordCount.jar WordCount /user/input /user/output
#这里input和output在同一user目录中,方便管理

显示如下结果,则说明运行成功:

……省略大量代码
File System Counters
FILE: Number of bytes read=2025025
FILE: Number of bytes written=4443318
FILE: Number of read operations=0
FILE: Number of large read operations=0
FILE: Number of write operations=0
HDFS: Number of bytes read=10356334
HDFS: Number of bytes written=616286
HDFS: Number of read operations=25
HDFS: Number of large read operations=0
HDFS: Number of write operations=5
Map-Reduce Framework
Map input records=33907
Map output records=663964
Map output bytes=6687108
Map output materialized bytes=1005779
Input split bytes=216
Combine input records=663964
Combine output records=68147
Reduce input groups=55800
Reduce shuffle bytes=1005779
Reduce input records=68147
Reduce output records=55800
Spilled Records=136294
Shuffled Maps =2
Failed Shuffles=0
Merged Map outputs=2
GC time elapsed (ms)=187
Total committed heap usage (bytes)=1323827200
Shuffle Errors
BAD_ID=0
CONNECTION=0
IO_ERROR=0
WRONG_LENGTH=0
WRONG_MAP=0
WRONG_REDUCE=0
File Input Format Counters
Bytes Read=4054789
File Output Format Counters
Bytes Written=616286

2.7 查看运行结果

$ hdfs dfs -ls /user/output

Found  items
-rw-r--r-- xumengting supergroup -- : output/_SUCCESS
-rw-r--r-- xumengting supergroup -- : output/part-r-00000 #查看结果输出文件中的内容
$ hdfs dfs -cat /user/output/part-r-00000

结果文件一般由2部分组成:

  • _SUCCESS文件:表示MapReduce运行成功。
  • part-r-00000文件:存放结果,也是默认生成的结果文件

参考文献:

[1]. 【Hadoop基础教程】5、Hadoop之单词计数——http://blog.csdn.net/andie_guo/article/details/44055863

[2]. MapReduce之Wordcount——http://andrewliu.tk/2015/03/29/MapReduce%E4%B9%8BWordCount/#more

[3]. Mac下Hadoop的配置及在Eclipse上编程

MapReduce程序(一)——wordCount的更多相关文章

  1. mapreduce程序编写(WordCount)

    折腾了半天.终于编写成功了第一个自己的mapreduce程序,并通过打jar包的方式运行起来了. 运行环境: windows 64bit eclipse 64bit jdk6.0 64bit 一.工程 ...

  2. 运行第一个MapReduce程序,WordCount

    1.安装Eclipse 安装后如果无法启动重新配置Java路径(如果之前配置了Java) 2.下载安装eclipse的hadoop插件 注意版本对应,放到/uer/lib/eclipse/plugin ...

  3. Hadoop学习之路(5)Mapreduce程序完成wordcount

    程序使用的测试文本数据: Dear River Dear River Bear Spark Car Dear Car Bear Car Dear Car River Car Spark Spark D ...

  4. MapReduce 程序:WordCount

  5. MapReduce程序——WordCount(Windows_Eclipse + Ubuntu14.04_Hadoop2.9.0)

    本文主要参考<Hadoop应用开发技术详解(作者:刘刚)> 一.工作环境 Windows7: Eclipse + JDK1.8.0 Ubuntu14.04:Hadoop2.9.0 二.准备 ...

  6. 第一个MapReduce程序——WordCount

    通常我们在学习一门语言的时候,写的第一个程序就是Hello World.而在学习Hadoop时,我们要写的第一个程序就是词频统计WordCount程序. 一.MapReduce简介 1.1 MapRe ...

  7. 从零开始学习Hadoop--第2章 第一个MapReduce程序

    1.Hadoop从头说 1.1 Google是一家做搜索的公司 做搜索是技术难度很高的活.首先要存储很多的数据,要把全球的大部分网页都抓下来,可想而知存储量有多大.然后,要能快速检索网页,用户输入几个 ...

  8. hadoop下跑mapreduce程序报错

    mapreduce真的是门学问,遇到的问题逼着我把它从MRv1摸索到MRv2,从年前就牵挂在心里,连过年回家的旅途上都是心情凝重,今天终于在eclipse控制台看到了job completed suc ...

  9. 使用Python实现Hadoop MapReduce程序

    转自:使用Python实现Hadoop MapReduce程序 英文原文:Writing an Hadoop MapReduce Program in Python 根据上面两篇文章,下面是我在自己的 ...

随机推荐

  1. 【转】锁(lock)知识及锁应用

    sql server锁(lock)知识及锁应用转自:http://blog.csdn.net/huwei2003/article/details/4047191 关键词:锁提示,锁应用 提示:这里所摘 ...

  2. 帝国cms底部代码哪里改?要修改版权和统计代码

    最近接手的几个站是用帝国cms做的,底部代码那边都有一个**设计的链接,还有一些不相关的东西,第一眼看到就想把那些帝国cms底部代码清理掉,这就是让别人建站的烦恼,让他们删除说要收费,坑就一个字,自己 ...

  3. OC 手势可能出现的问题

    oc手势有分別是 Tap(点一下).Pinch(二指往內或往外拨动).Rotation(旋转).Swipe(滑动,快速移动).Pan (拖移,慢速移动)以及 LongPress(长按). UITapG ...

  4. [LeetCode] 193. Valid Phone Numbers_Easy tag: Bash

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  5. Qt addStretch()详解

    addStretch函数,是在布局的时候用到. 函数原型: void QBoxLayout::addStretch ( int stretch = 0 ) 作用:平均分配Layout 比如: QVBo ...

  6. Lintcode: Hash Function && Summary: Modular Multiplication, Addition, Power && Summary: 长整形long

    In data structure Hash, hash function is used to convert a string(or any other type) into an integer ...

  7. Servlet—生命周期

    首次访问 重复访问 换浏览器访问(说明在一次项目运行期间,servlet实例只会创建一个)

  8. ARM_Core的处理器模式与寄存器,结构杂谈

    ARM处理器的工作状态:ARM处理器有两种工作状态.在程序的执行过程中,处理器可以在两种工作状态之间切换,并且不影响 相应寄存器中的内容. ARM状态,此时处理器执行32位对齐的ARM指令:BX指令, ...

  9. jQuery ajax 请求HttpServlet返回[HTTP/1.1 405 Method not allowed]

    1.问题使用jQuery的ajax请求 Servlet 时,返回没有进入ajax的success回调函数,浏览器控制台显示 [HTTP/1.1 405 Method not allowed]. 2.解 ...

  10. react脚手架构建工程

    https://blog.csdn.net/qtfying/article/details/78665664 第二步:安装less包: https://segmentfault.com/a/11900 ...