我的Hadoop之路——Win7 Eclipse开发环境搭建

来源:互联网 发布:热阻计算软件 编辑:程序博客网 时间:2024/06/14 04:49

前言:完成了在linux平台下Hadoop集群的搭建后,困扰大家的问题便从如何搭建Hadoop变成了 如何使用Hadoop,所以,在这里,我将会一步步亲手尝试在Win7系统中Eclipse开发环境的搭建,为以后的MapReduce编程建立一个良好的开端。

Part1 Eclipse及JDK安装

其实大家的电脑里想必都有Eclipse和JDK了吧,这里我就不再浪费口舌了,如果真的是从头做起的,可以百度一下Eclipse以及JAVA环境的配置。这里要注意的就是,JDK的版本最好和我们在linux系统中的版本保持一致,也就是1.6.30的版本。

然后,很重要的一点,就是我们需要修改我们的管理员名字,需要右键我的电脑(计算机),点击管理,然后选择“本地用户和组”,打开用户,将Administrator改为hadoop,然后注销或者重启电脑。

很重要的第二点,我们需要到C:\Windows\System32\drivers\etc\路径中,修改hosts文件,把我们先前在linux下配置过的hosts内容,也就是192.168.47.128    node1这样的ip与机器名的组合添加进去,不然在我们的主机上是无法识别这些linux下的机器名的。

Part2 Eclipse插件配置及使用

Hadoop对于Eclipse有着良好的支持,我们可以从网上下载到对应Hadoop版本的插件,由于我们的Hadoop版本是1.2.1,所以就下载了hadoop-eclipse-plugin-1.2.1.jar,将其放到eclipse/plugins目录下即可。然后,我们需要打开我们的Eclipse,为了方便起见,我们可以新建一个workspace,具体方法为File->Switch Workspace->other,然后Eclipse会自动重启。

Eclipse重启后,我们需要做两个操作。第一步,我们需要打开Window->Preferences菜单,选中Hadoop Map/Reduce选项,将我们的Hadoop的路径添加进去,Hadoop文件与我们在linux中安装的Hadoop的是相同的,只要解压出来就好。{a}

第二步,我们需要切换Map/Reduce的工作目录,同样在Window下,选择Open Perspective->other,然后选中Map/Reduce即可。

最后,我们需要建立与Hadoop集群的连接,在Eclipse软件下面的"Map/Reduce Locations"进行右击,弹出一个选项,选择"New Hadoop Location"。在弹出的新窗口中,我们需要如下填写。{a}

{b}

这里填的就是我们的Master机的ip地址,以及当时在mapred-site.xml、core-site.xml中配置的端口,随后,我们还需要在Advanced parameters中找到hadoop.tmp.dir条目,将其内容填为我们在core-site.xml中所填的配置。

这些工作都完成后,我们可以在左侧的文件栏中查看整个DFS的文件系统,并且能够进行新建和删除文件的操作了。

Part3 使用Eclipse运行Example程序

首先,新建项目,File->New->Others中选择Map/Reduce Project,起个名字,可以叫做WordCountProject,然后finish。我们可以看到左侧的项目栏中多了我们新建的项目,其中包含了大量的引用包,然后右键项目名,New->Class,我们需要把Package填为package org.apache.hadoop.examples,因为我们要运行的示例程序是在这个包路径下的,然后程序名叫WordCount,finish。

然后,我们在windows中打开hadoop-1.2.1的文件夹,在hadoop-1.2.1\src\examples\org\apache\hadoop\examples路径下,打开我们的示例程序WordCount.java的代码,将代码复制到我们自己的WordCount.java中,但是,这里的代码是需要做一些修改的,大家可以看下面的代码。

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();    conf.set("mapred.job.tracker", "192.168.47.128:49001");    String[] ars=new String[]{"input","output2"};    String[] otherArgs = new GenericOptionsParser(conf, ars).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);  }}
三行红色的代码是做了变更的地方,“conf.set("mapred.job.tracker", "192.168.47.128:49001");”这句代码是为了对刚才Map/Reduce Location的配置进行补充,原因是刚才的配置文件中的这一配置并没有起作用,如果不加这句话,操作将会在本地磁盘中进行,这样显然是不行的。而后两行代码则是为了做到对命令行输入参数的模拟,因为我们是用eclipse执行,无法将输入、输出的位置作为参数传给main函数,只能在这里手动设置一下了。

最后,右键点击WordCount.java,选择RunAs->RunonHadoop,然后选择我们的location即可。


原创粉丝点击