在Hadoop-2.6.0中运行我自己的WordCount.java版本

时间:2015-03-04 07:57:40

标签: java hadoop jar mapreduce word-count

我正在尝试创建自己的wordcount版本并执行它。为此,我尝试通过执行以下命令来创建wordcount.jar(如此处所述http://cs.smith.edu/dftwiki/index.php/Hadoop_Tutorial_1_--_Running_WordCount用于以前的版本而不是Hadoop-2。*):

javac -classpath /usr/local/hadoop-2.6.0/share/hadoop/common/*:/usr/local/hadoop-2.6.0/share/hadoop/mapreduce/* -d wordcount_classes/ WordCount.java
jar -cvf wordcount.jar -C wordcount_classes/ .

问题是我在尝试编译wordcount类时在第一个命令中出错。在第一个命令中,我尝试包含common和mapreduce目录中存在的所有jar,因为我找不到任何名称为" hadoop-0.19.2-core.jar"的jar。正如在互联网上为hadoop-0。*

找到的教程中所述
 javac -classpath /home/hadoop/hadoop/hadoop-0.19.2-core.jar -d wordcount_classes WordCount.java 

当我按如下方式执行给定的wordcount时,它完美无缺,但我无法编译自己的版本:

 hadoop jar /usr/local/hadoop-2.6.0/share/hadoop/mapreduce/hadoop-*-examples-2.6.0.jar wordcount input output

我在互联网上搜索过,但我发现大多数教程都是针对Hadoop-1。*这与2.6.0版本不同,后者不包含任何java源文件,例如wordcount.java我需要,也有完全不同的结构。因此,我不得不单独从https://github.com/apache/hadoop-common/blob/trunk/hadoop-mapreduce-project/hadoop-mapreduce-examples/src/main/java/org/apache/hadoop/examples/WordCount.java下载wordcount.java文件。 并在我执行命令的当前目录中复制WordCount.java以创建jar。

我已从此来源http://mirror.its.dal.ca/apache/hadoop/common/hadoop-2.6.0/

下载了Hadoop

以下是我得到的错误的详细信息:

  /usr/local/hadoop-2.6.0/share/hadoop/common/hadoop-common-2.6.0.jar(org  /apache/hadoop/fs/Path.class): warning: Cannot find annotation method 'value()' in type 'LimitedPrivate': class file for org.apache.hadoop.classification.InterfaceAudience not found
  WordCount.java:54: error: cannot access Options
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
                     ^
class file for org.apache.commons.cli.Options not found
WordCount.java:68: error: method waitForCompletion in class Job cannot be applied to given types;
System.exit(job.waitForCompletion() ? 0 : 1);
               ^
required: boolean
found: no arguments
reason: actual and formal argument lists differ in length
Note: WordCount.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
2 errors
1 warning

这也是WordCount.java:

//package org.apache.hadoop.examples;
package org.myorg;

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> <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() ? 0 : 1);
 }
}

提前感谢您的帮助

5 个答案:

答案 0 :(得分:1)

对我来说,这看起来更像是一个Java问题而不是Hadoop问题。我建议使用与Hadoop 2.2.0版兼容的Eclipse和Hadoop Developer Tools。 (除非你特别需要2.6.0)。

您也可以尝试%HADOOP_HOME%\ share \ hadoop \ mapreduce \ hadoop-mapreduce-client-core- version_number .jar

也可以通过实施org.apache.hadoop.util.Tool Class info for Tool来解决您在第54行获得的第一条错误消息 我在Apache docs on options找到了此信息。

在第68行,您错过了job.waitForCompletion()方法调用的参数。我建议将true参数传递给此方法。

答案 1 :(得分:0)

我通过执行以下操作解决了这个问题(现在使用Hadoop-2.6.0更加容易)(有关详细信息,请参阅本教程http://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html#Usage):

  • 将以下内容添加到〜/ .profile文件中(不要忘记执行&#34; source~ / .profile&#34;之后):

    导出JAVA_HOME = / usr / java / default

    export PATH = $ JAVA_HOME / bin:$ PATH

    export HADOOP_CLASSPATH = $ JAVA_HOME / lib / tools.jar#这对于简化java类的编译非常重要。

  • 然后我编译WordCount.java并创建一个jar:

    $ bin / hadoop com.sun.tools.javac.Main WordCount.java

    $ jar cf wc.jar WordCount * .class

  • 然后只需使用这样的命令来执行你的工作:

    $ hadoop jar wc.jar WordCount INPUT / OUTPUT /

答案 2 :(得分:0)

试试这个:

javac --classpath `hadoop classpath` .... (rest of command)

测试ubuntu和debian。

这对我来说很好。

答案 3 :(得分:0)

System.exit上有小的修正(job.waitForCompletion()?0:1); 它应该像这样的System.exit(job.waitForCompletion(true)?0:1); 因为它会返回布尔值..

答案 4 :(得分:0)

我解决了添加到我的WordCount项目中的jar:share / hadoop / common / lib / commons-cli-1.2.jar