0

我是 Hadoop 的新手,但已经阅读了 Yahoo 的教程,并且已经写了一些 mapReduce 作业。我之前的所有作品都使用了 TextInputFormat,但我现在需要将其更改为 KeyValueInputFormat。问题是在hadoop 0.20.2中找不到KeyValueInputFormat.class?

我在下面附上我的代码(这是仅更改输入格式的字数示例)

package org.myorg;

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(keyValueInputFormat.class);  //The modified input format
        conf.setOutputFormat(TextOutputFormat.class);

        FileInputFormat.setInputPaths(conf, new Path(args[0]));
        FileOutputFormat.setOutputPath(conf, new Path(args[1]));

        JobClient.runJob(conf);
      }
    }
4

1 回答 1

2

org.apache.hadoop.mapreduce.lib.input 中有 KeyValueTextInputFormat。

一些旧教程基于旧版本的 Hadoop API。我建议您阅读一些较新的教程。

这就是我在 KeyValueTextInputFormat 上找到源代码时得到的。

package org.apache.hadoop.mapreduce.lib.input;

import java.io.IOException;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.JobContext;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

public class KeyValueTextInputFormat extends FileInputFormat<Text, Text> {

public KeyValueTextInputFormat() {
    //compiled code
    throw new RuntimeException("Compiled Code");
}

protected boolean isSplitable(JobContext context, Path file) {
    //compiled code
    throw new RuntimeException("Compiled Code");
}

public RecordReader<Text, Text> createRecordReader(InputSplit genericSplit,     TaskAttemptContext context) throws IOException {
    //compiled code
    throw new RuntimeException("Compiled Code");
}
}
于 2012-07-12T02:16:44.553 回答