在 hadoop 1.2.1成功配置了为分布式环境,经过了十一长假,该继续学习了,

这次要在eclipse下运行一个hadoop 应用

 开发环境

操作系统:CentOS Linux release 6.0 (Final)

eclipse4.3

java version "1.7.0_25"

第一步 运行 start-all.sh 可以参照上一篇文章,启动守护线程

发现启动有问题,原来是ip地址冲突了而我的xml配置中设置的ip地址没有生效,没办法改一下ip

DEVICE="eth0"

BOOTPROTO=static
IPADDR=192.168.2.88
此处改成没有被占用的ip

/etc/rc.d/init.d/network restart 使修改生效

生效后修改vim core-site.xml

  vim mapred-site.xml   设置的ip (如果设置成 localhost 就不用改了)

配置eclipse插件

获取插件

参考:  可以自己生成 也可以直接下载使用

安装完重新打开eclipse后

在showview里面可以考到选项如果

选择让其显示在控制台旁边

右键新建一个

如图

master 处填写 mapred-site.xml ip和端口  dfs master 处填写 core-site.xml ip和端口

设置hadoop的安装路径 如图

设置完后可以看到 资源目录下如图

我们可以在这里通过右键对dfs文件进行操作 (增删 上传 下载)

创建helloword工程

File -> New -> Project 选择“Map/Reduce Project”,然后输入项目名称,创建项目。插件会自动把hadoop根目录和lib目录下的所有jar包导入

如图

第一个例子准备运行文档中的实例

打开

点击如图

按照例子 建立package 和 class 将代码复制

package org.myorg; import java.io.IOException;import java.util.*;import org.apache.hadoop.fs.Path;import org.apache.hadoop.conf.*;import org.apache.hadoop.io.*;import org.apache.hadoop.mapred.*;import org.apache.hadoop.util.*;public class WordCount {       public static class Map extends MapReduceBase implements Mapper
{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, OutputCollector
output, Reporter reporter) throws IOException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); output.collect(word, one); } } } public static class Reduce extends MapReduceBase implements Reducer
{ public void reduce(Text key, Iterator
values, OutputCollector
output, Reporter reporter) throws IOException { int sum = 0; while (values.hasNext()) { sum += values.next().get(); } output.collect(key, new IntWritable(sum)); } } public static void main(String[] args) throws Exception { JobConf conf = new JobConf(WordCount.class); conf.setJobName("wordcount"); conf.setOutputKeyClass(Text.class); conf.setOutputValueClass(IntWritable.class); conf.setMapperClass(Map.class); conf.setCombinerClass(Reduce.class); conf.setReducerClass(Reduce.class); conf.setInputFormat(TextInputFormat.class); conf.setOutputFormat(TextOutputFormat.class); FileInputFormat.setInputPaths(conf, new Path(args[0])); FileOutputFormat.setOutputPath(conf, new Path(args[1])); JobClient.runJob(conf); } }

直接运行会报错 报错了 (需要两个参数)  参考文档

需要传入 输入目录 和 输出目录

可以根据根据DFS 中的目录 进行设置  也可以直接写 绝对目录 如图

点击运行成功

通过

hadoop dfs -cat /home/hadoop-1.2.1/output/part-00000 可以查看输出  也可以在eclipse中dfs目录进行查看