Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9332975
  • 博文数量: 1669
  • 博客积分: 16831
  • 博客等级: 上将
  • 技术积分: 12594
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-25 07:23
个人简介

柔中带刚,刚中带柔,淫荡中富含柔和,刚猛中荡漾风骚,无坚不摧,无孔不入!

文章分类

全部博文(1669)

文章存档

2023年(4)

2022年(1)

2021年(10)

2020年(24)

2019年(4)

2018年(19)

2017年(66)

2016年(60)

2015年(49)

2014年(201)

2013年(221)

2012年(638)

2011年(372)

分类: 架构设计与优化

2013-11-25 13:26:25

 

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

分类: hadoop2013-10-23 19:48 62人阅读 评论(0) 收藏 举报

             上一篇文章在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插件

获取插件

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

安装完重新打开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 将代码复制

[java] view plaincopy
  1. package org.myorg;    
  2. import java.io.IOException;  
  3. import java.util.*;  
  4.   
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.conf.*;  
  7. import org.apache.hadoop.io.*;  
  8. import org.apache.hadoop.mapred.*;  
  9. import org.apache.hadoop.util.*;  
  10. public class WordCount {  
  11.        public static class Map extends MapReduceBase implements Mapper {  
  12.          private final static IntWritable one = new IntWritable(1);  
  13.          private Text word = new Text();  
  14.       
  15.          public void map(LongWritable key, Text value, OutputCollector output, Reporter reporter) throws IOException {  
  16.            String line = value.toString();  
  17.            StringTokenizer tokenizer = new StringTokenizer(line);  
  18.            while (tokenizer.hasMoreTokens()) {  
  19.              word.set(tokenizer.nextToken());  
  20.              output.collect(word, one);  
  21.            }  
  22.          }  
  23.        }  
  24.       
  25.        public static class Reduce extends MapReduceBase implements Reducer {  
  26.          public void reduce(Text key, Iterator values, OutputCollector output, Reporter reporter) throws IOException {  
  27.            int sum = 0;  
  28.            while (values.hasNext()) {  
  29.              sum += values.next().get();  
  30.            }  
  31.            output.collect(key, new IntWritable(sum));  
  32.          }  
  33.        }      
  34.        public static void main(String[] args) throws Exception {  
  35.          JobConf conf = new JobConf(WordCount.class);  
  36.          conf.setJobName("wordcount");  
  37.       
  38.          conf.setOutputKeyClass(Text.class);  
  39.          conf.setOutputValueClass(IntWritable.class);  
  40.       
  41.          conf.setMapperClass(Map.class);  
  42.          conf.setCombinerClass(Reduce.class);  
  43.          conf.setReducerClass(Reduce.class);  
  44.       
  45.          conf.setInputFormat(TextInputFormat.class);  
  46.          conf.setOutputFormat(TextOutputFormat.class);  
  47.       
  48.          FileInputFormat.setInputPaths(conf, new Path(args[0]));  
  49.          FileOutputFormat.setOutputPath(conf, new Path(args[1]));  
  50.       
  51.          JobClient.runJob(conf);  
  52.        }  
  53.     }  
  54.       

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



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

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


点击运行成功

通过

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





阅读(784) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~