Eclipse - 在Hadoop上运行不会提示任何内容

时间:2014-11-19 23:41:20

标签: java eclipse hadoop

我正在尝试构建一个简单的Wordcount Hadoop项目(https://developer.yahoo.com/hadoop/tutorial/module3.html#running),但是当我点击"在Hadoop上运行"完全没有动作......实际上控制台中没有显示任何动作。

这是我的项目结构 -

enter image description here

这是我的wordcount工作文件......

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.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;

public class WordCount {

    public static void main(String[] args) {
        Configuration config = new Configuration();
        config.addResource(new Path("/HADOOP_HOME/conf/hadoop-default.xml"));
        config.addResource(new Path("/HADOOP_HOME/conf/hadoop-site.xml"));
        JobClient client = new JobClient();
        JobConf conf = new JobConf(WordCount.class);

        // specify output types
        conf.setOutputKeyClass(Text.class);
        conf.setOutputValueClass(IntWritable.class);

        // specify input and output dirs
        FileInputPath.addInputPath(conf, new Path("input"));
        FileOutputPath.addOutputPath(conf, new Path("output"));

        // specify a mapper
        conf.setMapperClass(WordCountMapper.class);

        // specify a reducer
        conf.setReducerClass(WordCountReducer.class);
        conf.setCombinerClass(WordCountReducer.class);

        client.setConf(conf);
        try {
            JobClient.runJob(conf);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3 个答案:

答案 0 :(得分:0)

我认为问题在于您用于hadoop客户端到服务器的jar文件,那么会发生什么呢?它会尝试保留在以下行并尝试搜索服务器

Configuration config = new Configuration();

尝试调试并告诉我们您是否面临更多问题,

如果没有尝试以下

您是否尝试通过指向核心站点hdfs-site

在eclipse上运行该程序
Configuration.addResource(new Path("path-to-your-core-site.xml file"));
Configuration.addResource(new Path("path-to-your-hdfs-site.xml file"));

FileInputPath.addInputPath(hdfs path to your input file);
FileInputPath.addOutputPath(hdfs path to your output file);

看到它有效并回复我们

答案 1 :(得分:0)

试试这个,

import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
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.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;


public class WordCount  {

    public static class Map extends MapReduceBase implements
            Mapper<Object, Text, Text, IntWritable> {

        @Override
        public void map(Object key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter)
                throws IOException {

            String line = value.toString();
            StringTokenizer tokenizer = new StringTokenizer(line);
            System.out.println(line);
            while (tokenizer.hasMoreTokens()) {
                value.set(tokenizer.nextToken());
                output.collect(value, new IntWritable(1));
            }

        }
    }

    public static class Reduce extends MapReduceBase implements
            Reducer<Text, IntWritable, Text, IntWritable> {

        @Override
        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,IOException  {

        JobConf conf = new JobConf(WordCount.class);
        conf.setJobName("WordCount");

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

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

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

        FileInputFormat.setInputPaths(conf, new Path("/home/user17/test.txt"));
        FileOutputFormat.setOutputPath(conf, new Path("hdfs://localhost:9000/out2"));

        JobClient.runJob(conf);

    }
}

答案 2 :(得分:0)

我遇到了完全相同的问题,我刚刚想出来了。

  1. 在运行配置中添加参数。
  2. 右键单击WordCount.java&gt;运行方式&gt;运行配置&gt; Java应用程序&gt;字数&gt;参数
  3. 输入此hdfs:// hadoop:9000 / hdfs:// hadoop:9000 /
  4. 再次应用并完成运行。
  5. 运行后,刷新项目,结果在输出文件夹中。