用Eclipse进行hadoop开发

安装之前下载所需软件:
eclipse:http://www.eclipse.org/downloads/
jre下载:http://www.java.com/en/download/manual.jsp
Hadoop2.7.3插件下载:http://download.csdn.net/detail/young_kim1/9334233

安装Eclipse
我自己安装,后面使用的是Mars那个版本,Neno那个版本对应的插件,没找到,2.7.3的插件放进去没反应,而且Neno这个版本很坑,安装靠运气,所以建议还是直接下载Mars这个版本,解压就可以直接使用了。

安装Hadoop2.7.3插件

1、将下载好的插件移动到eclipse安装目录下的plugins文件夹下。

2、重新启动eclispe,配置hadoop安装目录和hdfs端口。

如果插件安装成功,打开【Windows】—>【Preferences】后,在窗口左侧会有Hadoop Map/Reduce选项,点击此选项,在窗口右侧设置hadoop安装路径,然后点击【OK】。

这里写图片描述

打开【Windows】–>【Perspective】–>【Open perspective】–>【Other】,选择【Map/Reduce】,点击【OK】。就会出现以下内容窗口。

这里写图片描述

点击【Map/Reduce Location】选项卡,点击右边小象图标,打开Hadoop Location配置窗口:

这里写图片描述

这里的内容根据实际hadoop的配置填写:

Location name 可以定义一个好记的名字。
Host需要和hadoop中配置的信息一致,如果用了hostname,请把本机的hosts修改,指向对应ip。

DFS Master
是core-site.xml 中的内容

Map/Reduce Master
请查看日志文件中hadoop-root-namenode-XX.log里面的内容。查找到对应的
for DN 这行,取出来对应的端口信息。
这里写图片描述

这时选择完成即可,然后界面点击蓝色大象的箭头,会弹出异常信息。

这里写图片描述

可以不管,这个不影响后续开发和其他操作,暂时网上没有找到类似的问题处理方法。

新建WordCount项目

1、点击【File】—>【Project】,选择【Map/Reduce Project】,输入项目名称WordCount,一直回车。

在WordCount项目里新建class,名称为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.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);
        }
    }

    @SuppressWarnings("deprecation")
    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);
    }
}

2、在HDFS上创建目录input

hadoop fs -mkdir /input

3、拷贝本地README.txt到HDFS的input里

hadoop fs -copyFromLocal /hadoophome/hadoop/README.txt /input

4、点击WordCount.java,右键,点击【Run As】—>【Run Configurations】,配置运行参数,即输入和输出文件夹

hdfs://hadoop001:8020/input hdfs://hadoop001:50010/output

这里写图片描述

5、点击【Run】,运行程序。

然后就可以从DFS locations中看到对应的output生成情况。

这里写图片描述

双击对应生成文件查看即可看到统计结果。

或者在控制台输入:

hadoop fs -cat /output/part-r-00000

这样就完成第一个简单的程序开发了。

猜你喜欢

转载自blog.csdn.net/iamadk/article/details/72845852
今日推荐