一、安装Hadoop开发插件 java
hadoop安装包contrib/目录下有个插件hadoop-0.20.2-eclipse-plugin.jar,拷贝到myeclipse根目录下/dropins目录下。 node
二、 启动myeclipse,打开Perspective: apache
【Window】->【Open Perspective】->【Other...】->【Map/Reduce】->【OK】 app
三、 打开一个View: eclipse
【Window】->【Show View】->【Other...】->【MapReduce Tools】->【Map/Reduce Locations】->【OK】 ssh
四、 添加Hadoop location: 分布式
location name: 我填写的是:localhost.
Map/Reduce Master 这个框里
Host:就是jobtracker 所在的集群机器,这里写localhost
Hort:就是jobtracker 的port,这里写的是9999
这两个参数就是mapred-site.xml里面mapred.job.tracker里面的ip和port
DFS Master 这个框里
Host:就是namenode所在的集群机器,这里写localhost
Port:就是namenode的port,这里写8888
这两个参数就是core-site.xml里面fs.default.name里面的ip和port
(Use M/R master host,这个复选框若是选上,就默认和Map/Reduce Master这个框里的host同样,若是不选择,就能够本身定义输入,这里jobtracker 和namenode在一个机器上,所以是同样的,就勾选上) oop
user name:这个是链接hadoop的用户名,由于我是用lsq用户安装的hadoop,并且没创建其余的用户,因此就用lsq。下面的不用填写。
而后点击finish按钮,此时,这个视图中就有多了一条记录。 spa
重启myeclipse并从新编辑刚才创建的那个链接记录,如今咱们编辑advance parameters tab页 插件
(重启编辑advance parameters tab页缘由:在新建链接的时候,这个advance paramters tab页面的一些属性会显示不出来,显示不出来也就无法设置,因此必须重启一下eclipse再进来编辑才能看到)
这里大部分的属性都已经自动填写上了,其实就是把core-defaulte.xml、hdfs-defaulte.xml、mapred-defaulte.xml里面的一些配置属性展现出来。由于在安装hadoop的时候,其site系列配置文件里有改动,因此这里也要弄成同样的设置。主要关注的有如下属性:
fs.defualt.name:这个在General tab页已经设置了
mapred.job.tracker:这个在General tab页也设置了
dfs.replication:这个这里默认是3,由于我在hdfs-site.xml里面设置成了1,因此这里也要设置成1
hadoop.job.ugi:这里要填写:lsq,Tardis,逗号前面的是链接的hadoop的用户,逗号后面就写死Tardis(这个属性不知道我怎么没有...)
而后点击finish,而后就链接上了(先要启动sshd服务,启动hadoop进程),链接上的标志如图:
五、新建Map/Reduce Project:
【File】->【New】->【Project...】->【Map/Reduce】->【Map/Reduce Project】->【Project name: WordCount】->【Configure Hadoop install directory...】->【Hadoop installation directory: D:\cygwin\home\lsq\hadoop-0.20.2】->【Apply】->【OK】->【Next】->【Allow output folders for source folders】->【Finish】
六、新建WordCount类:
添加/编写源代码:
D:\cygwin\home\lsq\hadoop-0.20.2/src/examples/org/apache/hadoop/examples/WordCount.java
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);
}
}
七、上传模拟数据文件夹。
为了运行程序,须要一个输入的文件夹和输出的文件夹。输出文件夹,在程序运行完成后会自动生成。咱们须要给程序一个输入文件夹。
(1)、在当前目录(如hadoop安装目录)下新建文件夹input,并在文件夹下新建两个文件file一、file2,这两个文件内容分别以下:
file1
①在新建的项目WordCount,点击WordCount.java,右键-->Run As-->Run Configurations
②在弹出的Run Configurations对话框中,点Java Application,右键-->New,这时会新建一个application名为WordCount
③配置运行参数,点Arguments,在Program arguments中输入“你要传给程序的输入文件夹和你要求程序将计算结果保存的文件夹”,如:
(若是运行时报java.lang.OutOfMemoryError: Java heap space 配置VM arguments(在Program arguments下)
-Xms512m -Xmx1024m -XX:MaxPermSize=256m
八、点击Run,运行程序
点击Run,运行程序,过段时间将运行完成,等运行结束后,能够在终端中用命令以下,查看是否生成文件夹output:
bin/hadoop fs -ls
用下面命令查看生成的文件内容:
bin/hadoop fs -cat output/*
若是显示以下,说明已经成功在myeclipse下运行第一个MapReduce程序了。