实验配置:系统:Ubuntu | 环境:Hadoop | 软件:Eclipse
要求
1.将待分析的文件(不少于10000英文单词)上传到HDFS
2.调用MapReduce对文件中各个单词出现的次数进行统计
3.将统计结果下载本地。
操作步骤:调用MapReduce执行WordCount对单词进行计数**
一:
• 1. 在Eclipse中创建项目
• 2. 为项目添加需要用到的JAR包
• 3. 编写Java应用程序
• 4. 编译运行程序
• 5. 应用程序部署
二:
• 1. 在Eclipse中创建项目
• 2. 为项目添加需要用到的JAR包
• 3. 编写Java应用程序
• 4. 编译打包程序
• 5. 运行程序
(一):
1.打开Eclipse,需要填写workspace(工作空间),用来保存程序所在的位置,这里按照默认,不需要改动,如下图,点击“OK”按钮,进入Eclipse软件。
2.选择“File->New->Java Project”菜单,开始创建一个Java工程
3.点击界面中的“Libraries”选项卡,然后,点击界面右侧的“Add External JARs
4.进入到common目录,然后,界面会显示出common目录下的所有内容,用鼠标点击选中hadoop-common-2.7.1.jar和haoop-nfs-2.7.1.jar,然后点击界面右下角的“确定”按钮,就可以把这两个JAR包增加到当前Java工程中
(二):
在创建好的工程名称“HDFSExample”右键菜单中
选择New->Class,如下图
这时,Eclipse自动创建了一个名为“HDFSFileIfExist.java”的源代码文件,在文件输入以下代码并运行,如下图
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HDFSFileIfExist {
public static void main(String[] args){
try{
String fileName = "test";
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
FileSystem fs = FileSystem.get(conf);
if(fs.exists(new Path(fileName))){
System.out.println("文件存在");
}else{
System.out.println("文件不存在");
}
}catch (Exception e){
e.printStackTrace();
}
}
}
(三):在Hadoop安装目录下新建一个名称为myapp的目录,用来存放我们自己编写的Hadoop应用程序
在Linux系统中查看一下生成的HDFSExample.jar文件,再使用hadoop jar命令运行程序
(四):
1.启动Eclipse,选择“File–>New–>Java Project”菜单,开始创建一个Java工程,弹出如下图所示界面。
2.接下来在这个界面,把剩余的其他JAR包都添加进来。
(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目录。如下图
(五):
1.把创建的“WordCount”工程名上右击,选择New->Class菜单,弹出以下界面
2.Eclipse自动创建了一个名为“WordCount.java”的源代码文件,然后在该文件中输入完整的词频统计程序代码,如下图
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);
}
}
}
}
3.在工程名称“WordCount”上点击鼠标右键,在弹出的菜单中选择“Export”,弹出下图界面之后点击Next,再接着点Next
4.在运行程序之前,需要启动Hadoop,启动Hadoop之后,需要首先删除HDFS中与当前Linux用户hadoop对应的input和output目录(即HDFS中的“/user/hadoop/input”和“/user/hadoop/output”目录),这样确保后面程序运行不会出现问题,然后在HDFS中新建与当前Linux用户hadoop对应的input目录,如下图
cd /usr/local/hadoop
./bin/hdfs dfs -mkdir input
在Linux本地文件系统中新建的文件上传到HDFS的“/user/hadoop/input”目录下(我是在网上找10000字的英语文章),命令如下:
cd /usr/local/hadoop
./bin/hdfs dfs -put ./wordfile.txt input
如果HDFS中已经存在目录“/user/hadoop/output”,则使用如下命令删除该目录:
cd /usr/local/hadoop
./bin/hdfs dfs -rm -r /user/hadoop/output
使用hadoop jar命令运行程序,命令如下
cd /usr/local/hadoop
./bin/hadoop jar ./myapp/WordCount.jar input output
运行结束后,会出现以下界面
5.最后,.将统计结果下载至本地