码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-04-16 20:08:22      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:hadoop eclipse

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

技术分享

4、在win7上解压hadoop-2.6.0。

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

      要求支持hadoop2.6版本的(低版本的hadoop.dll会报错),让后拷贝下载文件到hadoop的bin目录,如果有已存在的文件直接跳过就行,不用覆盖原来的bin目录下的文件

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

技术分享


7、显示Map/Reduce选项卡。

选择Window->Open Perspective->Other->Map/Reduce

技术分享

    

8、创建hdfs连接。

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


技术分享

9、弹出配置

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

技术分享


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

技术分享


11、创建mapreduce程序

技术分享


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


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

技术分享一个为输入目录 一个为输出目录 (输出目录不能存在)


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

18、完成!

本文出自 “凤凰涅槃” 博客,请务必保留此出处http://yntmdr.blog.51cto.com/3829621/1633528

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

标签:hadoop eclipse

原文地址:http://yntmdr.blog.51cto.com/3829621/1633528

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!