gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 15:33:37 27 4
gpt4 key购买 nike

我正在尝试创建我自己的 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 类时在第一条命令中遇到错误。在第一个命令中,我尝试包含公共(public)目录和 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 的当前目录中。

我已经从这个来源下载了 Hadoop http://mirror.its.dal.ca/apache/hadoop/common/hadoop-2.6.0/

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

  /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);
}
}

预先感谢您的帮助

最佳答案

在我看来,这更像是 Java 问题而不是 Hadoop 问题。我建议使用 Eclipse 和 Hadoop Developer Tools适用于 Hadoop 2.2.0 版。 (除非您特别需要 2.6.0)。

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

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

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

关于java - 在 Hadoop-2.6.0 中运行我自己的 WordCount.java 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28849199/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com