我是 Hadoop 编程的新手,我已经通过在三节点集群上设置 Hadoop 2.7.1 开始学习。我试过在 Hadoop 中运行开箱即用的 helloworld jar,它运行良好并成功,但我在本地机器上编写了自己的驱动程序代码并将其捆绑到一个 jar 中并以这种方式执行但它失败了,没有错误消息。
这是我的代码,这就是我所做的。
WordCountMapper.java
package mot.com.bin.test;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
public class WordCountMapper extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable>
{
public void map(LongWritable key, Text Value,
OutputCollector<Text, IntWritable> opc, Reporter r)
throws IOException {
String s = Value.toString();
for (String word :s.split(" ")) {
if( word.length() > 0) {
opc.collect(new Text(word), new IntWritable(1));
}
}
}
}
WordCountReduce.java
package mot.com.bin.test;
import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
public class WordCountReduce extends MapReduceBase implements Reducer < Text, IntWritable, Text, IntWritable>{
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> opc, Reporter r)
throws IOException {
// TODO Auto-generated method stub
int i = 0;
while (values.hasNext()) {
IntWritable in = values.next();
i+=in.get();
}
opc.collect(key, new IntWritable (i));
}
WordCount.java
/**
* **DRIVER**
*/
package mot.com.bin.test;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.io.Text;
//import com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider.Text;
/**
* @author rgb764
*
*/
public class WordCount extends Configured implements Tool{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public int run(String[] arg0) throws Exception {
if (arg0.length < 2) {
System.out.println("Need input file and output directory");
return -1;
}
JobConf conf = new JobConf();
FileInputFormat.setInputPaths(conf, new Path( arg0[0]));
FileOutputFormat.setOutputPath(conf, new Path( arg0[1]));
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
conf.setMapperClass(WordCountMapper.class);
conf.setReducerClass(WordCountReduce.class);
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
JobClient.runJob(conf);
return 0;
}
}
首先,我尝试将其作为 jar 从 eclipse 中提取并在我的 hadoop 集群中运行。没有错误也没有成功。然后将我的个人 java 文件移动到我的 NameNode 并编译每个 java 文件,然后在那里创建 jar 文件,hadoop 命令仍然没有返回结果但也没有错误。请帮我解决这个问题。
hadoop jar WordCout.jar mot.com.bin.test.WordCount /karthik/mytext.txt /tempo
使用 Maven 提取所有依赖的 jar 文件,并将它们添加到我的名称节点的类路径中。帮我弄清楚哪里出错了。
最佳答案
IMO,您缺少 main 方法中的代码,该代码实例化工具实现(在您的情况下为 WordCount)并运行相同的代码。
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(), new WordCount(), args);
System.exit(res);
}
引用this .
关于java - Hadoop Jar 运行但没有输出。 Driver、mapper 和 reduce 在 namenode 中编译成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37671503/