一、环境
Ubuntu18.0.4、Hadoop2.7.7、Eclipse2020.3。
(全程需要的文件都会通过百度链接进行分享,见文章内容!)
二、eclipse安装
方式一:Ubuntu软件中心直接安装(不建议)。
方式二:手动安装(建议——必须保持联网,Ubuntu系统不可睡眠)。
链接:https://pan.baidu.com/s/1fxP6b7VCgUkaBtwuCDabhQ
提取码:y8w3
这里我下载完成后存放到“下载”目录中,然后直接提取到了桌面。
这里我选择第2个,手动安装的好处在于可以自己选择文件路径。
我在usr/local/中创建了eclipse文件夹作为安装路径。
创建eclipse文件夹必须使用一下命令,要不然一会选择文件路径会提示eclipse文件夹只读,不可以安装。
cd /usr/local/
sudo mkdir eclipse
sudo chmod -R 777 / usr/local/eclipse
命令输入完成就可以选择路径安装了。
安装完成选择工作空间,如图所示:
这里的工作空间的文件夹就可以手动创建。
三、安装hadoop-eclipse-plugin插件
要在 Eclipse 上编译和运行 MapReduce 程序,需要安装 hadoop-eclipse-plugin,地址如下:
链接:https://pan.baidu.com/s/1S2oc4j6Y6hGwiWhMrPN8iA
提取码:18ex
下载完成后,将release中的hadoop-eclipse-kepler-plugin-2.6.0.jar复制到eclipse安装目录的plugins文件夹中(手动复制即可),如图所示:
插件添加完成后,请重启eclipse,运行一下命令:
/usr/local/eclipse/eclipse/eclipse -clean
四、配置 Hadoop-Eclipse-Plugin
1、配置之前必须保证开启了Hadoop。
我的Hadoop安装路径是:usr/local/hadoop,所以执行如下命令:
cd /usr/local/hadoop
./sbin/start-dfs.sh
2、eclipse启动完成后在左侧的Project Explorer中可以看到 DFS Locations,如果没看到也不要紧。
3、接下来插件还需要进行配置。
第一步:选择 Window 菜单下的 Preference。
第二步:在新窗体中选择Hadoop/Reduce选项,然后选择Hadoop的安装目录。
如果看不到Hadoop/Reduce选项,就进行如下操作,如果有就不需要:
删除eclipse安装目录中的configuration文件夹中的org.eclipse.update文件夹,然后重启eclipse即可。
第三步:切换Map/Reduce开发视图,按下图操作:
点击open就进入了Map/Reduce视图了。
第四步:建立与Hadoop集群的连接,点击eclipse下面的Map/Reduce Locations面板,然后右键单击,选择New Hadoop Location,如图所示:
在弹出的窗口中选择General进行配置,必须与Hadoop的配置保持一致。如图所示即可:
到了这里,简单的配置也就完成了!
五、在eclipse中创建Map/Reduce项目
1、点击 File 菜单,选择 New -> Project…;
2、选择 Map/Reduce Project,点击 Next;
3、填写 Project name 为 WordCount 即可,点击 Finish 就创建好了项目。
4、此时在左侧的 Project Explorer 就能看到刚才建立的项目了。
5、接着右键点击刚创建的 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();
String[] otherArgs=new String[]{"input","output"};
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);
}
}
}
}
六、通过eclipse运行Map/Reduce
在运行 MapReduce 程序前,还需要执行一项重要操作。
将 /usr/local/hadoop/etc/hadoop 中将有修改过的配置文件(如伪分布式需要 core-site.xml 和 hdfs-site.xml),以及 log4j.properties 复制到 WordCount 项目下的 src 文件夹(~/workspace/WordCount/src)中。
地址如下(文件之前我已经按照自己的安装进行了修改的)。
链接:https://pan.baidu.com/s/1xmApe_pwnn4QZZmQ6XLnOw
提取码:lomd
复制完成后,刷新一下项目,结果如图所示:
再者右击java类,选择Run As -> Run on Hadoop,就可以运行 MapReduce 程序了。
此时又会由于没有指定参数,运行时会提示 “Usage: wordcount “,需要通过Eclipse设定一下运行参数。
右键java类,选择Run As -> Run Configurations;
在此处可以设置运行时的相关参数(如果 Java Application 下面没有 WordCount,那么需要先双击 Java Application)。切换到 “Arguments” 栏,在 Program arguments 处填写 “input output” 就可以了。
此时就可以看到成功运行的提示和输出信息了!
至此,就可以使用 Eclipse 进行 MapReduce程序的开发了。
创作不易,请点个赞!参考自厦门大学数据库实验室!