Mapreduce传递命令行参数

时间:2014-05-02 19:00:07

标签: hadoop mapreduce hadoop2

我正在尝试将新API用于map reduce并将常规传递作为-D命令行参数传递,但它没有被拾取。结果是Pattern.compile(模式)得到NullPointerException

我的映射器代码是;

public class MdacMapper extends Mapper<Text, Text, Text, Text> {

    private Pattern compiledPattern;

    public void setup(Context context) {
        Configuration config = context.getConfiguration();
        String pattern = config.get("mapper.pattern");
        compiledPattern = Pattern.compile(pattern);
    }

    // mapper
}

我的控制器代码是;

public class JobController {

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();

       Job job = new Job(conf);
       job.setJarByClass(JobController.class);
       job.setInputFormatClass(TextInputFormat.class);
       job.setOutputFormatClass(TextOutputFormat.class);

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

       job.setMapperClass(MdacMapper.class);
       job.setReducerClass(MdacReducer.class);
       job.setMapOutputKeyClass(Text.class);
       job.setMapOutputValueClass(Text.class);
       job.setOutputKeyClass(Text.class);
       job.setOutputValueClass(Text.class);

       job.waitForCompletion(true);
       job.submit();
    }

}

最后,我正在进行的命令行调用是;

 hadoop jar mapreducer.jar /input/aclogdrop /output/acjava \ 
 -Dmapper.pattern=".*\|(\d{8}):\d*\.\d*\|(\d+)\|AC_ADO_QRY\(\d*?\)\|\d?\s?\[\s?(.*?)\]"

有什么建议我无法获取配置参数mapper.pattern?

3 个答案:

答案 0 :(得分:4)

您想使用Tools interface - 例如:

public class WordCount extends Configured implements Tool {

  public static class Map
      extends Mapper<Object, Text, Text, IntWritable> {

    private final static IntWritable one = new IntWritable(1);
    private final static Text word = new Text();

    public void setup(Context context) {
        Configuration config = context.getConfiguration();
        String wordstring = config.get("mapper.word");
        word.set(wordstring);
    }

    /* wordcount mapper, reducer */
  }

  public static void main(String[] args) throws Exception {
    int res = ToolRunner.run(new Configuration(), new WordCount(), args);
    System.exit(res);
  }

  public int run(String[] args) throws Exception {

    if (args.length != 2) {
      System.err.println("Usage: wordcount <in> <out>");
      System.exit(2);
    }

    Configuration conf = this.getConf();

    Job job = new Job(conf, "WordCount");
    job.setJarByClass(WordCount.class);

    job.setMapperClass(Map.class);
    job.setReducerClass(Reduce.class);

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);

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

    return job.waitForCompletion(true) ? 0 : 1;
  }
}

答案 1 :(得分:2)

也许你应该使用GenericOptionsParser 这是API GenericOptionsParser

答案 2 :(得分:2)

此外,使用Tool界面时,必须首先在命令中指定通用选项(例如-D property = value),然后指定应用程序参数。

正确的命令如下所示:

hadoop jar mapreducer.jar -D mapper.pattern=".*\|(\d{8}):\d*\.\d*\|(\d+)\|AC_ADO_QRY\(\d*?\)\|\d?\s?\[\s?(.*?)\]" /input/aclogdrop /output/acjava