VMWare虚拟机安装CentOS 7 Linux及Hadoop与Eclipse学习环境(3-Eclipse开发环境)

来源:互联网 发布:单片机开发板怎么做得 编辑:程序博客网 时间:2024/05/21 11:25

4. 安装Eclipse

4.1. 安装Java IDE版Eclipse

  由于Java环境是JDK1.7版本,则Eclipse不能使用较新版本,按同事推荐使用Eclipse 4.4 版本,下载地址为:eclipse-java-luna-SR2-linux-gtk-x86_64.tar.gz

  要把eclipse安装到hadoop用户下,因为以后的开发也都是在hadoop用户下进行,否则会有一些权限的问题导致后面无法继续。

  拷贝Eclipse压缩包到/home/hadoop下,并解压(网上资料一般是解压到 /opt 目录下)。

[hadoop@hadoop_master ~]$  cp /mnt/hgfs/dev/eclipse-java-luna-SR2-linux-gtk-x86_64.tar.gz eclipse-java-luna.tar.gz[hadoop@hadoop_master ~]$  tar -zvxf eclipse-java-luna.tar.gz

  符号链接的用法 ln -s 源文件 目标文件 ,-s 是符号的意思(symbolic)软连接,命令的意思是,在/usr/bin/eclipse 目录下创建一个同步连接,而源文件在/home/hadoop/eclipse/eclipse目录下

[hadoop@hadoop_master ~]$ su root[hadoop@hadoop_master ~]$ cd eclipse[root@hadoop_master eclipse]# ln -s /home/hadoop/eclipse/eclipse /usr/bin/eclipse[root@hadoop_master eclipse]# vim /usr/share/applications/eclipse.desktop

创建一个 Gnome 启动(桌面应用程序)

[Desktop Entry]Encoding=UTF-8Name=Eclipse 4.4.2Comment=Eclipse LunaExec=/usr/bin/eclipseIcon=/home/hadoop/eclipse/icon.xpmCategories=Application;Development;Java;IDEVersion=1.0Type=ApplicationTerminal=0

  检查 app 是否已经被添加,在菜单“应用程序”->“编程”下,将出现Eclipse 4.4.2。
这里写图片描述
  设置Eclipse的Workspace。
这里写图片描述

4.2. 安装Eclipse hadoop插件

[hadoop@hadoop_master ~]$ cd eclipse/plugins[hadoop@hadoop_master plugins]$ cp /mnt/hgfs/dev/hadoop-eclipse-plugin-2.7.3.jar hadoop-eclipse-plugin-2.7.3.jar

  关于参数配置和问题调整:
  启动Eclipse,进入菜单 Window->Peferences,找到Hadoop插件,按Browse按钮,设置当前主机上的Hadoop安装目录(/home/hadoop/hadoop-2.7.3
)。
这里写图片描述

  进入菜单 Window ->Open Perspective->Others,选择Map/Reduce
这里写图片描述
这里写图片描述
  主窗口会出现插件视图,在Location区域单击右键,选择 New Hadoop Location。
这里写图片描述

  Hadoop Location配置窗口,location name随便起一个有意义的名字就可以,这里为myhadoop。

  Map/Reduce Mater的Host/Port有两种情况:
  (1)如果是以传统的JT/TT模式运行M/R任务,这个值要根据mapred_site.xml 当中的 mapreduce.jobtracker.address 参数设定
  (2)如果是以YARN资源管理模式运行M/R任务,这个值要根据 yarn_site.xml 当中的 yarn.resourcemanager.scheduler.address 参数设定

  在本例中:host为localhost,port为50070。

这里写图片描述

  DFS Master的Host/Port值是要根据 core_site.xml 当中的 fs.defaultFS 参数设定。

  上面参数设定完成后,就可以在图形界面上,看到HDFS系统的目录树和文件了。

  启动hadoop:

  用sbin/start-dfs.sh来启动hadoop,就可以在eclipse中连接和查看hadoop了。

这里写图片描述

5. 编写WordCount例子

  在hadoop包中的hadoop-2.7.3->share->hadoop->mapreduce中的hadoop-mapreduce-examples 2.7.3.jar(源码在hadoop-mapreduce-examples-2.7.3-sources.jar)包中。

package org.apache.hadoop.examples;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> [<in>...] <out>");      System.exit(2);    }    Job job = Job.getInstance(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);    for (int i = 0; i < otherArgs.length - 1; ++i) {      FileInputFormat.addInputPath(job, new Path(otherArgs[i]));    }    FileOutputFormat.setOutputPath(job,      new Path(otherArgs[otherArgs.length - 1]));    System.exit(job.waitForCompletion(true) ? 0 : 1);  }}

  点击 File 菜单,选择 New -> Project…:
这里写图片描述
  选择 Map/Reduce Project,点击 Next。
这里写图片描述
  填写 Project name 为 WordCount 即可,点击 Finish 就创建好了项目。
这里写图片描述
  此时在左侧的 Project Explorer 就能看到刚才建立的项目了。接着右键点击刚创建的 WordCount 项目,选择 New -> Class。

  需要填写两个地方:在 Package 处填写 org.apache.hadoop.examples;在 Name 处填写 WordCount。

  粘贴代码到Eclipse的代码编辑窗口中。

  在运行 MapReduce 程序前,还需要执行一项重要操作(也就是上面提到的通过复制配置文件解决参数设置问题):将/home/hadoop/hadoop-2.7.3/etc/hadoop 中将有修改过的配置文件(如伪分布式需要 core-site.xml 和 hdfs-site.xml),以及 log4j.properties 复制到 WordCount 项目下的 src 文件夹(~/workspace/WordCount/src)中:

[hadoop@hadoop_master examples]$ cp /home/hadoop/hadoop-2.7.3/etc/hadoop/core-site.xml core-site.xml[hadoop@hadoop_master examples]$ cp /home/hadoop/hadoop-2.7.3/etc/hadoop/hdfs-site.xml hdfs-site.xml[hadoop@hadoop_master examples]$ cp /home/hadoop/hadoop-2.7.3/etc/hadoop/log4j.properties log4j.properties 

  点击工具栏中的 Run 图标,或者右键点击 Project Explorer 中的 WordCount.java,选择 Run As -> Run on Hadoop,就可以运行 MapReduce 程序了。不过由于没有指定参数,运行时会提示 “Usage: wordcount “,需要通过Eclipse设定一下运行参数。

这里写图片描述

  右键点击刚创建的 WordCount.java,选择 Run As -> Run Configurations,在此处可以设置运行时的相关参数(如果 Java Application 下面没有 WordCount,那么需要先双击 Java Application)。切换到 “Arguments” 栏,在 Program arguments 处填写 “input output” 就可以了。
这里写图片描述

注:在运行时报错:Exception in thread “main” org.apache.hadoop.mapreduce.lib.input.InvalidInputException: Input path does not exist: file:/home/hadoop/workspace/WordCount/input。
解决办法是:手工在WordCount下建input目录。

参考:

[1].VMWare虚拟机安装CentOS 7 Linux及Hadoop与Eclipse学习环境(2-伪分布模式hadoop环境) 肖永威 2016.11

[2].使用Eclipse编译运行MapReduce程序 Hadoop2.6.0_Ubuntu/CentOS

0 0
原创粉丝点击