专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Eclipse

Hadoop学习笔记(四)-Eclipse下搭建Hadoop2.6.4开发环境

发布时间:2010-06-05 11:57:47 文章来源:www.iduyao.cn 采编人员:星星草
Hadoop学习笔记(4)-Eclipse下搭建Hadoop2.6.4开发环境

0.前言

本文参考博客:http://www.51itong.net/eclipse-hadoop2-7-0-12448.html
搭建开发环境前保障已经搭建好hadoop的伪分布式。可参考上个博客:
http://blog.csdn.net/xummgg/article/details/51173072

1.下载安装eclipse

下载网址:http://www.eclipse.org/downloads/
这里写图片描述
因为运行在ubuntu下,所以下载linux 64为的版本(支持javaEE),下载后默认放在当前用户的Downloads。
解压,命令如下:
这里写图片描述
解压后可以在/usr/local下看到:
这里写图片描述
因为,要加入新jar包进入eclipse,所以把ecplise文件夹权限,设置高权限。
这里写图片描述

2.下载hadoop插件

2.6.4插件hadoop-eclipse-plugin-2.6.4.jar 下载地址:
http://download.csdn.net/download/tondayong1981/9437360
下载完成后,把插件放到eclipse/plugins目录下
这里写图片描述
用sudo要输入用户密码。

3.设置eclipse

运行eclipse
这里写图片描述

打开window->preferences
这里写图片描述
可以看到多了个Hadoop Map/Reduce,设置本机的hadoop目录,我的目录时/usr/local/hadoop/hadoop-2.6.4/ ,如下图所示:
这里写图片描述

4.配置Map/Reduce Locations

注意:配置前先在后台运行起hadoop,即开启hadoop伪分布式的dfs和yarn,参考上一个博客。

Eclipse中打开Windows—Open Perspective—Other
这里写图片描述

选择Map/Reduce,点击OK
这里写图片描述

在右下方看到如下图所示
这里写图片描述
点击Map/Reduce Location选项卡,点击右边蓝色小象图标,打开Hadoop Location配置窗口。

 输入Location Name,任意名称即可.配置Map/Reduce Master和DFS Mastrer,Host和Port配置成与core-site.xml的设置一致即可。如下图:

这里写图片描述
点击”Finish”按钮,关闭窗口。

点击左侧的DFSLocations—>myhadoop(上一步配置的location name),如能看到user,表示安装成功。这样eclipse就连接上了分布式文件系统,可以在eclipse里做查看,方便编程。
这里写图片描述

5.新建WordCount项目

点击File—>Project:

这里写图片描述

选择Map/Reduce Project,点next进入下一步:
这里写图片描述

输入项目名称WordCount,点finish完成:
这里写图片描述

在WordCount项目里右键src新建class,包名com.xxm(请自行命明),类名为WordCount:
这里写图片描述

代码如下:

package com.xxm;//改为自己的包明

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);
  }
}

6.运行

运行前保证分布式文件系统里的input目录下有文件,如果是HDFS刚格式化过,也请参考《搭建Hadoop伪分布式》教程,创建和上传input文件以及里面的内容。

在WordCount的代码区域,右键,点击Run As—>Run Configurations,配置运行参数,即输入和输出文件夹地址参数:
 hdfs://localhost:9000/user/xxm/input hdfs://localhost:9000/user/xxm/output/wordcount3
如下图所示:
这里写图片描述
点击Run。
结果可以重连接myhadoop后进入output双击查看。也可以用HDFS命令get下来看。重连接myhadoop方法:在项目管理窗口,右键蓝色小象,选reconnect。
这里写图片描述
到此hadoop的eclipse开发环境搭建完成。

7.关联hadoop源码

还是在hadoop的下载网站下载源码:
http://hadoop.apache.org/releases.html
这里写图片描述

下载后解压到/usr/hadoop文件夹里:
这里写图片描述

如下图所示,选择了一个hadoop的InWritable函数,右击查看源程序:
这里写图片描述
结果源程序不能找到,所以要做源程序的关联。点击Attach Source:
这里写图片描述

将hadoop-2.6.4-src源码关联,点击ok:
这里写图片描述
跳转后可以看到InWritable函数已经可以看到源码了:
这里写图片描述
到此源码关联成功。


XianMing

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: