摘要:MapReduce程序进行单词计数。

关键词:MapReduce程序  单词计数

数据源:人工构造英文文档file1.txt,file2.txt。

file1.txt 内容

Hello   Hadoop

I   am  studying   the   Hadoop  technology

file2.txt内容

Hello  world

The  world  is  very  beautiful

I   love    the   Hadoop    and    world

问题描写叙述:

统计人工构造的英文文档中单词的频数,要求输出的结果依照单词字母的顺序进行排序。

解决方式:

1  开发工具:VM10+ Ubuntu12.04+ Hadoop1.1.2

2  设计思路:把英文文档内容且分成单词,然后把全部同样的单词聚集在一起,最后计算各个单词的频数。

程序清单:

package com.wangluqing;

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;

import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {

public static class TokenizerMapper extends Mapper<Object,Text,Text,IntWritable> {

private final static IntWritable one = new IntWritable(1);

private Text word = new Text();

public void map(Object key, Text value, Context context) throws IOException,InterruptedException {

StringTokenizer its = new StringTokenizer(value.toString());

while (its.hasMoreTokens()) {

word.set(its.nextToken());

context.write(word,one);

}

}

}

public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {

private IntWritable result = new IntWritable();

public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

int sum = 0;

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();

String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();

if(otherArgs.length !=2 ) {

System.err.println("Usage:wordcount<in><out>");

System.exit(2);

}

Job job = new Job(conf,"word count");

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(otherArgs[0]));

FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));

System.exit(job.waitForCompletion(true)?0:1);

}

}

3 运行程序

1)创建输入文件夹

hadoop  fs  -mkdir   wordcount_input

2)上传本地英文文档

hadoop  fs -put  /usr/local/datasource/article/*   wordcount_input

3)编译WordCount.java程序,把结果存放在当前文件夹的WordCount文件夹下。

root@hadoop:/usr/local/program/hadoop# javac -classpath hadoop-core-1.1.2.jar:lib/commons-cli-1.2.jar -d WordCount WordCount.java

4) 将编译结果打成Jar包

jar -cvf  wordcount.jar   WordCount/  .

5)执行WordCount程序,输入文件夹为wordcount_input,输出文件夹为wordcount_output。

hadoop jar wordcount.jar  com.wangluqing.WordCount  wordcount_input  wordcount_output

6) 查看各个单词频数结果

root@hadoop:/usr/local/program/hadoop# hadoop fs -cat wordcount_output/part-r-00000

Hadoop 3

Hello 2

I 2

The 1

am 1

and 1

beautiful 1

is 1

love 1

studying 1

technology 1

the 2

very 1

world 3

总结:

WordCount程序是最简单也是最具代表性的MapReduce程序,一定程度上MapReduce设计的初衷,即对日志文件的分析。

Resource:

1  http://www.wangluqing.com/2014/03/hadoop-mapreduce-programapp2/

2  《Hadoop实战 第二版》陆嘉恒著 第5章 MapReduce应用案例

HADOOP之MAPREDUCE程序应用二的更多相关文章

  1. Hadoop之MapReduce程序应用三

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

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

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

  3. 用PHP编写Hadoop的MapReduce程序

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

  4. 如何在Hadoop的MapReduce程序中处理JSON文件

    简介: 最近在写MapReduce程序处理日志时,需要解析JSON配置文件,简化Java程序和处理逻辑.但是Hadoop本身似乎没有内置对JSON文件的解析功能,我们不得不求助于第三方JSON工具包. ...

  5. Hadoop之Mapreduce 程序

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

  6. hadoop开发MapReduce程序

    准备工作: 1.设置HADOOP_HOME,指向hadoop安装目录 2.在window下,需要把hadoop/bin那个目录替换下,在网上搜一个对应版本的 3.如果还报org.apache.hado ...

  7. 【Hadoop】MapReduce笔记(二):MapReduce容错,任务失败处理

    典型问题:Hadoop如何判断一个任务失败?失败了怎么做? 分析:实际情况下,用户代码存在软件错误.进程崩溃.机器故障等都会导致失败.Hadoop判断的失败有不同级别类型,针对不同级别的失败有不同的处 ...

  8. 在window下远程虚拟机(centos)hadoop运行mapreduce程序

    (注:虽然连接成功但是还是执行不了.以后有时间再解决吧 看到的人别参考仅作个人笔记)先mark下 1.首先在window下载好一个eclipse.和拷贝好linux里面hadoop版本对应的插件(我是 ...

  9. hadoop-初学者写map-reduce程序中容易出现的问题 3

    1.写hadoop的map-reduce程序之前所必须知道的基础知识: 1)hadoop map-reduce的自带的数据类型: Hadoop提供了如下内容的数据类型,这些数据类型都实现了Writab ...

随机推荐

  1. Adobe Acrobat Ⅺ Pro安装激活

    1.注意一定要断网安装,如果你有防火墙拦截亦可(注意:系统自带那防火墙不行). 2.将AcrobatPro_11_Web_WWMUI.exe解压到一个目录下,找到目录下的setup.exe安装,安装时 ...

  2. iOS 用GDataXMLNode创建和解析XML

    原文地址:http://blog.csdn.net/gf771115/article/details/7718403 NSError *error; //    NSString *path = [[ ...

  3. CentOS6.5 搭建基础PHP环境(yum安装)

    转载:闲来无事 » CentOS6.5 搭建基础PHP环境(yum安装) yum安装php环境只需要几条简单的命令就可以实现,OK,各位客官,菜来了.首先确保你的yum源可用,或者网络是通的,不然下载 ...

  4. 15-UIKit(view布局、Autoresizing)

    目录: 1. 纯代码布局 2. 在View中进行代码布局 3. Autoresizing 回到顶部 1. 纯代码布局 纯代码布局分VC下和V下 [MX1-layout-code] 在VC下覆盖view ...

  5. EF+jQueryUI前后端分离设计

    开源项目练习EF+jQueryUI前后端分离设计   最近大家流行把项目开源,我也来玩玩.只是开源公司项目不好,小弟只好从公司项目经验上另外弄出一套练习开源给大家. 这个项目可以做简单的团队任务系统( ...

  6. 不用SWIG,Go使用C++代码的方式

    将C++代码用C作一次封装,就可以让Go调用了. 这是一个C++头文件: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #ifndef CGO_CPPGO_C ...

  7. 用 Asterisk 搭建自己的免费 VoIP 服务器

    原文 http://www.yaoblog.info/?p=5021 1. 这里以 Debian 为例子,安装 Asterisk apt-get update apt-get install aste ...

  8. Java Swing界面编程(22)---事件处理:动作事件及监听处理

    要想让一个button变得有意义,就必须使用事件处理.在swing的事件处理中.能够使用ActionListener接口处理button的动作事件. package com.beyole.util; ...

  9. Android中贝塞尔曲线的绘制方法

    贝塞尔曲线,很多人可能不太了解,什么叫做贝塞尔曲线呢?这里先做一下简单介绍:贝塞尔曲线也可以叫做贝济埃曲线或者贝兹曲线,它由线段与节点组成,节点是可拖动的支点,线段像可伸缩的皮筋.一般的矢量图形软件常 ...

  10. Windows Azure 安全最佳实践 - 第 5 部分:基于Claim 的标识,单点登录

    基于Claim的身份标识是处理网站与 Web 服务的身份认证和访问一种简单而强大的方式,无论您是在本地工作还是面向云工作.您可以通过减少自定义实施和使用基于Claim的单一简化标识模型,创建更安全的应 ...