Ubuntu系统下使用eclipse搭建Hadoop2.7.1运行环境

来源:互联网 发布:剑灵2016天女捏脸数据 编辑:程序博客网 时间:2024/06/09 00:39

一、安装eclipse

从eclipse官网下载安装文件eclipse downloads,得到eclipse installer,可选择需要的版本安装到本地,搭建hadoop环境选择IDE for Java进行安装。

安装好eclipse后如果需要汉化,可以进入eclipse babel项目eclipse babel downloads根据页面指示从eclipse中在线下载语言包或者下载到本地解压到eclipse工作目录。

二、安装Hadoop-Eclipse-Plugin

下载hadoop-eclipse-plugin,将release中最新的jar包复制到eclipse安装目录下的dropins文件夹中,重启eclipse。

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

第二步:切换 Map/Reduce 工作目录,选择 Window 菜单下选择 Perspective,打开透视图,其他,弹出一个窗体,从中选择 Map/Reduce 选项即可进行切换。

第三步 点击Map/Reduce Location选项卡,点击右边小象图标,打开Hadoop Location配置窗口,输入Location Name,任意名称即可.配置Map/Reduce Master和DFS Mastrer,Host和Port配置成与core-site.xml和mapred-site.xml的设置一致即可。

点击"Finish"按钮,关闭窗口。

点击左侧的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有没有启动,其他的原因参考其他作者的博客:在Ubuntu下使用Eclispe连接hadoop时拒绝链接解决方案总结


三、新建WordCount例子

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

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


/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements.  See the NOTICE file * distributed with this work for additional information * regarding copyright ownership.  The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */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);  }}

1、在HDFS上创建目录input:

hadoop fs -mkdir input

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

2、拷贝本地任一文本文件到HDFS的input里:

hadoop fs -copyFromLocal /usr/local/hadoop/README.txt input
同样我们可以右键input,然后选择Upload file  ,使用可视化的形式进行文件上传。

3、点击WordCount.java,右键,点击Run As—>Run Configurations,配置运行参数,参数为:hdfs://localhost:9000/user/hadoop/input hdfs://localhost:9000/user/hadoop/output,分别对应输入和输出,需要注意的是,每个人启动hadoop后的输入路径和输出路径均不相同,具体路径以eclipse右侧MapReduceProject中的文件夹路径为准。

当然我们也可以在代码里直接写路径,真正搞懂文件系统你会发现方法还有很多,只是需要修改java代码。

下面这个配置是对应了代码里面这个代码段:

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

4、运行完成后,查看运行结果。

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

hadoop fs -ls output

可以看到有两个输出结果,_SUCCESS和part-r-00000

执行

hadoop fs -cat output/*

第二种方法就是直接在Eclipse里面查看。首先记得刷新一下文件系统, 展开DFS Locations,双击打开part-r00000查看结果。 





0 0