实现MapReduce程序完成单词统计

一、目的

理解MapReduce在Hadoop体系结构中的角色,通过该实验后,能设计开发简单的MapReduce程序。

二、设备

计算机:CPU四核i7 6700处理器;内存8G; SATA硬盘2TB硬盘; Intel芯片主板;集成声卡、千兆网卡、显卡; 20寸液晶显示器。
编译环境:(1)操作系统:Linux (2)Hadoop版本:2.7.2 机器:虚拟机3台 (3)Eclipse 4.7

三、内容

3.1启动Hadoop服务

(1)格式化namenode。
(2)启动Hadoop。

[root@hadoop101 ~]# cd /opt/module/hadoop-2.7.2/
[root@hadoop101 hadoop-2.7.2]# sbin/./start-all.sh

1
(3)用jps验证服务器服务是否启动成功。

[root@hadoop101 hadoop-2.7.2]# jps

2
3
4

3.2开发LineCount程序

(1)打开Eclipse开发工具,新建Maven项目。
5
6
7

8
(2)WORDCOUNT代码
WordCountMapper:

package com.lizi.mr;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //拿到输入的这行数据
        String line = value.toString();
        //根据空格进行分割得到这行的单词
        String[] words = line.split(" ");
        //将单词输出为 <word,1>
        for (String word : words) {
            //将单词作为key ,将次数 做为value输出,
            // 这样也利于后面的数据分发,可以根据单词进行分发,
            // 以便于相同的单词落到相同的reduce task 上,方便统计
            context.write(new Text(word), new IntWritable(1));
        }
    }
}

WordCountReduce:

package com.lizi.mr;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        String word = key.toString();
        int count = 0;
        for (IntWritable value : values) {
            count += value.get();
        }
        context.write(key, new IntWritable(count));
    }
}

WordCountDriver:

package com.lizi.mr;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class WordCountDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        // mapreduce.framework.name 配置成 local 就是本地运行模式,默认就是local
        // 所谓的集群运行模式 yarn ,就是提交程序到yarn 上. 要想集群运行必须指定下面三个配置.
        conf.set("mapreduce.framework.name", "yarn");
        conf.set("yarn.resoucemanager.hostname", "hadoop101");
        conf.set("fs.defaultFS","hdfs://hadoop101:9000/");
        Job job = Job.getInstance(conf);
        //指定本程序的jar 包 所在的本地路径
        job.setJarByClass(WordCountDriver.class);
        //指定本次业务的mepper 和 reduce 业务类
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReduce.class);
        //指定mapper 输出的 key  value 类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //指定 最终输出的 kv  类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        //指定job的输入原始文件所在目录
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        //指定job 输出的文件目录
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        boolean waitForCompletion = job.waitForCompletion(true);
        System.exit(waitForCompletion ? 0 : 1);
    }
}

3.3导出jar文件。

我们需要将开发的LineCount.java编译后的class打成jar包,并上传到master服务器上才能运行。可以使用jar命令也可以使用Eclipse里的导出jar包功能。在MapReduce项目上点击右键,点击“export”,如下图:
1
2

3.4将jar文件上传到master服务器上。

1

3.5准备测试数据

首先在本地见文件,编写内容,之后上传到HDFS系统上面。

[root@hadoop101 hadoop-2.7.2]# cd /home/hadoop/
[root@hadoop101 hadoop]# lstest.txt  下载
[root@hadoop101 hadoop]# touch file1.txt
[root@hadoop101 hadoop]# vi file1.txt 

5
上传到HDFS系统:

[root@hadoop101 hadoop-2.7.2]# hadoop fs -mkdir -p /user/hadoop/mapreduce/input
[root@hadoop101 hadoop-2.7.2]# hadoop fs -put /home/hadoop/file1.txt /user/hadoop/mapreduce/input/
[root@hadoop101 hadoop-2.7.2]# hadoop fs -ls /user/hadoop/mapreduce/input/
Found 1 items
-rw-r--r--   2 root supergroup         71 2020-05-12 23:43 /user/hadoop/mapreduce/input/file1.txt

运行JAR包:

[root@hadoop101 hadoop-2.7.2]# hadoop jar /opt/jar/mapreduce/WordCount.jar com/lizi/mr/WordCountDriver /user/hadoop/mapreduce/input/file1.txt /user/hadoop/mapreduce/output1/

查看输出目录:

[root@hadoop101 hadoop-2.7.2]# hadoop fs -ls /user/hadoop/mapreduce/output1
Found 2 items
-rw-r--r--   2 root supergroup          0 2020-05-12 23:53 /user/hadoop/mapreduce/output1/_SUCCESS
-rw-r--r--   2 root supergroup         73 2020-05-12 23:53 /user/hadoop/mapreduce/output1/part-r-00000

查看part-r-00000:

[root@hadoop101 hadoop-2.7.2]# hadoop fs -cat /user/hadoop/mapreduce/output1/part-r-00000

3
3

  • 4
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值