创建hadoop java示例

vlf7wbxs  于 2021-06-03  发布在  Hadoop
关注(0)|答案(1)|浏览(252)

我知道以前有人问过这个问题,但我似乎想不出来。我有一个使用hadoop的单节点集群设置,可以正常工作。我正在尝试编译wordcount.java示例:

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);
  }
}

我已经将hadoop-2.2.0安装到/usr/local/hadoop/中,但我一辈子都搞不清楚需要在eclipse中添加哪些.jar文件来导入org.apache.hadoop文件。我很少使用java或eclipse,但我已经将hadoop主文件夹中的每个.jar文件复制到eclipse工作区中的lib文件夹中,并在项目设置中添加了external.jar。知道我做错了什么吗?

o2rvlv0m

o2rvlv0m1#

您还需要提供输入和输出的详细信息,以及可能的其他事情(取决于您在提交的示例代码之外所做的工作)。
下面是一个更详细的教程:
从eclipseKepler运行hadoop

相关问题