在Eclipse中调试MapReduce(Hadoop 2.2或更高版本)

时间:2015-09-13 02:48:03

标签: eclipse debugging hadoop mapreduce

我可以按照http://www.thecloudavenue.com/2012/10/debugging-hadoop-mapreduce-program-in.html中的步骤在Eclipse中调试MapReduce(Hadoop 1.2.1)。但是如何在Eclipse中调试MapReduce(Hadoop 2.2或更高版本)?

2 个答案:

答案 0 :(得分:0)

您可以以相同的方式进行调试。 您只需在独立模式下运行MapReduce代码,并使用eclipse像任何Java代码一样调试MR代码。

答案 1 :(得分:0)

以下是我在Eclipse中设置的步骤。环境:Ubuntu 16.04.2,Eclipse Neon.3发行版(4.6.3RC2),jdk1.8.0_121。我在/ j01 / srv / hadoop下安装了一个新的hadoop-2.7.3,这是我的$ HADOOP_HOME。将$ HADOOP_HOME值替换为您在下面引用的实际路径。对于从Eclipse运行的hadoop,您不需要执行任何hadoop配置,真正需要的是将正确的hadoop jar集合引入Eclipse。

步骤1创建新的Java项目
文件>新>项目...
选择Java Project,Next

create new Java project

输入项目名称:hadoopmr

create new Java project(continue)

单击配置默认...

enter image description here

源文件夹名称:src / main / java
输出文件夹名称:目标/类别
单击Apply,OK,然后单击Next 单击选项卡库 add external hadoop jars

单击添加外部JAR ...
浏览到hadoop安装文件夹,添加以下jar,完成后单击Finish

$HADOOP_HOME/share/hadoop/common/hadoop-common-2.7.3.jar
$HADOOP_HOME/share/hadoop/common/hadoop-nfs-2.7.3.jar

$HADOOP_HOME/share/hadoop/common/lib/avro-1.7.4.jar
$HADOOP_HOME/share/hadoop/common/lib/commons-cli-1.2.jar
$HADOOP_HOME/share/hadoop/common/lib/commons-collections-3.2.2.jar
$HADOOP_HOME/share/hadoop/common/lib/commons-configuration-1.6.jar
$HADOOP_HOME/share/hadoop/common/lib/commons-io-2.4.jar
$HADOOP_HOME/share/hadoop/common/lib/commons-lang-2.6.jar
$HADOOP_HOME/share/hadoop/common/lib/commons-logging-1.1.3.jar
$HADOOP_HOME/share/hadoop/common/lib/hadoop-auth-2.7.3.jar
$HADOOP_HOME/share/hadoop/common/lib/httpclient-4.2.5.jar
$HADOOP_HOME/share/hadoop/common/lib/httpcore-4.2.5.jar
$HADOOP_HOME/share/hadoop/common/lib/jackson-core-asl-1.9.13.jar
$HADOOP_HOME/share/hadoop/common/lib/jackson-mapper-asl-1.9.13.jar
$HADOOP_HOME/share/hadoop/common/lib/log4j-1.2.17.jar
$HADOOP_HOME/share/hadoop/common/lib/slf4j-api-1.7.10.jar
$HADOOP_HOME/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar

$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-common-2.7.3.jar
$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.7.3.jar
$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-jobclient-2.7.3.jar
$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-shuffle-2.7.3.jar
$HADOOP_HOME/share/hadoop/mapreduce/lib-examples/hsqldb-2.0.0.jar

$HADOOP_HOME/share/hadoop/tools/lib/guava-11.0.2.jar
$HADOOP_HOME/share/hadoop/yarn/hadoop-yarn-api-2.7.3.jar
$HADOOP_HOME/share/hadoop/yarn/hadoop-yarn-common-2.7.3.jar

步骤2创建MapReduce示例
创建一个新包:org.apache.hadoop.examples
使用以下内容在包org.apache.hadoop.examples下创建WordCount.java:

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.examples;

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> [<in>...] <out>");
      System.exit(2);
    }
    Job job = Job.getInstance(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);
    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);
  }
}

使用以下内容在/ home / hadoop / input /(或您的路径)下创建input.txt:

What do you mean by Object
What is Java Virtual Machine
How to create Java Object
How Java enabled High Performance

步骤3设置调试配置
在Eclipse中,打开WordCount.java,在你喜欢的地方设置断点 右键单击WordCount.java,Debug As&gt;调试配置...
选择Java Application,单击左上角图标上的新启动配置

Debug Configuration

在主类框中输入org.apache.hadoop.examples.WordCount
单击“参数”选项卡

enter image description here

输入

/home/hadoop/input/input.txt /home/hadoop/output

进入程序参数
单击Apply,然后单击Debug
程序从hadoop开始,它应该达到你设置的断点。

检查结果

ls -l /home/hadoop/output
-rw-r--r-- 1 hadoop hadoop 131 Apr  5 22:59 part-r-00000
-rw-r--r-- 1 hadoop hadoop   0 Apr  5 22:59 _SUCCESS

注意:

1)如果程序没有运行,请确保Project&gt;检查自动构建。
项目&gt;清洁......强制建造

2)您可以从

获得更多示例
jar xvf $HADOOP_HOME/share/hadoop/mapreduce/sources/hadoop-mapreduce-examples-2.7.3-sources.jar

将它们复制到此项目中以继续探索

3)你可以从

下载这个eclipse项目
git clone https://github.com/drachenrio/hadoopmr

在Eclipse中,文件&gt;导入...&gt; <工作区中的现有项目>接下来
浏览到克隆的项目并导入它 打开.classpath,用你的hadoop安装主页替换/j01/srv/hadoop-2.7.3