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

在Eclipse中调试MapReduce(Hadoop 2.2或更高版本),eclipse,debugging,hadoop,mapreduce,Eclipse,Debugging,Hadoop,Mapreduce,我能够按照中的步骤在Eclipse中调试MapReduce(Hadoop 1.2.1的)。但是如何在Eclipse中调试MapReduce(Hadoop2.2或更高版本的) 您可以用同样的方法进行调试。 您只需在独立模式下运行MapReduce代码,并像任何Java代码一样使用eclipse调试MR代码。以下是我在eclipse中设置的步骤。环境:Ubuntu 16.04.2,EclipseNeon.3发行版(4.6.3RC2),jdk1.8.0_121。我在/j01/srv/hadoop下重新

我能够按照中的步骤在Eclipse中调试MapReduce(Hadoop 1.2.1的)。但是如何在Eclipse中调试MapReduce(Hadoop2.2或更高版本的)

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

以下是我在eclipse中设置的步骤。环境:Ubuntu 16.04.2,EclipseNeon.3发行版(4.6.3RC2),jdk1.8.0_121。我在/j01/srv/hadoop下重新安装了hadoop-2.7.3,这是我$hadoop_的家。将$HADOOP_HOME值替换为实际路径,如下所述。对于从Eclipse运行的hadoop,您不需要进行任何hadoop配置,真正需要的是将正确的hadoop JAR集合拉入Eclipse

步骤1创建新的Java项目
文件>新建>项目…
选择Java项目,下一步

输入项目名称:hadoopmr

单击配置默认值

源文件夹名称:src/main/java
输出文件夹名称:目标/类
单击应用,确定,然后单击下一步
单击选项卡库

单击添加外部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);
  }
}
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,调试为>调试配置…
选择Java应用程序,单击左上角的新建启动配置图标

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

进入

进入程序参数
单击应用,然后调试
程序随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) 如果程序未运行,请确保选中“项目>自动生成”。
项目>清除…以强制生成

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中,文件>导入…>现有项目进入工作区>下一步
浏览到克隆的项目并将其导入
打开.classpath,用hadoop安装主页替换/j01/srv/hadoop-2.7.3

git clone https://github.com/drachenrio/hadoopmr