(十二)使用Eclipse编译运行WordCount程序

因为eclipse有Hadoop-Eclipse-Plugin插件,这里介绍
Eclipse 上编译和运行 MapReduce 程序

一、安装配置Hadoop-Eclipse-Plugin

1、要在 Eclipse 上编译和运行 MapReduce 程序,需要安装 hadoop-eclipse-plugin。
     下载地址:可下载 Github 上的 hadoop2x-eclipse-plugin

2、将下载的hadoop-eclipse-plugin-2.6.5.jar文件放到eclipse的dropins目录下,重启eclipse打开Windows -> Preferences后,在窗口左侧会有Hadoop Map/Reduce选项,点击此选项,在窗口右侧设置Hadoop(已配置好的Hadoop)安装路径,如下图

 

 

  3、通过Window -> Open Perspective->Other菜单打开Map Reduce视图,在eclipse控制台出现Map/Reduce Locations

  4、在控制台右击图标,进行配置如图,其中Host配置需要连接的master的IP,若在本机Hosts文件中已配置master IP可直接写master

5、点击DFS Locations–>Hadoop如果能显示文件夹如图所示,说明配置正确

 

二、 Eclipse 中创建 MapReduce 项目

1、点击 File 菜单,选择 New -> Project->选择 Map/Reduce Project,点击 Next。

2、此时在左侧的 Project Explorer 就能看到刚才建立的项目了。

3、上传模拟数据文件夹,为了运行程序,我们需要一个输入的文件夹,和输出的文件夹。

通过hadoop shell命令进行创建输入文件夹 hadoop fs -mkdir -p /test/data,之后对文件夹进行权限修改,hadoop fs -chmod -R 777 /test,在本地创建一个t1.txt,写一些单词,右击eclipse DFS Locations进行文件上传,如图:

4、在windows上配置变量环境path

5、创建 Class 完成后,将代码复制进去

package demo;

import java.io.IOException;
import java.util.Iterator;
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 WordCount() {
    }
 
    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(WordCount.TokenizerMapper.class);
        job.setCombinerClass(WordCount.IntSumReducer.class);
        job.setReducerClass(WordCount.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);
    }
 
    public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        private IntWritable result = new IntWritable();
 
        public IntSumReducer() {
        }
 
        public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            int sum = 0;
 
            IntWritable val;
            for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get()) {
                val = (IntWritable)i$.next();
            }
 
            this.result.set(sum);
            context.write(key, this.result);
        }
    }
 
    public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
        private static final IntWritable one = new IntWritable(1);
        private Text word = new Text();
 
        public TokenizerMapper() {
        }
 
        public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
 
            while(itr.hasMoreTokens()) {
                this.word.set(itr.nextToken());
                context.write(this.word, one);
            }
 
        }
    }
}

6、在运行 MapReduce 程序前,还需要执行一项重要操作:将有修改过的配置文件(如 core-site.xml 、hdfs-site.xml、mapred-site.xml、yarn-site.xml以及 log4j.properties 复制到 WordCount 项目下的 src 文件夹中(有包复制在包中)

7、通过Eclipse设定一下运行参数。右键点击刚创建的 WordCount.java,选择 Run As -> Run Configurations,在此处可以设置运行时的相关参数。切换到 “Arguments” 栏,在 Program arguments 处填写 “input output” 就可以了。

8、运行,点击output下part-r-00000查看结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值