需求描述
要求:
1.将待分析的文件(不少于10000英文单词)上传到HDFS。
2.调用MapReduce对文件中各个单词出现的次数进行统计。
3.将统计结果下载本地。
注意:需要实现的是调用MapReduce对文件中各个单词出现的次数进行统计。要求在Linux系统中实现上述操作。首先要安装Ubuntu系统,然后要配置Java环境,安装JDK。Ubuntu提供了一个健壮,功能丰富的计算环境。
环境介绍
Ubuntu 14.04
Hadoop 2.6.0(伪分布式)
Eclipse 3.8
实验步骤
一、准备环境
1. 安装Eclipse
安装完成后打开eclipse
2. 在Eclipse中创建项目并添加需要用到的JAR包
第一次打开需要填写workspace(工作空间),保持默认即可
选择“File->New->Java Project”菜单,开始创建一个Java工程
添加需要的jar包,为了编写一个能够与HDFS交互的Java应用程序,一般需要向Java工程中添加以下JAR包:
(1)”/usr/local/hadoop/share/hadoop/common”目录下的hadoop-common-2.7.1.jar和haoop-nfs-2.7.1.jar;
(2)/usr/local/hadoop/share/hadoop/common/lib”目录下的所有JAR包;
(3)“/usr/local/hadoop/share/hadoop/hdfs”目录下的haoop-hdfs-2.7.1.jar和haoop-hdfs-nfs-2.7.1.jar;
(4)“/usr/local/hadoop/share/hadoop/hdfs/lib”目录下的所有JAR包。
比如,如果要把“/usr/local/hadoop/share/hadoop/common”目录下的hadoop-common-2.7.1.jar和haoop-nfs-2.7.1.jar添加到当前的Java工程中,可以在界面中点击目录按钮,进入到common目录,然后,界面会显示出common目录下的所有内容
3. 编写并编译运行Java应用程序
鼠标右键点击你创建的工程,选择“New->Class”菜单,
我这里是之前做的一个实验判断HDFS中是否存在某文件,程序名为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();
}
}
}
4.编译运行程序
在开始编译运行程序之前,请一定确保Hadoop已经启动运行,如果还没有启动,需要打开一个Linux终端,输入以下命令启动Hadoop:
cd /usr/local/hadoop
./sbin/start-dfs.sh
jps#执行出现namenode secondnode datanode证明启动成功
现在就可以编译运行上面编写的代码。可以直接点击Eclipse工作界面上部的运行程序的快捷按钮,当把鼠标移动到该按钮上时,在弹出的菜单中选择“Run As”,继续在弹出来的菜单中选择“Java Application”
到这里说明eclipse运行所需要的环境都没问题,接下来就是执行MapReduce程序完成这次实验的要求:词频统计。
二、调用MapReduce对文件中各个单词出现的次数进行统计
1.配置 eclipse运行MapReduce需要的插件及环境请参考下面的文章:
使用Eclipse编译运行MapReduce程序_Hadoop2.6.0_Ubuntu/CentOS_厦大数据库实验室博客 (xmu.edu.cn)
2.配置好需要的插件后创建MapReduce程序
点击File选择new project 按照下图操作:
创建好后鼠标右键点击新建class 名称取WordCount即可。
3.程序代码(在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);
}
}
}
}
4.运行:右键选择Run as ->run on Hadoop
显示已存在output文件就删除,不存在input文件就创建
cd /usr/local/hadoop
./bin/hdfs dfs -rm -r output #删除output文件夹
./bin/hdfs dfs -mkdir input #创建input文件 如果出错就加上 -p
5.为了方便命令行执行java打包的程序,先创建一个test文件夹保存打包的程序
mkdir /usr/local/hadoop/test
在工程名称“WordCount”上点击鼠标右键,在弹出的菜单中选择“Export”,弹出下图界面之后点击Next,再接着点Next
4.点击"Finsh“之后弹出以下界面之后可以点击ok,忽略它
在运行程序之前,需要启动Hadoop,启动Hadoop之后,需要首先删除HDFS中与当前Linux用户hadoop对应的input和output目录,命令上面有。
在Linux本地文件系统中新建的文件上传到HDFS的“/user/hadoop/input”目录下(我是在网上找10000字的英语文章),命令如下:
cd /usr/local/hadoop
./bin/hdfs dfs -put ./wordtets.txt input #wordtest.txt是待词频统计的文本文件
./bin/hdfs dfs -ls input #查看是否上传成功
最后查看文本已经上传成功了
使用hadoop jar命令运行程序,命令如下
cd /usr/local/hadoop
./bin/hadoop jar ./test/WordCount.jar input output
最后查看统计结果:
./bin/hdfs dfs -cat output/part-r-00000
出现统计结果
下载统计结果到本地:
./bin/hdfs dfs -get output
ls #出现的output目录就是下载到本地的统计结果
cat ./output/part-r-00000 #结果和HDFS上的output中的一样
这样全部要求就完成了