天天看点

win7下安装hadoop 2.6.0 的eclipse插件并编写运行WordCount程序

win7 64位下安装hadoop的eclipse插件并编写运行WordCount程序

环境:

win7 64位

hadoop-2.6.0

步骤:

1、下载hadoop-eclipse-plugin-2.6.0.jar包

2、把hadoop-eclipse-plugin-2.6.0.jar放到eclipse安装目录下的plugins目录下

3、打开eclipse发现左边多出来一个DFS Locations

<a href="http://s3.51cto.com/wyfs02/M02/6B/86/wKiom1UveVKBuFO3AAC8GNRvicg978.jpg" target="_blank"></a>

4、在win7上解压hadoop-2.6.0。

5、下载hadoop.dll、winutils.exe等文件。

   根据你的hadoop版本下载相应的文件,我们用的是2.6所以要求支持hadoop2.6的(低版本的hadoop.dll会报错),然后拷贝下载文件到hadoop的bin目录,如果有已存在的文件直接跳过就行,不用覆盖原来的bin目录下的文件

说明:这一步非常重要,不然你运行项目时会报各种异常

6、设置Window-&gt;Prefrences-&gt;Hadoop Map/Reduce的installation directory为你解压的hadoop目录。

<a href="http://s3.51cto.com/wyfs02/M00/6B/86/wKiom1UvefShRuWrAAEgA2bQv1k850.jpg" target="_blank"></a>

7、显示Map/Reduce选项卡。

选择Window-&gt;Open Perspective-&gt;Other-&gt;Map/Reduce

<a href="http://s3.51cto.com/wyfs02/M00/6B/86/wKiom1UvfUKwuXCZAAKmm2vOMzo531.jpg" target="_blank"></a>

8、创建hdfs连接。

右键单击Map/Reduce Locations选项卡 选择新建

<a href="http://s3.51cto.com/wyfs02/M02/6B/86/wKiom1UvfbqTBAqbAACyeTyLLm8352.jpg" target="_blank"></a>

9、弹出配置

这里面的Host、Port分别为你在mapred-site.xml、core-site.xml中配置的地址及端口

<a href="http://s3.51cto.com/wyfs02/M00/6B/86/wKiom1Uvfm2jWEnnAAHZwW6lU9I956.jpg" target="_blank"></a>

10、查看是否连接成功,是否可以看到hdfs上的文件

<a href="http://s3.51cto.com/wyfs02/M00/6B/82/wKioL1UvgNvhf_NPAABE-kMjnQs954.jpg" target="_blank"></a>

11、创建mapreduce程序

<a href="http://s3.51cto.com/wyfs02/M00/6B/86/wKiom1Uvf_jzPRLrAAE8XkQ518Q778.jpg" target="_blank"></a>

12、下一步 填写项目名称OK。

13、如果自动导入了hadoop的jar包建议全部删除(因为hadoop-2.6.0不同目录下会有好多相同的jar包)都导进去就有重复的了。

14、搜索hadoop-2.6.0目录下所有的jar包拷贝到自己新建的一个目录,有重复的略过,然后把所有的jar包导入项目中。

15、编写wordcount程序新建一个class,内容如下:

package hdWordCount;

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&lt;Object, Text, Text, IntWritable&gt; {

  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&lt;Text, IntWritable, Text, IntWritable&gt; {

  private IntWritable result = new IntWritable();

  public void reduce(Text key, Iterable&lt;IntWritable&gt; 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 &lt;in&gt; &lt;out&gt;");

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

}

16、右键run as 选择配置 ,配置参数

17、右键run as ,run on hadoop 完成 ,查看 输出目录文件内容

18、完成!

本文转自 yntmdr 51CTO博客,原文链接:http://blog.51cto.com/yntmdr/1633528,如需转载请自行联系原作者

下一篇: route/ip命令

继续阅读