Hadoop Jar运行但没有输出。驱动程序,映射器和reduce命令在namenode

时间:2016-06-07 05:54:02

标签: java maven hadoop

我是Hadoop Programming的新手,我已经开始通过在三节点集群上设置Hadoop 2.7.1来学习。我已经尝试在Hadoop中运行开箱即用的helloworld jars并且它运行良好但我在本地机器中编写了自己的驱动程序代码并将其捆绑到一个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;
        }

}

首先,我尝试将其从eclipse中提取为jar并在我的hadoop集群中运行它。没有错误,但也没有成功。然后将我的个人java文件移动到我的NameNode并编译每个java文件然后在那里创建了jar文件,仍然hadoop命令返回没有结果但也没有错误。请帮助我。

hadoop jar WordCout.jar mot.com.bin.test.WordCount /karthik/mytext.txt /tempo

使用Maven提取所有相关jar文件,我将它们添加到我的名称节点中的类路径中。帮我弄清楚我出错的地方和地点。

1 个答案:

答案 0 :(得分:1)

IMO您缺少main方法中的代码,该代码实例化Tool实现(在您的情况下为WordCount)并运行相同的代码。

     public static void main(String[] args) throws Exception {        
           int res = ToolRunner.run(new Configuration(), new WordCount(), args);         
           System.exit(res);
   }

参考this