写一个空的MapReduce作业

时间:2014-10-18 17:03:38

标签: java hadoop mapreduce hortonworks-data-platform

我想写一个空的mapreduce作业,实际上我指的是一个什么都不做的mapreduce工作,只有一个Mapper,一个Reducer和一个主类。我希望它在hortonwoks沙箱2.1中进行测试。 这是我的代码:

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 MainClassName {

  public static class Map extends MapReduceBase 
    implements Mapper<IntWritable, Text, IntWritable, Text> 
  {
    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 
    {
      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 data = 0;
      }
      output.collect(key, new IntWritable(data));
    }
  }

  public static void main(String[] args) throws Exception 
  {
    JobConf conf = new JobConf(MainClassName.class);
    conf.setJobName("JobName");

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

这是对的吗? 但它给了我一个错误:

描述资源路径位置类型 MainClassName.Map类型必须实现继承的抽象方法Mapper.map(Text,Text,OutputCollector,Reporter)MainClassName.java / mainempty / src第14行Java问题

而且我想知道必须导入哪些java文件才能运行简单的工作。 非常感谢。 :)

1 个答案:

答案 0 :(得分:1)

您的类型参数有点混乱。您的映射器正在使用<LongWritable,Text>对,并输出<Text,IntWritable>对。但是你的班级声明说:

implements Mapper<IntWritable, Text, IntWritable, Text> 

应该阅读

implements Mapper<LongWritable, Text, Text, LongWritable>

其余的看起来还不错。