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. 思科交换机配置DHCP的四个方面

    这里我们主要讲解了思科交换机配置DHCP的相关内容.我们对网络拓扑先进行一下了解,然后对于其在进行一下说明,之后对于配置的代码和命令再进行一下解析. 思科交换机配置DHCP一.网络拓扑 思科交换机配置 ...

  2. 登陆数据库,界面一直保持正在登陆的状态,oracle使用界面无法登陆

    原因:关机时没有关闭oracle窗口. 导致在登陆数据库的时候,使用oracle的这个界面登陆时,界面一直保持''正在登陆''的状态,一旦点击就会卡住,使界面变得无法响应. 然后使用sqlplus仍然 ...

  3. 实践.Net Core在Linux环境下的第一个Hello World

    基础环境和相关软件准备 1.CentOS7.1 64位系统(或者其他CentOS版本的64位系统) 2.WinSCP软件(主要是方便管理和编辑Linux系统的文件) 3.XShell软件(Window ...

  4. 探索C#之虚拟桶分片

    阅读目录 背景 虚拟桶(virtual buckets) 实现 总结 背景 关于数据分片讨论最多的是一致性hash,然而它并不是分布式设计中的银弹百试百灵. 在数据稳定性要求比较高的场景下它的缺点是不 ...

  5. 比官方教程代码更简短的SignalR Server Broadcast示例

    SignalR是微软ASP.NET技术体系中的新成员. 在www.asp.net网站上的SignalR专区有一篇SignalR的入门级教程<Tutorial: Server Broadcast  ...

  6. Asp.net MVC5 路由Html后缀的问题

    环境:VS2013+MVC5+IIS EXPRESS 问题:如果从Asp.net Web迁移到MVC,可能会遇到需要使原来的链接(如http://localhost:12345/old/library ...

  7. iOS-多线程基础

    进程与线程: 1>   一个应用程序对应一个进程,一个进程帮助程序占据一块存储空间 2>   要想在进程中执行任务,就必须开启线程,一条线程就代表一个任务 3>   一个进程中允许开 ...

  8. HTML5新增标签与属性

    目录 一.HTML5新增属性 1.1.contextmenu 1.2.contentEditable 1.3.hidden 1.4.draggable 1.5.data-* 1.6.placehold ...

  9. Unity 特殊文件夹 : 位置不能随便放

    有以下几个文件夹: Assets 用来存放资源的文件夹,包括各种材质.模型等 Editor 编辑器类等脚本 Editor Default Resources Editor scripts can ma ...

  10. OpenCASCADE PCurve of Topological Face

    OpenCASCADE PCurve of Topological Face eryar@163.com Abstract. OpenCASCADE provides a class BRepBuil ...