提示:需要安装和配置的地方,在最后的参考材料里面一、将待分析的文件(不少于100000英文单词)上传到HDFS
demo.txt为待分析的文件
启动Hadoop
将该文件上传到hdfs的input文件夹中
确保上传成功
二、调用MapReduce对文件中各个单词出现的次数进行统计打开Eclipse,配置好MapReduce后能看到该文件
创建MapReduce项目
选中Map/Reduce Project,点击next
填写Project name为WordCount,点击Finish就创建好了项目
为项目添加需要用到的JAR包
点击界面右侧的“Add External JARs…”按钮,弹出如下图所示界面
(1)“/usr/local/hadoop/share/hadoop/common”目录下的 hadoop-common-3.1.3.jar和 haoop-nfs-3.1.3.jar;
(2)“/usr/local/hadoop/share/hadoop/common/lib”目录下的所有 JAR
(3)“/usr/local/hadoop/share/hadoop/mapreduce”目录下的所有 JAR 包,但是,不包括 jdiff、lib、lib-examples 和 sources 目录。
(4)“/usr/local/hadoop/share/hadoop/mapreduce/lib”目录下的所有 JAR包
接着右键点击刚创建的 WordCount 项目,选择 New -> Class
在 Package 处填写 org.apache.hadoop.examples;在 Name 处填写 WordCount
创建 Class 完成后,在 Project 的 src 中就能看到 WordCount.java 这个文件。将如下 WordCount 的代码复制到该文件中
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 [...] ");
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 {
private IntWritable result = new IntWritable();
public IntSumReducer() {
}
public void reduce(Text key, Iterable values, Reducer.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
在运行 MapReduce 程序前,将/usr/local/hadoop/etc/hadoop中将有修改过的配置文件(如伪分布式需要core-site.xml和hdfs-site.xml),以及 log4j.properties复制到WordCount项目下的src文件夹(~/workspace/WordCount/src)中:
cp /usr/local/hadoop/etc/hadoop/core-site.xml ~/workspace/WordCount/src cp /usr/local/hadoop/etc/hadoop/hdfs-site.xml ~/workspace/WordCount/src cp /usr/local/hadoop/etc/hadoop/log4j.properties ~/workspace/WordCount/src
复制完成后,右键点击WordCount选择refresh进行刷新,可以看到文件结构如下所示:
在代码中设置好输入参数。可将代码 main() 函数的 String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
改为:
String[] otherArgs=new String[]{"input","output"};
设定参数后,运行程序,可以看到运行成功的提示,刷新DFS Location后也能看到输出的output文件夹
执行MapReduce项目WordCount后,在 output 的 part-r-00000 这个文件中能看到输出结果
用命令查看输出结果
三、将统计结果下载到本地四、参考材料
安装Ubuntu
http://dblab.xmu.edu.cn/blog/2760-2/
创建Hadoop账户、配置SSH;安装Hadoop、java;Hadoop伪分布式配置
http://dblab.xmu.edu.cn/blog/2441-2/
安装Eclipse、配置 Hadoop-Eclipse-Plugin
http://dblab.xmu.edu.cn/blog/hadoop-build-project-using-eclipse/



