gpt4 book ai didi

java - Java:执行Hadoop MapReduce存在一些问题

转载 作者:行者123 更新时间:2023-12-02 20:58:36 24 4
gpt4 key购买 nike

我想使用MapReduce执行一个字数统计。我从hadoop网站上获取了代码:

package org.myorg;
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;

public class WordCount {

public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();

public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
output.collect(word, one);
}
}
}

public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}

public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(WordCount.class);
conf.setJobName("wordcount");

conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);

conf.setMapperClass(Map.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class);

conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);

FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));

JobClient.runJob(conf);
}
}

我写了这个脚本来编译和执行程序:
#!/bin/bash
#Compile
javac -classpath $HADOOP_INSTALL/share/hadoop/common/hadoop-common- 2.6.5.jar:$HADOOP_INSTALL/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.6.5.jar:$HADOOP_INSTALL/share/hadoop/common/lib/commons-cli-1.2.jar -d /home/ciro/Scrivania/BDABI/wordcount *.java

#Convert into Jar File
jar -cvf WordCountj.jar -C /home/ciro/Scrivania/BDABI/wordcount/org/myorg .

#Run JAR File
hadoop jar /home/ciro/Scrivania/BDABI/wordcount/WordCountj.jar wordcount /user/inputdata/test.txt outputwc

这些是我收到的错误:
Exception in thread "main" java.lang.ClassNotFoundException: wordcount
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.apache.hadoop.util.RunJar.run(RunJar.java:214)
at org.apache.hadoop.util.RunJar.main(RunJar.java:136)

有什么问题?我在ubuntu 14.04和hadoop 2.6.5中使用了一个集群节点(我的电脑)

最佳答案

您的jar命令中的jar路径和类名错误

您编写了/home/ciro/Scrivania/BDABI/wordcount/WordCountj.jar而不是/home/ciro/Scrivania/BDABI/wordcount/org/myorg/WordCountj.jarwordcount而不是WordCount

关于java - Java:执行Hadoop MapReduce存在一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43808797/

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