Ubuntu系统下使用Eclipse搭建hadoop2.4执行环境

     使用hadoop进行MapReduce编程的时候。我们都希望使用IDE进行开发。本文主要介绍怎样使用Eclipse进行hadoop编程。

假设你的集群还没搭好,能够參考我的前一篇文章Ubuntu下用hadoop2.4搭建集群(伪分布式)

 一、安装Eclipse

 方法一:直接在Ubuntu的软件中心进行下载,例如以下图所看到的。


 方法二:先下载Eclispe压缩文件后,使用命令进行安装。下载地址:http://pan.baidu.com/s/1mgiHFok

      sudo tar -zxvf eclipse-dsl-juno-SR1-linux-gtk.tar.gz

这样Eclipse就安装好了。


二、安装 Hadoop-Eclipse-Plugin

   下载 hadoop2x-eclipse-plugin ,将 release 中的 hadoop-eclipse-kepler-plugin-2.2.0.jar (尽管标注的是 2.2.0。但在 2.4.1 下是没问题的。应该在 2.x 版本号下都能够,只是有时候会提示有些东西过期,对于学习者来说。我个人认为临时能够不考虑这些细节)拷贝到 Eclipse 安装文件夹的 plugin 文件夹中。执行./eclipse -clean 重新启动 Eclipse 就可以。用法一的Eclipse 的默认安装文件夹为:/usr/lib/eclipse :用法二的安装Eclipse的话文件夹依据自己而定了。我的文件夹是/usr/local/eclipse。

./eclipse -clean命令须要在Eclipse的安装文件夹下。打开后就可一看到文件系统了。


插件须要进一步的配置。

第一步:选择 Window 菜单下的 Preference ,然后弹出一个窗口,窗口的左側会多出 Hadoop Map/Reduce 选项,点击此选项,选择 Hadoop 的安装文件夹(如/usr/local/hadoop)。


第二步:切换 Map/Reduce 工作文件夹,选择 Window 菜单下选择 Open Perspective,弹出一个窗口,从中选择 Map/Reduce 选项就可以进行切换。


第三步 点击Map/Reduce Location选项卡,点击右边小象图标,打开Hadoop Location配置窗体:


    输入Location Name。随意名称就可以.配置Map/Reduce Master和DFS Mastrer,Host和Port配置成与core-site.xml的设置一致就可以。



点击"Finish"button。关闭窗体。

 点击左側的DFSLocations—>MapReduceProject(上一步配置的location name),如能看到user,表示成功安装

   假设出现这个提示这个错误Error:call from mylinux/127.0.1.1 to localhost:9090 failed on connection exception java.Connection.net.ConnectException拒绝连接。

 

首先确定hadoop有没有启动

我当时也是因为没有启动hadoop,然后折腾了好久才发现了这个问题,希望对大家有所帮助。

详细一些原因能够參考我的一篇博客在Ubuntu下使用Eclispe连接hadoop时拒绝链接解决方式总结

三、新建WordCount样例

    File—>Project,选择Map/Reduce Project,输入项目名称WordCount等。

    在WordCount项目里新建class,名称为WordCount,代码例如以下:

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、在HDFS上创建文件夹input

    hadoop fs -mkdir input  

这是使用命令来创建。我们能够在Eclipse里面右键hadoop(依据个人配置不同这个会有出入)进行创建


    2、拷贝本地README.txt到HDFS的input里

 hadoop fs -copyFromLocal /usr/local/hadoop/README.txt input

     相同我们能够右键input,然后选择Upload file  ,使用可视化的形式进行文件上传。

    3、点击WordCount.java,右键。点击Run As—>Run Configurations,配置执行參数,即输入和输出目录

当然我们也能够在代码里直接写路径,真正搞懂文件系统你会发现方法还有非常多,仅仅是须要改动java代码。

以下这个配置是相应了代码里面这个

 String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();。

 hdfs://localhost:9000/user/hadoop/input hdfs://localhost:9000/user/hadoop/output

    

   4、执行完毕后。查看执行结果        

       第一种方法就是在终端里面直接使用命令行进行查看。

  hadoop fs -ls output

        能够看到有两个输出结果。_SUCCESS和part-r-00000

        运行

hadoop fs -cat output/*

      另外一种方法就是直接在Eclipse里面查看。首先记得刷新一下文件系统~

      展开DFS Locations。例如以下图所看到的。双击打开part-r00000查看结果




转载于:https://www.cnblogs.com/ljbguanli/p/7090494.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值