hadoop 1.2.1 Eclipse mapreduce hello word 学习笔记(二)

             上一篇文章在http://blog.csdn.net/zwkwd/article/details/11740589 

在 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插件

获取插件

参考:http://f.dataguru.cn/thread-187770-1-1.html  可以自己生成 也可以直接下载使用

安装完重新打开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包导入

如图


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

打开http://hadoop.apache.org/docs/r1.2.1/mapred_tutorial.html

点击如图


按照例子 建立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<LongWritable, Text, Text, IntWritable> {
 	     private final static IntWritable one = new IntWritable(1);
	     private Text word = new Text();
 	
	     public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> 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<Text, IntWritable, Text, IntWritable> {
 	     public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> 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目录进行查看








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值