IT虾米网

java之创建 Hadoop java 示例

yjmyzz 2023年09月08日 编程语言 262 0

我知道以前有人问过这个问题,但我似乎无法弄明白。我有一个使用 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。知道我做错了什么吗?

请您参考如下方法:

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

这里有更详细的教程:


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!