
- 需求描述
- 一、环境介绍
- 二、具体步骤
需求描述
1.将待分析的文件(不少于10000英文单词)上传到HDFS。
2.调用MapReduce对文件中各个单词出现的次数进行统计。
3.将统计结果下载本地。
一、环境介绍
Ubuntu 14.04
Hadoop 2.6.0
Eclipse 3.8
JAVA环境
二、具体步骤 1.将一个10000字英语单词的文件直接拖到hadoop中 2.启动hadoopcd /usr/local/hadoop ./sbin/start-dfs.sh
jps查看是否启动成功
3.将文件上传至HDFS并查看是否成功./bin/hdfs dfs -put /home/hadoop/xxx.txt input ./bin/hdfs dfs -ls input #查看是否上传成功4.在应用商店中下载Eclipse 5.安装Hadoop-Eclipse-Plugin
unzip -qo ~/下载/hadoop2x-eclipse-plugin-master.zip -d ~/下载 #解压到~/下载 中 sudo cp ~/下载/hadoop2x-eclipse-plugin-master/release/hadoop-eclipse-plugin-2.6.0.jar /usr/lib/eclipse/plugins/ #复制到 eclipse 安装目录的 plugins 目录下 /usr/lib/eclipse/eclipse -clean6.配置Hadoop-Eclipse-Plugin
选择 Window 菜单下的 Preference。窗体的左侧会多出 Hadoop Map/Reduce 选项,点击此选项,选择 Hadoop 的安装目录
切换 Map/Reduce 开发视图,选择 Window 菜单下选择 Open Perspective -> Other,d出一个窗体,从中选择 Map/Reduce 选项即可进行切换。
建立与 Hadoop 集群的连接,点击 Eclipse软件右下角的 Map/Reduce Locations 面板,在面板中单击右键,选择 New Hadoop Location。
7.在Eclipse中创建MapReduce项目点击 File 菜单,选择 New -> Project,选择 Map/Reduce Project,点击 Next。
填写 Project name 为 WordCount 即可,点击 Finish 就创建好了项目。
接着右键点击刚创建的 WordCount 项目,选择 New -> Class
在 Package 处填写 org.apache.hadoop.examples;在 Name 处填写 WordCount。
8.将有修改过的配置文件复制到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
9.通过Eclipse设定运行参数
右键点击刚创建的 WordCount.java,选择 Run As -> Run Configurations,在此处可以设置运行时的相关参数(如果 Java Application 下面没有 WordCount,那么需要先双击 Java Application)。切换到 “Arguments” 栏,在 Program arguments 处填写 “input output” 就可以了。
10.编译程序代码并运行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
11.打包程序并放在/usr/local/hadoop目录下
在 Eclipse 工作界面左侧的“Package Explorer”面板中,在工程名称“WordCount” 上点击鼠标右键,在d出的菜单中选择“Export”。
12.查看output生成的文件并运行
cd /usr/local/hadoop ./bin/hdfs dfs -ls output #查看output生成的文件 ./bin/hdfs dfs -cat output/xxxxxxxxx13.将统计结果下载至本地
./bin/hdfs dfs -get output ./output
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)