java - 创建 Hadoop java 示例

标签 java eclipse hadoop

我知道以前有人问过这个问题,但我似乎无法弄明白。我有一个使用 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 文件夹,并在项目设置中添加了外部 .jars。知道我做错了什么吗?

最佳答案

您还需要提供输入和输出的详细信息,可能还需要提供其他内容(取决于您在提交的示例代码之外完成了多少工作)。

这里有更详细的教程:

关于java - 创建 Hadoop java 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23329517/

相关文章:

java - 通过一列的聚合映射实体

java - 将外部 JAR 附加到 eclipse 中的 java 项目 - java.lang.NoClassDefFoundError

hadoop - Flume 架构 : Events passing to Channel

java - 在 TestNG 中测试多线程代码

java - 如何在不启动方法的情况下设置变量的属性?

java - 如何配置我的表达式语言以在 Eclipse 中工作,同时还使用 JSP/JSTL?只是 JSP 吗?

java - Java 1.8 中的 switch-case 与 Eclipse 不可能吗?

scala - 使用Spark Scala提取每月数据

shell - 通过 shell 运行 hive 命令获取错误

java - Servlet 映射 : Only when a file or directory doesn't exist?