MapReduce
单词统计案例编程

一、在Linux环境安装Eclipse软件

1、   解压tar包

下载安装包eclipse-jee-kepler-SR1-linux-gtk-x86_64.tar.gz到/opt/software目录下。

解压到/opt/tools目录下:

[hadoop@bigdata-senior01 tools]$ tar -zxf
/opt/sofeware/eclipse-jee-kepler-SR1-linux-gtk-x86_64.tar.gz -C /opt/tools/

2、   创建存放源代码的目录

[hadoop@bigdata-senior01 eclipse]$ sudo
mkdir -p /opt/mysource/workspace

修改mysource的所有者为hadoop用户

[hadoop@bigdata-senior01 opt]$ sudo chown
-R  hadoop:hadoop /opt/mysource/

3、   启动Eclipse

在XWindow环境中,进入/opt/tools/eclipse目录,执行eclipse打开eclipse界面。

[hadoop@bigdata-senior01 eclipse]$
/opt/tools/eclipse/eclipse

设置Workspace目录为:/opt/mysource/workspace。

二、Hadoop Maven配置

1、 安装Apache Maven

(1)    解压Maven

[hadoop@bigdata-senior01
sofeware]$ tar -zxf apache-maven-3.0.5-bin.tar.gz -C /opt/modules/

(2)    配置/etc/profile文件

export MAVEN_HOME="/opt/modules/apache-maven-3.0.5"

export
PATH=$MAVEN_HOME/bin:$PATH

(3)    生效配置文件

[root@bigdata-senior01
sofeware]# source /etc/profile

(4)    确认Maven配置成功

[root@bigdata-senior01
sofeware]# mvn -version

Apache Maven
3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 21:51:28+0800)

Maven home:
/opt/modules/apache-maven-3.0.5

Java version:
1.7.0_67, vendor: Oracle Corporation

Java home:
/opt/modules/jdk1.7.0_67/jre

Default locale:
en_US, platform encoding: UTF-8

OS name:
"linux", version: "2.6.32-504.el6.x86_64", arch:
"amd64", family: "unix"

2、 设置Eclipse中设置Maven路径

(1)    Preferences对话框左侧选择Maven下的Installations,右侧点击Add,添加一个Maven位置。

(2)    选择自己的maven目录:/opt/modules/apache-maven-3.0.5

3、 查看home目录下是否有.m2目录

在Preferences左侧的Maven下的User Setting中,查看右侧是否提示.m2目录不存在,如果不存在,要手动创建。

4、 拷贝maven的settings.xml

[hadoop@bigdata-senior01 ~]$ cp
/opt/modules/apache-maven-3.0.5/conf/settings.xml ~/.m2/

三、创建WordCount程序项目

1、 创建一个Maven项目

(1)    File菜单中,新建Maven Project。

2、 添加Source Folder用来存放配置文件

将来core-site.xml、hdfs-site.xml、yarn-site.xml等配置文件存放在这个目录下。

3、 为src/main/resource指定输出路径

4、 编辑pom.xml文件

修改pom.xml后保存后,maven会自动去下载依赖包

四、编写MapReduce方法

1、   添加一个类WordCountMapReduce

WordCountMapReduce类继承org.apache.hadoop.con类并实现org.apache.hadoop.util接口。

package com.chybinmy.hadoop.mapreduce;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.util.Tool;

public class WordCountMapReduce extends Configuration
implements Tool {

}

2、   Map类

public
static class
WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    @Override

    public void map(LongWritable key, Text value, Context context)

            throws IOException,
InterruptedException {

        String line = value.toString();

        String[] cols = line.split("\t");

        for (String col : cols) {

           
context.write(new Text(col),
new IntWritable(1));

        }

    }



    // step 2: Reducer Class

   
@Override

    protected void cleanup(Context context) throws IOException,

           
InterruptedException {

        // TODO



   
}



    @Override

    protected void setup(Context context) throws IOException,

           
InterruptedException {

        // TODO



   
}

}

3、   Reduce类

public static class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
    @Override
    public void reduce(Text key, Iterable<IntWritable> values,
                       Context context) throws IOException, InterruptedException {
        int sum=0;
        for (IntWritable count :values)
        {
            sum+=count.get();
        }
        context.write(new Text(key),new IntWritable(sum));
    }

    @Override
    public void cleanup(Context context) throws IOException,
            InterruptedException {
        // TODO

   
}

    @Override
    public void setup(Context context) throws IOException,
            InterruptedException {
        // TODO

   
}
}

4、   run方法

public int run(String[] args) throws Exception {

    Configuration configuration = this.getConf();
    Job job = Job.getInstance(configuration, this.getClass()
            .getSimpleName());

    job.setJarByClass(this.getClass());
    Path inpath = new Path(args[0]);
    FileInputFormat.addInputPath(job, inpath);

    // output:
   
Path outpath = new Path(args[1]);
    FileOutputFormat.setOutputPath(job, outpath);

    // mapper
   
job.setMapperClass(WordCountMapper.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(IntWritable.class);

    // ================shuffle====================
    // 1.
分区
    // job.setPartitionerClass(cls);

    // 2.
排序
    // job.setSortComparatorClass(cls);

    // 3.combiner
优化
    // job.setCombinerClass(WordCountCombiner.class);

    // 4.compress
压缩

    // 5.group
分组
    // job.setGroupingComparatorClass(cls);

    // ================shuffle====================

    // reducer
   
job.setReducerClass(WordCountReduce.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);

    //reduce个数可以指定
    //job.setNumReduceTasks(tasks);

    // submit job
   
boolean isSucces = job.waitForCompletion(true);
    return isSucces ? 0 : 1;
}

5、   main方法

public static void main(String[] args) throws Exception {
    args = new String[] {
            "hdfs://bigdata-senior01.chybinmy.com:8020/wordcountdemo/input/wordcount.input",
            "hdfs://bigdata-senior01.chybinmy.com:8020/wordcountdemo/output1" };

    Configuration configuration = new Configuration();
    int status = ToolRunner.run(configuration, new WordCountMapReduce(), args);
    System.exit(status);
}

五、打包JAR,在YARN上运行

1、   将打包好的jar包放在

2、   运行jar

[hadoop@bigdata-senior01 hadoop-2.5.0]$ bin/yarn jar
/opt/mysource/mapreduce.jar com.chybinmy.hadoop.mapreduce.WordCountMapReduce /wordcountdemo/input/wordcount.input
/wordcountdemo/output3

3、   查看结果

[hadoop@bigdata-senior01
hadoop-2.5.0]$ bin/hdfs dfs -text /wordcountdemo/output3/part*

hadoop  3

hbase   1

hive   
2

mapreduce      
1

spark   2

sqoop   1

storm   1

六、以WordCount为例理解MapReduce并行运行过程

1、 流程图

2、 执行过程描述

(1)    每个分片数据分配一个map任务,任务内容是用户写的map函数,map函数是尽量运行在数据分片的机器上,这样保证了“数据本地优化”。

(2)    map任务的结果是各自排好序的,各个map结果进行再次排序合并后,作为reduce任务的输入。

(3)    reduce任务执行reduce函数来处理数据,得到最终结果后,存入HDFS。

(4)    会有多个reduce任务,每个reduce任务的输入都来自于许多map任务,map任务和reduce任务之间是需要传输数据的,占用网络资源,影响效率,为了减少数据传输,可以在map()函数后,添加一个combiner函数来对结果做预处理。

附件列表

MapReduce 单词统计案例编程的更多相关文章

  1. 2.Storm集群部署及单词统计案例

    1.集群部署的基本流程 2.集群部署的基础环境准备 3.Storm集群部署 4.Storm集群的进程及日志熟悉 5.Storm集群的常用操作命令 6.Storm源码下载及目录熟悉 7.Storm 单词 ...

  2. 关于MapReduce单词统计的例子:

    要统计的文件的文件名为hello hello中的内容如下 hello you hello me 通过MapReduce程序统计出文件中的各个单词出现了几次.(两个单词之间通过tab键进行的分割) im ...

  3. hadoop笔记之MapReduce的应用案例(WordCount单词计数)

    MapReduce的应用案例(WordCount单词计数) MapReduce的应用案例(WordCount单词计数) 1. WordCount单词计数 作用: 计算文件中出现每个单词的频数 输入结果 ...

  4. 大数据学习——mapreduce程序单词统计

    项目结构 pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&q ...

  5. 【Cloud Computing】Hadoop环境安装、基本命令及MapReduce字数统计程序

    [Cloud Computing]Hadoop环境安装.基本命令及MapReduce字数统计程序 1.虚拟机准备 1.1 模板机器配置 1.1.1 主机配置 IP地址:在学校校园网Wifi下连接下 V ...

  6. 2、 Spark Streaming方式从socket中获取数据进行简单单词统计

    Spark 1.5.2 Spark Streaming 学习笔记和编程练习 Overview 概述 Spark Streaming is an extension of the core Spark ...

  7. Spark入门(三)--Spark经典的单词统计

    spark经典之单词统计 准备数据 既然要统计单词我们就需要一个包含一定数量的文本,我们这里选择了英文原著<GoneWithTheWind>(<飘>)的文本来做一个数据统计,看 ...

  8. Java实现单词统计

    原文链接: https://www.toutiao.com/i6764296608705151496/ 单词统计的是统计一个文件中单词出现的次数,比如下面的数据源 其中,最终出现的次数结果应该是下面的 ...

  9. ytu 2002:C语言实验——单词统计(水题)

    C语言实验——单词统计 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 61  Solved: 34[Submit][Status][Web Board] ...

随机推荐

  1. The Swiss Army Knife of Data Structures … in C#

    "I worked up a full implementation as well but I decided that it was too complicated to post in ...

  2. 使用JDBC调用存储过程

    DELIMITER $$ DROP PROCEDURE IF EXISTS `jdbc`.`addUser` $$ ),in birthday date,in money float,out pid ...

  3. 喜大普奔,微软Microsoft JDBC Driver For SQL Server已发布到maven中央仓库

    相信通过java和SQLServer开发应用的同学们都经历过如下类似的问题. 微软提供的JDBC官方驱动没有放置在Maven仓库中,这样如果你的Java应用需要访问SQL Server,你不得不下载s ...

  4. 如何在 ASP.NET MVC 中集成 AngularJS(2)

    在如何在 ASP.NET MVC 中集成 AngularJS(1)中,我们介绍了 ASP.NET MVC 捆绑和压缩.应用程序版本自动刷新和工程构建等内容. 下面介绍如何在 ASP.NET MVC 中 ...

  5. 在Windows中安装Memcached

    Memcached是一个高并发的内存键值对缓存系统,它的主要作用是将数据库查询结果,内容,以及其它一些耗时的计算结果缓存到系统内存中,从而加速Web应用程序的响应速度. Memcached最开始是作为 ...

  6. LInux 安装东西

    本机   PHP 安装 ./configure --prefix=/usr/local/php5 --with-gd --enable-gd-native-ttf --enable-gd-jis-co ...

  7. Atitti 存储引擎支持的国内点与特性attilax总结

    Atitti 存储引擎支持的国内点与特性attilax总结 存储引擎处理的事情: · 并发性:某些应用程序比其他应用程序具有很多的颗粒级锁定要求(如行级锁定). · 事务支持:并非所有的应用程序都需要 ...

  8. FreeBSD_11-系统管理——{Part_7 - AUDIT}

    相关概念 EVENT 事件,审计系统计录的对象,包括用户登陆.网络与文件操作等各方面 CLASS 类,对具有相同或类似属性的事件的分組 RECORD 记录,审计系统生成的日志中的每一条信息 TRAIL ...

  9. uwp 图片切换动画 使用帧动画

    上一篇博客使用了Timer来实现图片的切换,@lindexi_gd讨论了一下性能,我本人其实对性能这一方面不太熟,但我觉得还是有必要考虑一下,那么今天我们使用帧动画开实现以下 新建项目,添加一个But ...

  10. C++构造函数和析构函数

    构造函数简介 在上一个章节我们在创建好类的对象之后,首先对它的每一个成员属性赋值之后再对它们进行输出操作,如果不赋值就输出,这些值就会是垃圾值.而为了代码的简介,一次性为所有成员属性初始化,C++的类 ...