**词频统计,可以说是hadoop家族入门的hello world
本次将在Ubuntu环境下,调用MapReduce对文件中各个单词出现的次数进行统计**
一、环境介绍
在 VMware Workstation Pro虚拟机环境中安装 Ubuntu系统,并在 Ubuntu 中安装 Hadoop,Eclipse,以及二者所需 Java运行环境(jdk1.8),配置并运行 Hadoop 伪分布式。在Eclipse官网下载压缩版 Eclipse 2021-09进行安装,并使用eclipse-hadoop-plugin插件辅助开发。将待分析的文件上传至 HDFS。
二、数据来源及数据上传
准备一个英文文本,这里使用MySQL官网文档部分截取(来源:https://dev.mysql.com/doc/),字符数(不含空格)为358365,单词量为60320(由word 2017识别统计获得数据)。通过Windows10自带的scp传输至虚拟机内。
检查虚拟机内文件目录已传输成功。
三、数据上传结果查看
启动伪分布式Hadoop,使用jdk中的jps查看关键进程是否正常运行。
使用dfs将文件上传至hdfs文件系统,查看是否上传成功。
四、数据处理过程的描述
下载eclipse-hadoop-plugin插件的jar包,上传至虚拟机。
将插件jar包移动至eclipse的plugins目录
使用eclipse –clean 刷新并启动eclipse
在 Eclipse 中启用插件并连接本地启动的伪分布式Hadoop
该插件可以使hdfs文件系统目录可视化,展示文件目录变化情况,方便我们的开发
在 Eclipse 中创建 MapReduce 项目
在左侧的 Project Explorer 可以看到刚才建立的项目。
新建包com.yan,并在其下新建Java类WordCount。
在WordCount类编写词频统计代码,调用Hadoop进行词频统计。
package org.apache.hadoop.examples;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class WordCount {
public WordCount() {
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = (new GenericOptionsParser(conf, args)).getRemainingArgs();
if(otherArgs.length < 2) {
System.err.println("Usage: wordcount <in> [<in>...] <out>");
System.exit(2);
}
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(WordCount.TokenizerMapper.class);
job.setCombinerClass(WordCount.IntSumReducer.class);
job.setReducerClass(WordCount.IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
for(int i = 0; i < otherArgs.length - 1; ++i) {
FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
}
FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
System.exit(job.waitForCompletion(true)?0:1);
}
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
public IntSumReducer() {
}
public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
int sum = 0;
IntWritable val;
for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get()) {
val = (IntWritable)i$.next();
}
this.result.set(sum);
context.write(key, this.result);
}
}
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
private static final IntWritable one = new IntWritable(1);
private Text word = new Text();
public TokenizerMapper() {
}
public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while(itr.hasMoreTokens()) {
this.word.set(itr.nextToken());
context.write(this.word, one);
}
}
}
}
在运行之前还需将本地Hadoop 的伪分布式配置文件加入到项目中,否则会导致调用出错。
三个必须的配置文件分别来自:
hadoop/etc/hadoop/log4j.properties
hadoop/etc/hadoop/hdfs-site.xml
hadoop/etc/hadoop/core-site.xml
代码逻辑中指定了输入/输出目录和文件名由启动参数获得,所以还需配置jvm启动参数。
将输入文件和结果输出目录填入,以空格键隔开。
点击run按钮运行程序,可以看到控制台输出的文件数据。
此时进入插件为我们提供的DFS-Location视图并刷新,可以发现目录下新增了WordCount-By-LJY目录,正是我们指定的结果输出目录。至此,词频统计运算已完成。
六、处理结果的下载及命令行展示
回到终端窗口,使用hdfs的dfs -ls指令查看,可以发现新增的词频统计结果目录。
进一步查看该目录会发现两个文件,_SUCCESS和part-r-00000,其中前者是MapReduce成功运行的标志,后者则是本次词频统计输出的最终结果
使用hdfs的dfs -get指令,将本次词频统计是输出结果下载至本地的文件系统
(不指定路径即默认为当前目录/usr/local/hadoop)
在当前目录使用cat指令预览词频统计的结果,至此,词频统计的所有步骤完成