目录
[一、MapReduce 示例程序的导入并运行测试](#一、MapReduce 示例程序的导入并运行测试)
[二、准备 4 个小文件(文件大小分别为 1.7M,5.1M,3.4M,6.8M)](#二、准备 4 个小文件(文件大小分别为 1.7M,5.1M,3.4M,6.8M))
[1. 第一种情况,默认分片:不修改程序代码,直接使用 WordCount 源程序](#1. 第一种情况,默认分片:不修改程序代码,直接使用 WordCount 源程序)
[2. 第二种情况,在代码中增加如下内容](#2. 第二种情况,在代码中增加如下内容)
[3. 第三种情况,将数值设为 20M](#3. 第三种情况,将数值设为 20M)
[三、对 sogou.500w.utf8 数据进行分析,使用 MapReduce 编写程序完成。](#三、对 sogou.500w.utf8 数据进行分析,使用 MapReduce 编写程序完成。)
[1. 程序源代码](#1. 程序源代码)
[2. 程序输出结果](#2. 程序输出结果)
一、MapReduce 示例程序的导入并运行测试
步骤 1 :在 eclipse 中创建 Java Project → new Package
步 骤 2 : 将 /home/2130502441ryx/hadoop-3.1.3-src/hadoop-mapreduce-project/hadoop-mapreduce-examples/src/main/java/org/apache/hadoop/examples 目录下的 WordCount.java 文件通过复制命令 cp 放到桌面/home/gdpu/Desktop,如下所示:
bash
// 解压压缩包
tar -zxvf /home/2130502441ryx/hadoop-3.1.3-src.tar.gz /home/2130502441ryx
![](https://img-blog.csdnimg.cn/direct/e9b0abdd4db34c13afac4f30190dcf06.png)
bash
// 复制文件
cp /home/2130502441ryx/hadoop-3.1.3-src/hadoop-mapreduce-project/hadoop-mapreduce-examples/src/main/java/org/apache/hadoop/examples/WordCount.java
![](https://img-blog.csdnimg.cn/direct/facf7ac2756c4388a493e7bd7c15e5e6.png)
![](https://img-blog.csdnimg.cn/direct/30b23ca12e144c1fb302f42ebf96bcc8.png)
步骤 3:将 WordCount.java 文件通过复制粘贴,拷贝至 eclipse 对应项目的包下。
![](https://img-blog.csdnimg.cn/direct/ea9ac94e2e5f4e29b9c0e03734aa3060.png)
步骤 4:导入依赖包 External jars,将以下文件夹里面的 jar 加入到项目的 build path。
\share\hadoop\common
\share\hadoop\common\lib
\share\hadoop\hdfs
\share\hadoop\mapreduce
\share\hadoop\yarn
步骤 5:将项目打包成 jar 包,项目点右键---> export ---> Java(JAR file)。
![](https://img-blog.csdnimg.cn/direct/e4c46b37fb194ef29278cadde51aca10.png)
步骤 6:在 HDFS 文件系统上创建文件夹 input,并上传一些文本文件到该目录,文件自行在操作系统上选取。
bash
hdfs dfs -mkdir /input
hdfs dfs -ls /
![](https://img-blog.csdnimg.cn/direct/984e9567fbcb4a228c300c576b606e47.png)
bash
hdfs dfs -put /home/2130502441ryx/testWordCount /input
hdfs dfs -ls /input
hdfs dfs -cat /input/testWordCount
![](https://img-blog.csdnimg.cn/direct/00e8fabe55dc44e8baaeb5e000e42260.png)
步骤 7:运行 WordCount 示例程序,将运行程序的命令和结果截图在下面。
bash
hadoop jar /home/2130502441ryx/MapReduceTest.jar org/ryx/WordCount /input
![](https://img-blog.csdnimg.cn/direct/c9a3cf71020d4978846ed5fb84aa84e7.png)
bash
hdfs dfs -ls /output
hdfs dfs -cat /output/part-r-00000
![](https://img-blog.csdnimg.cn/direct/cf0990baf6aa46d7a09704769f068453.png)
二、准备 4 个小文件(文件大小分别为 1.7M,5.1M,3.4M,6.8M)
a.txt在我上传的资源中,可以免费下载!!
https://download.csdn.net/download/m0_67830223/89498183?spm=1001.2014.3001.5503
bash
cat a.txt >>b.txt
cat a.txt >>b.txt
cat a.txt >>c.txt
cat a.txt >>c.txt
cat a.txt >>c.txt
cat a.txt >>d.txt
cat a.txt >>d.txt
cat a.txt >>d.txt
cat a.txt >>d.txt
![](https://img-blog.csdnimg.cn/direct/95a2495d8d1c45508311180636ca3460.png)
![](https://img-blog.csdnimg.cn/direct/02275e99cd9349a88dae998d7af6f3d9.png)
将上述 4 个文件上传至 HDFS 文件系统作为 WordCount 的输入,运行WordCount 程序观察分片情况。
bash
hdfs dfs -ls /input1
![](https://img-blog.csdnimg.cn/direct/6c3393b49dd74bbc9a06f8499eee0204.png)
1. 第一种情况,默认分片:不修改程序代码,直接使用 WordCount 源程序
bash
hadoop jar /home/2130502441ryx/MapReduceTest.jar org/ryx/WordCount /input1 /output1
![](https://img-blog.csdnimg.cn/direct/f797519119b34a93baef3acba5969faf.png)
2. 第二种情况,在代码中增加如下内容
java
job.setInputFormatClass(CombineTextInputFormat.class);
CombineTextInputFormat.setMaxInputSplitSize(job, 4194304);
添加代码如下:
![](https://img-blog.csdnimg.cn/direct/dd30d66e8523412ebfb57cb8b99ea944.png)
运行截图:
bash
hadoop jar /home/2130502441ryx/MapReduceTest.jar org/ryx/WordCount /input1 /output2
![](https://img-blog.csdnimg.cn/direct/2cd405c895bb46d880810e9984eaaa5f.png)
3. 第三种情况,将数值设为 20M
java
CombineTextInputFormat.setMaxInputSplitSize(job, 20971520);
修改代码如下:
![](https://img-blog.csdnimg.cn/direct/708de66507bc468ab6682e56b4957bc3.png)
运行截图:
bash
hadoop jar /home/2130502441ryx/MapReduceTest.jar org/ryx/WordCount /input1 /output3
![](https://img-blog.csdnimg.cn/direct/851a83efa1fc4f65a56da518fed3204b.png)
三、对 sogou.500w.utf8 数据进行分析,使用 MapReduce 编写程序完成。
将程序主要代码复制或者截图在下面,包括主要的 Mapper 类,Reducer 类和 Partition 类,并将打包运行后的结果输出并截图。
分析统计用户查询关键字长度的次数分布,关键字长度为 1 的搜索次数,长度为 2 的搜索次数,...,长度为 N 的搜索次数。输出结果将按搜索长度分为 3 组,长度在小于等于 5 的分一组,长度在 6 到 10 之间的分一组,大于 10 的分一组。
bash
hdfs dfs -ls /
![](https://img-blog.csdnimg.cn/direct/218956dd4eb5416f93bf023801f00d2f.png)
1. 程序源代码
① KeywordLengthMapper
![](https://img-blog.csdnimg.cn/direct/7b4d0ccf88c04238b7561e60462df6a4.png)
② KeywordLengthReducer
![](https://img-blog.csdnimg.cn/direct/b0476e4ffbb44ecd96420b9e9c477bec.png)
③ KeywordLengthPartitioner
![](https://img-blog.csdnimg.cn/direct/c7aed2229bd34e0ba6e7977a07a1b2e0.png)
④ KeywordLengthAnalysis
![](https://img-blog.csdnimg.cn/direct/f5f1b23ff54d49719b70bf301aecbe4b.png)
2. 程序输出结果
bash
hadoop jar /home/2130502441ryx/MapReduceTest.jar org/ryx/KeyWordLengthAnalysis /sogou.500.utf8 /output4
![](https://img-blog.csdnimg.cn/direct/dce66b813b5946bcb8f2ee9a606d77d0.png)
bash
hdfs dfs -ls /output4
![](https://img-blog.csdnimg.cn/direct/17a07b62bdf9421ea0eac0905fa51edf.png)
bash
hdfs dfs -cat /output4/part-r-00000
hdfs dfs -cat /output4/part-r-00001
hdfs dfs -cat /output4/part-r-00002
![](https://img-blog.csdnimg.cn/direct/431e8d0bd55241d9bfba08ef47214de8.png)