eclipse下安装配置hadoop(含WordCount程序测试),hadoopwordcount


这里我为大家介绍如何在windows下安装配置hadoop.,以及测试一个程序

所需要使用的插件和分别有:

一、准备工作


1、eclipse,最好是JAVAEE版本的,以为可以改变模式。

2、hadoop和eclipse的连接器:

hadoop-eclipse-plugin-1.2.1.jar(这个是我所使用的,在这里可以自定义选取版本)

3、是hadoop源码包(下载最新的就可以)。


将hadoop-0.20.2-eclipse-plugin.jar 复制到eclipse/plugins目录下,重启eclipse。


二.打开MapReduce视图

Window -> Open Perspective -> Other 选择Map/Reduce,图标是个蓝色的象。








点击后整个界面换到了MapReduce模式。

三.添加一个MapReduce环境

在eclipse下端,控制台旁边会多一个Tab,叫“Map/Reduce Locations”,在下面空白的地方点右键,选择“New Hadoop location...”,如图所示:





在弹出的对话框中填写如下内容:

Location name(取个名字)
Map/Reduce Master(Job Tracker的IP和端口,根据mapred-site.xml中配置的mapred.job.tracker来填写)
DFS Master(Name Node的IP和端口,根据core-site.xml中配置的fs.default.name来填写)





四.使用eclipse对HDFS内容进行修改

经过上一步骤,左侧“Project Explorer”中应该会出现配置好的HDFS,点击右键,可以进行新建文件夹、删除文件夹、上传文件、下载文件、删除文件等操作。

注意:每一次操作完在eclipse中不能马上显示变化,必须得刷新一下。 


五.创建MapReduce工程

5.1配置Hadoop路径

Window -> Preferences 选择 “Hadoop Map/Reduce”,点击“Browse...”选择Hadoop文件夹的路径。
这个步骤与运行环境无关,只是在新建工程的时候能将hadoop根目录和lib目录下的所有jar包自动导入。

5.2创建工程

File -> New -> Project 选择“Map/Reduce Project”,然后输入项目名称,创建项目。插件会自动把hadoop根目录和lib目录下的所有jar包导入。

5.3创建Mapper或者Reducer

File -> New -> Mapper 创建Mapper,自动继承mapred包里面的MapReduceBase并实现Mapper接口。
注意:这个插件自动继承的是mapred包里旧版的类和接口,新版的Mapper得自己写。

Reducer同理。

六.在eclipse中运行WordCount程序

6.1导入WordCount



WordCount
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.LongWritable;
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;

public class WordCount {
    public static class TokenizerMapper extends Mapper<LongWritable, Text, Text, IntWritable>{

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        public void map(LongWritable 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();
        if (args.length != 2) {
            System.err.println("Usage: wordcount  ");
            System.exit(2);
        }

        Job job = new Job(conf, "word count");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setReducerClass(IntSumReducer.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        System.exit(job.waitForCompletion(true) ? 0 : 1);

    }

}


6.2配置运行参数

Run As -> Open Run Dialog... 选择WordCount程序,在Arguments中配置运行参数:/mapreduce/wordcount/input /mapreduce/wordcount/output/1

分别表示HDFS下的输入目录和输出目录,其中输入目录中有几个文本文件,输出目录必须不存在。

6.3运行

Run As -> Run on Hadoop 选择之前配置好的MapReduce运行环境,点击“Finish”运行。

控制台会输出相关的运行信息。



七、查看结果:


在输出目录/mapreduce/wordcount/output/1中,可以看见WordCount程序的输出文件,以及日志,对于Hadoop学习来说,学习去看日志是比较重要的一个能力。




相关内容

    暂无相关文章