[英]Running my own version of WordCount.java in Hadoop-2.6.0
I am trying to create my own version of wordcount and execute it. 我正在尝试创建自己的wordcount版本并执行它。 For that, I am trying to create the wordcount.jar by executing the following command (as described here http://cs.smith.edu/dftwiki/index.php/Hadoop_Tutorial_1_--_Running_WordCount for previous releases than Hadoop-2.*):
为此,我正在尝试通过执行以下命令来创建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/ .
The problem is that I get errors in the first command when trying to compile the wordcount class. 问题是尝试编译wordcount类时,我在第一个命令中遇到错误。 In the first command I try to include all the jars that exist in the common and mapreduce directories, because I don't find any jar that has the name "hadoop-0.19.2-core.jar" as described in tutorials I found in the internet for the hadoop-0.*
在第一个命令中,我尝试包括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
When I execute the given wordcount as follows, it works perfect, but I can't compile my own version: 当我按如下方式执行给定的字数计数时,它可以完美运行,但无法编译自己的版本:
hadoop jar /usr/local/hadoop-2.6.0/share/hadoop/mapreduce/hadoop-*-examples-2.6.0.jar wordcount input output
I've searched in the internet but I find most of tutorials are for Hadoop-1.* which is different from the 2.6.0 version which does not contain any java source file such as the wordcount.java I need, and also has a completely different structure. 我已经在互联网上进行搜索,但是我发现大多数教程都是针对Hadoop-1。*的,它与2.6.0版本不同,后者不包含任何Java源文件(如wordcount.java),并且具有完全不同的结构。 Hence, I was obliged to download the wordcount.java file separately from https://github.com/apache/hadoop-common/blob/trunk/hadoop-mapreduce-project/hadoop-mapreduce-examples/src/main/java/org/apache/hadoop/examples/WordCount.java and copy the WordCount.java in the current directory where I execute my commands to create the jar.
因此,我不得不从https://github.com/apache/hadoop-common/blob/trunk/hadoop-mapreduce-project/hadoop-mapreduce-examples/src/main/java/单独下载wordcount.java文件。 org / apache / hadoop / examples / WordCount.java并将WordCount.java复制到执行命令以创建jar的当前目录中。
I've downloaded Hadoop from this source http://mirror.its.dal.ca/apache/hadoop/common/hadoop-2.6.0/ 我已经从此资源http://mirror.its.dal.ca/apache/hadoop/common/hadoop-2.6.0/下载了Hadoop。
Here is the details of the errors I get: 这是我得到的错误的详细信息:
/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
Here is also the WordCount.java : 这也是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);
}
}
Thank you in advance for your help 预先感谢您的帮助
This looks more like a Java problem than a Hadoop problem to me. 在我看来,这更像是Java问题,而不是Hadoop问题。 I would recommend to use Eclipse and Hadoop Developer Tools that works with Hadoop version 2.2.0.
我建议使用与Hadoop 2.2.0版一起使用的Eclipse和Hadoop Developer Tools 。 (unless you specifally need 2.6.0).
(除非您特别需要2.6.0)。
Also you may try the %HADOOP_HOME%\\share\\hadoop\\mapreduce\\hadoop-mapreduce-client-core- version_number .jar 您也可以尝试%HADOOP_HOME%\\ share \\ hadoop \\ mapreduce \\ hadoop-mapreduce-client-core- version_number .jar
Also the first error message you get at line 54 may be resolved by implementing org.apache.hadoop.util.Tool
Class info for Tool I found this info at Apache docs on options . 同样,您可以通过实现
org.apache.hadoop.util.Tool
工具的类信息来解决您在第54行获得的第一条错误消息,我在options上的Apache文档中找到了该信息。
At line 68 it you're missing an argument for the job.waitForCompletion()
method call. 在第68行,您缺少
job.waitForCompletion()
方法调用的参数。 I would suggest to pass a true
argument to this method. 我建议将一个
true
参数传递给此方法。
I resolved the problem by doing the following (It is even easier now with Hadoop-2.6.0) (see this tutorial for more details http://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html#Usage ): 我通过执行以下操作解决了问题(现在使用Hadoop-2.6.0甚至更容易了)(有关更多详细信息,请参阅本教程, http://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop- mapreduce-client-core / MapReduceTutorial.html#Usage ):
Add the following to your ~/.profile file (don't forget to execute "source ~/.profile" afterwards): 将以下内容添加到您的〜/ .profile文件中(不要忘了之后执行“ source〜/ .profile”):
export JAVA_HOME=/usr/java/default 导出JAVA_HOME = / usr / java / default
export PATH=$JAVA_HOME/bin:$PATH 导出PATH = $ JAVA_HOME / bin:$ PATH
export HADOOP_CLASSPATH=$JAVA_HOME/lib/tools.jar # This is very important to facilitate compilation of your java classes. export HADOOP_CLASSPATH = $ JAVA_HOME / lib / tools.jar#这对简化Java类的编译非常重要。
I then compile WordCount.java and create a jar: 然后,我编译WordCount.java并创建一个jar:
$ bin/hadoop com.sun.tools.javac.Main WordCount.java $ bin / hadoop com.sun.tools.javac.Main WordCount.java
$ jar cf wc.jar WordCount*.class $ jar cf wc.jar WordCount * .class
Then just use a command like this to execute your job: 然后只需使用如下命令执行您的工作即可:
$ hadoop jar wc.jar WordCount INPUT/ OUTPUT/ $ hadoop jar wc.jar WordCount输入/输出/
Try this: 尝试这个:
javac --classpath `hadoop classpath` .... (rest of command)
Test on ubuntu and debian. 在ubuntu和debian上测试。
This work fine for me. 这对我来说很好。
there is small correction at System.exit(job.waitForCompletion() ? 0 : 1); 在System.exit(job.waitForCompletion()?0:1)处有较小的更正; it should be like this System.exit(job.waitForCompletion(true) ? 0 : 1);
应该像这样System.exit(job.waitForCompletion(true)?0:1); because it will return boolean value..
因为它将返回布尔值。
我解决了将位于以下位置的jar添加到WordCount项目中的问题:share / hadoop / common / lib / commons-cli-1.2.jar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.