实现MapReduce程序完成单词统计
程序员文章站
2022-05-01 11:30:20
...
一、实验目的
理解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。
[aaa@qq.com ~]# cd /opt/module/hadoop-2.7.2/
[aaa@qq.com hadoop-2.7.2]# sbin/./start-all.sh
(3)用jps验证服务器服务是否启动成功。
[aaa@qq.com hadoop-2.7.2]# jps
3.2开发LineCount程序
(1)打开Eclipse开发工具,新建Maven项目。
(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”,如下图:
3.4将jar文件上传到master服务器上。
3.5准备测试数据
首先在本地见文件,编写内容,之后上传到HDFS系统上面。
[aaa@qq.com hadoop-2.7.2]# cd /home/hadoop/
[aaa@qq.com hadoop]# lstest.txt 下载
[aaa@qq.com hadoop]# touch file1.txt
[aaa@qq.com hadoop]# vi file1.txt
上传到HDFS系统:
[aaa@qq.com hadoop-2.7.2]# hadoop fs -mkdir -p /user/hadoop/mapreduce/input
[aaa@qq.com hadoop-2.7.2]# hadoop fs -put /home/hadoop/file1.txt /user/hadoop/mapreduce/input/
[aaa@qq.com 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包:
[aaa@qq.com 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/
查看输出目录:
[aaa@qq.com 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:
[aaa@qq.com hadoop-2.7.2]# hadoop fs -cat /user/hadoop/mapreduce/output1/part-r-00000