摘要: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. 查看linux下各数据类型的大小

    代码如下: #include<stdio.h> int main() { printf("int:%d bytes\n",sizeof(int)); printf(&q ...

  2. 我的Python成长之路---第一天---Python基础(2)---2015年12月26日(雾霾)

    三.数据类型 Python基本类型(能够直接处理的数据类型有以下几种)主要有5种 1.整数(int) Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样,例如 ...

  3. 11-UIKit(Storyboard、View的基本概念、绘制图形、UIBezierPath)

    目录: 1. Storyboard 2. Views 3. View的基本概念介绍 4. 绘制图形 5. UIBezierPath 回到顶部 1. Storyboard 1.1 静态表视图 1)Sec ...

  4. Qt核心剖析: moc

    前面我们说过,Qt 不是使用的“标准的” C++ 语言,而是对其进行了一定程度的“扩展”.这里我们从Qt新增加的关键字就可以看出来:signals.slots 或者 emit.所以有人会觉得 Qt 的 ...

  5. Android 关于网址,电话号码,邮箱的正则表达式-最权威

    需求:判断网址是否合法 今天在写一个项目的时候,需要能够识别网址的功能,首先想到的是正则表达式 但是网址的类型多种多样,网络上各种表达式也一搜一大把,很难知道哪一位大神写的靠谱 发现:TextView ...

  6. mysql各版本区别

    MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Server 社区版本,开源免费, ...

  7. Swift语言Storyboard教程:第一部分

    更新记录: 该Storyboard教程由Caroline Begbie更新iOS 8和Swift相关内容.原文作者为教程编纂组的成员Matthijs Hollemans. 2014/12/10更新:  ...

  8. BZOJ 3446: [Usaco2014 Feb]Cow Decathlon( 状压dp )

    水状压dp. dp(x, s) = max{ dp( x - 1, s - {h} ) } + 奖励(假如拿到的) (h∈s). 时间复杂度O(n * 2^n) ------------------- ...

  9. JAVA UTF-8字符转换为GBK

    String t = "\u53d6"; try { String gbk=URLEncoder.encode(t,"GBK"); System.out.pri ...

  10. django-cookieless 0.7 : Python Package Index

    django-cookieless 0.7 : Python Package Index django-cookieless 0.7 Download django-cookieless-0.7.ta ...