使用Eclipse编写MapReduce程序

写在前面:

测试环境:Deepin 15.10.1、Hadoop-2.8.5、Eclipse Photon Release (4.8.0)

Step0:使用Eclipse操作HDFS

安装hadoop2x-eclipse-plugin插件,能够使得在Eclipse中查看HDFS中的目录和文件。

安装 hadoop-eclipse-plugin,可在下Github 上下载的 hadoop2x-eclipse-plugin.下载后,将 release 中的 hadoop-eclipse-kepler-plugin-2.6.0.jar 复制到 Eclipse 安装目录的 plugins 文件夹中,运行 eclipse -clean 重启 Eclipse。(添加插件后只需要运行一次该命令,以后按照正常方式启动就行了)。

安装完成后进行有关配置,在此强调需要先启动Hadoop,需要先启动Hadoop,需要先启动Hadoop(重要的事情说三遍)。重启完成Eclipse后,可在project Explorer中看到上图中的DFS Locations。

接着配置本地hdfs目录位置。安装插件后在Window->Preferences中会多出Hadoop Map/Reduce选项,点击配置本机上的hadoop目录。

Step1:创建MapReduce项目

MapReduce项目其实就是普通的Java项目,由于我们先前安装了插件,在此处我们可以使用插件进行快捷项目创建,在File->New->Map/Reduce Project。(其实这里就是帮助我们导入了有关的jar包,无需再手动导入)

创建项目完成后,切换Map/Reduce开发视图,Window -> Perspective -> Open Perspective -> Other,在下方同Console相同的面板区即可查看到Map/Reduce Locations 开发面板。

扫描二维码关注公众号,回复: 10006865 查看本文章

 选中Map/Reduce Locations面板中 ,在空白区右击选择New Hadoop Location,建立与 Hadoop 集群的连接。

 在弹出来的 General 选项面板中,General 的设置要与 Hadoop 的配置一致。一般两个 Host 值是一样的,如果是伪分布式,填写 localhost 即可,此处本地配置的是Hadoop伪分布式配置,设置 fs.defaultFS 为 hdfs://localhost:9001,则 DFS Master 的 Port 要改为 9001。Map/Reduce(V2) Master 的 Port 用默认的即可,Location Name 随意填写。

 配置完成后即可在DFS Locations中查看到HDFS中的文件,可以自由的进行文件的上传和下载等操作。

 Step3:编写MapReduce程序

右击WordCount项目,创建一个新的Class,这里我们使用官方的WordCount文件。代码如下:

package org.apache.hadoop.examples;
 
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);
            }
 
        }
    }
}

执行WordCount.java之前,还需要将本地Hadoop配置文件和log4j配置文件复制到项目目录(未添加log4j配置文件可以正常执行), WordCount的输入和输出文件都在HDFS中,所以需要配置Eclipse中的执行文件时的参数,正常执行后控制台无输出(图中输出WARN由于未配置log4j),在DFS Locantions中可以查看到执行结果。(test.txt文件内容只有一句话:It's a test text.txt)

 除了设置运行参数以外,还可以通过修改代码实现参数的设置。修改如下代码即可。

// String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
String[] otherArgs=new String[]{"input","output"}; /* 直接设置输入参数 */
发布了29 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42404727/article/details/90700507