本文主要介绍如何在window7中使用eclipse开发hadoop项目。我们首先要做的工作是先搭建hadoop的伪分布或完全分布运行环境。关于hadoop的伪分布运行环境请参考:hadoop自学笔记1——hadoop2.6.5 ubuntu16.04单机和伪分布环境搭建教程。
搭建hadoop开发环境需要用到如下的软件:jdk1.7(64位),eclipse(LUNA),hadoop-eclipse-plugin-2.6.5.jar,hadoop-common-2.2.0-bin-master.zip,hadoop-2.6.5。相关的软件我都上传到了百度云,大家可以在文章末尾找到相关的链接进行下载。
1、设置hadoop jobTracker和HDFS端口。
这两个端口是在hadoop伪分布运行环境的mapred-site.xml和core-site.xml文件中设置,如下图所示,在mapred-site.xml文件中添加mapred.job.tracker的属性,并把值设置为:hadoop-only:9000。其中hadoop-only是hadoop伪分布运行环境的主机名,9000是端口。
在core-site.xml文件中设置HDFS端口,如下图所示:
2、配置开发hadoop的eclipse插件。
把下载后下来的hadoop-eclipse-plugin-2.6.5.jar文件拷贝到eclipse的plugins文件夹中。然后重启eclipse。点击Windows->Preferences,看到如下的界面表明eclipse插件安装成功。
3、配置hadoop主目录。
把下载的hadoop-2.6.5.tar解压后,然后在eclipse中选择hadoop的位置。点击Windows->Preferences->Hadoop Map/Reduce。如下图所示:
4、创建hadoop连接
打开Windows->Open Perspective中的Map/Reduce,在此perspective下进行hadoop程序开发。
打开Windows->Show View中的Map/Reduce Locations,如下图右键选择New Hadoop location…新建hadoop连接。
然后在打开的界面中输入在步骤1中配置的hadoop jobTracker和HDFS端口。Host可以是主机名也可以是IP地址。
填写完上面的端口和地址后,点击上图中的"Advanced parameters"按钮,在弹出的界面中找到
hadoop.tem.dir属性,把值设置为步骤1中core-site.xml文件中设置的hadoop.tem.dir属性的值。
完成上面的步骤后,点击左上角的project explorer,就会在DFS Locations下会展现hdfs集群中的文件。如下图所示:
如果显示文件夹(0),则表示该hdfs集群中没有文件夹,但是已经连接成功。
5、解压下载下来的hadoop-common-2.2.0-bin-master.zip文件,然后把bin目录下的winutils.exe文件拷贝到G:\hadoop\hadoop-2.6.5\bin目录中。
6、修改hadoop包中的NativeIO.java类
如果不修改该类,会出现 Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z错误。目前我还不知道这个错误怎么解决。因此只能通过修改NativeIO.java来解决。下载hadoop2.6.5的源码,文件名是hadoop-2.6.5-src.tar.gz并解压,然后在路径hadoop-2.6.5-src\hadoop-common-project\hadoop-common\src\main\java\org\apache\hadoop\io\nativeio中找到NativeIO.java类。在hadoop项目中,创建和NativeIO.java一样的包结构,然后把NativeIO.java拷贝到里面,接着修改第557行,改为return true。如下图所示:
到此hadoop2.6.5在win7中eclipse的开发环境已经搭建完毕。我们使用hadoop自带的wordcount例子来测试下。
7、测试hadoop开发环境
7.1、先上传一个文本文件到hdfs集群中,如下图所示:
7.2、然后编写一个WordCount的类,类代码如下:
package com;
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> [<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);
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);
}
}
7.3设置hadoop运行参数。
在hadoop项目中,找到上面写好的WordCount.java类,然后鼠标右键Run As -> Run Configurations…,在参数中填好输入输出目录,执行Run即可。本例子的运行参数设置如下:
hdfs://10.22.45.7:8020/user/hadoop/input hdfs://10.22.45.7:8020/user/hadoop/output1。
hdfs://10.22.45.7:8020/user/hadoop/input是hadoop项目要读取的文本路径。hdfs://10.22.45.7:8020/user/hadoop/output1是hadoop是hadoop项目分析文本文件后生成结果的路径。
如下图所示:
如果执行过程中没问题,则在hadoop项目的DFS Locations中看到一个output1的文件夹。
补充:请将hadoop-common-2.2.0-bin-master.zip解压出来的文件全部拷贝到hadoop的安装目中的bin文件夹中。在本例子中就是拷贝到G:\hadoop\hadoop-2.6.5\bin。特别是winutils.exe文件,绝对不能遗漏。
转载请注明:http://www.mobile-open.com/2017/981053.html