Eclipse编译运行MapReduce程序 Hadoop
来源:互联网 发布:乐天72纯黑巧克力 知乎 编辑:程序博客网 时间:2024/04/30 03:14
方法行,但代码如是2.0请修改
准备工作
安装好 Eclipse,可在 Ubuntu 软件中心直接搜索安装。
在左侧任务栏,点击“Ubuntu软件中心”。
在右上角搜索栏中搜索 eclipse,在搜索结果中单击 eclipse,并点击安装。
如此,就完成了 eclipse 的安装。Eclipse 的默认安装目录为:/usr/lib/eclipse。
安装 Hadoop-Eclipse-Plugin
下载 hadoop2x-eclipse-plugin ,将 release 中的 hadoop-eclipse-kepler-plugin-2.2.0.jar (虽然标注的是 2.2.0,但在 2.6.0 下是没问题的,应该在 2.x 版本下都可以)复制到 Eclipse 安装目录的 plugin 文件夹中,运行 eclipse -clean
重启 Eclipse 即可。
cd ~/下载/
unzip ./hadoop2x-eclipse-plugin-master.zip
cd /usr/lib/eclipse
sudo cp ~/下载/hadoop2x-eclipse-plugin-master/release/hadoop-eclipse-kepler-plugin-2.2.0.jar ./plugins/
./eclipse -clean
配置 Hadoop-Eclipse-Plugin
启动 Eclipse 后就可以在左侧的Project Explorer中看到 DFS Locations(若看到的是 welcome 界面,点击左上角的 x 关闭就可以看到了)。
插件需要进一步的配置。
第一步:选择 Window 菜单下的 Preference。
此时会弹出一个窗体,窗体的左侧会多出 Hadoop Map/Reduce 选项,点击此选项,选择 Hadoop 的安装目录(如/usr/local/hadoop,Ubuntu不好选择目录,直接输入就行)。
第二步:切换 Map/Reduce 工作目录,选择 Window 菜单下选择 Open Perspective -> Other,弹出一个窗体,从中选择 Map/Reduce 选项即可进行切换。
第三步:建立与 Hadoop 集群的连接,点击 Eclipse软件右下角的 Map/Reduce Locations 面板,在面板中单击右键,选择 New Hadoop Location。
在弹出来的 General 选项面板中进行 Master 的设置,设置要要 Hadoop 的配置一致,如我使用的Hadoop伪分布式配置,设置了 fs.defaultFS 为 hdfs://localhost:9000,则 DFS Master 那的 Post 也应改为 9000。
Location Name 随意填写,Map/Reduce Master 的 Host 就填写你本机的IP(localhost 也行),Port 默认就是 50020。最后的设置如下:
接着再切换到 Advanced parameters 选项面板,这边有详细的配置,切记需要与 Hadoop 的配置(/usr/local/hadoop/etc/hadoop中的配置文件)一致,如我配置了 hadoop.tmp.dir ,就要进行修改。
最后点击 finish,Map/Reduce Location 就创建好了。
这样配置就完成了。
在 Eclipse 中查看HDFS中的文件内容
配置成功后,点击左侧 Project Explorer中的 MapReduce Location 就能直接查看 HDFS 中的文件内容了(如下图是 WordCount 的输出结果),而无需再通过繁琐的 hdfs dfs -ls
命令。如果无法查看,可尝试重启Eclipse!
HDFS 中的内容变动后,Eclipse 不会同步刷新,需要右键点击 Project Explorer中的 MapReduce Location,选择 Refresh,才能看到变动后的文件。
在Eclipse中创建MapReduce项目
点击 File 菜单,选择 New -> Project…:
选择 Map/Reduce Project,点击 Next。
填写 Project name 为 WordCount 即可,点击 Finish 就创建好了项目。
此时在左侧的 Project Explorer 就能看到刚才建立的项目了。
接着右键点击刚创建的 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.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 static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
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> <out>");
System.exit(2);
}
Job job = new Job(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
通过Eclipse运行MapReduce
在运行 MapReduce 程序前,还需要执行一项操作:将 /usr/local/hadoop/etc/hadoop 中将有修改过的配置文件(如伪分布式需要 core-site.xml 和 hdfs-site.xml),以及 log4j.properties 复制到 WordCount 项目下的 src 文件夹(~/workspace/WordCount/src)中。否则程序将无法正确运行,文章最后解释了为什么需要复制这些文件。
复制完成后,右键点击 WordCount 进行刷新,可以看到文件结构如下所示:
点击工具栏中的 Run 图标,或者右键点击 Project Explorer 中的 WordCount.java,选择 Run As -> Run on Hadoop,就可以运行 MapReduce 程序了。不过由于没有指定参数,运行时会提示 “Usage: wordcount “,需要通过Eclipse设定一下运行参数。
右键点击刚创建的 WordCount.java,选择 Run As -> Run Configurations,在此处可以设置运行时的相关参数。切换到 “Arguments” 栏,在 Program arguments 处填写 “input output” 就可以了。
或者也可以直接在代码中设置好输入参数。可将代码 main() 函数的 String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
改为:
// String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
String[] otherArgs=new String[]{"input","output"}; /* 直接设置输入参数 */
设定参数后,再次运行程序,可以看到运行成功的提示,刷新 DFS Location 后也能看到输出的 output 文件夹。
至此,你就可以使用 Eclipse 方便的进行 MapReduce程序的开发了。
在 Eclipse 中运行 MapReduce 项目会遇到的问题
虽然配置了 Hadoop-Eclipse-Plugin,但一些设置项似乎没有正确配置,如果没有复制 core-site.xml 和 hdfs-site.xml ,程序将无法运行,会提示 Input 路径不存在(实际读取的是当前目录而非 HDFS 目录)。
Exception in thread "main" org.apache.hadoop.mapreduce.lib.input.InvalidInputException: Input path does not exist: file:/home/hadoop/workspace/WordCountProject/input
log4j用于记录程序的输出日记,需要 log4j.properties 这个配置文件,如果没有复制该文件到项目中,运行程序后在 Console 面板中会出现警告提示:
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
虽然不影响程序的正确运行的,但程序运行时无法看到任何提示消息(只能看到出错信息)。
- Eclipse编译运行MapReduce程序 Hadoop
- Hadoop+eclipse运行MapReduce程序
- Hadoop系列-使用Eclipse编译运行MapReduce程序 (三)
- 使用Eclipse编译运行MapReduce程序
- 使用Eclipse编译运行MapReduce程序
- 使用Eclipse编译运行MapReduce程序
- 编译制作hadoop 2.4.1 Eclipse插件,在线调试运行MapReduce程序
- Ubuntu系统下的Hadoop集群(4)_使用Eclipse编译运行MapReduce程序
- Hadoop — 使用Eclipse编译运行MapReduce程序(Hadoop2.6.0)
- Hadoop MapReduce示例程序WordCount.java手动编译运行解析
- Hadoop MapReduce示例程序WordCount.java手动编译运行解析
- Eclipse下运行hadoop自带的mapreduce程序--wordcount
- Eclipse打包mapreduce程序并提交至hadoop集群运行
- Hadoop学习笔记2:eclipse运行Mapreduce程序问题总结
- 在eclipse将mapreduce程序运行在hadoop集群中
- 使用Eclipse编译运行MapReduce程序 Hadoop2.6.0/Ubuntu
- 【图文详细】使用Eclipse编译运行MapReduce程序_Hadoop_2.4.1
- 使用Eclipse编译运行MapReduce程序 Hadoop2.7.1/Ubuntu
- 在TCP端口筛选只允
- IO流
- 黑马程序员---面向对象中的内部类
- 【JavaScript】——剖析面向对象与原型(一)
- 大端模式和小端模式
- Eclipse编译运行MapReduce程序 Hadoop
- 苹果商店appstore下载应用时,总是重复验证付款信息,如何解决?
- bzoj-1026 windy数
- 超时统计shell
- hdu 1856 More is better(并查集)
- Linux fcntl函数详解
- JavaScript 表单处理
- ContentResolver query 参数详解
- LeetCode #17 Letter Combinations of a Phone Number