Search code examples
hadoopmapreducehadoop2

Mapreduce passing command line parameters


I'm trying to use the new API for map reduce and pass in a regular expession as a -D command line parameter but it isn't being picked up. The result is that the Pattern.compile(pattern) gets a NullPointerException

My mapper code is;

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
}

and my controller code is;

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

}

Finally, the command line call I'm making is;

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

Any suggestions why I can't pickup the config parameter mapper.pattern?


Solution

  • You want to use the Tools interface - eg:

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