我正在研究此页面上找到的Hadoop示例,并且遇到找不到类的错误。Eclipse 没有看到任何语法错误,当我在 job.setMapperClass(TokenizerMapper.class)
中突出显示实例时,甚至突出显示了类 TokenizerMapper。这是因为它是一个子类,还是我在这里忽略了什么?我使用命令 hadoop jar word.jar input output
从主 Hadoop 节点执行它(我已经发现目录 (args) 参数是 HDFS 中/user/[myuser] 的相对路径,所以这不是问题。
这里的任何帮助将不胜感激。
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;
public class WordCount {
Configuration configuration = null;
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) {
Configuration conf = new Configuration();
Job job;
conf.addResource(new Path("/work/hadoop/config","core-site.xml"));
conf.addResource(new Path("/work/hadoop/config","hdfs-site.xml"));
try {
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);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
命令行中缺少 jar 名称。在您的文档中搜索此内容。除了wc.jar之外,它还具有WordCount,这是您的驱动程序类名称。
bin/hadoop jar wc.jar WordCount /user/joe/wordcount/input /user/joe/wordcount/output
当您的 jar 应用程序入口点是您WordCount.java
时,您可以毫无问题地运行此命令。 hadoop jar word.jar input output
.目前 jar 没有入口点。因此,如果未在 jar 文件中指定类的完全限定名称,则会出错。
通过使用 Eclipse,您可以将类设置为默认类/入口点,以如下所示运行。
Project=> Right click => Export => JAR File => Next => Specify Jar Path => Next
=> JAR Manifest specification.
select the class of the application entry point.
Main class. Browse and select the main class(WordCount in your case).
Finish.
希望对您有所帮助!
将 JAR 文件导出到类路径定义的目录可以解决此问题。从 eclipse 导出 JAR 文件时,您可能会遇到"资源与文件系统不同步"错误。在这种情况下,只需右键单击项目并选择日食时刷新。这将解决"不同步"问题