Search code examples
testinghadoopmapreducerecordreader

Hadoop Map reduce Testing - custom record reader


I have written a custom record reader and looking for sample test code to test my custom reader using MRUnit or any other testing framework. Its working fine as per the functionality but I would like to add test cases before I make an install. Any help would be appreciable.


Solution

  • In my opinion, a custom record reader is like any iterator. For testing my record reader I have been able to work without MRUnit or any other hadoop junit frameworks. The test executes quickly and the footprint is small too. Initialize the record reader in your test case and keep iterating on it. Here is a pseudocode from one of my tests. I can provide you more details if you want to proceed in this direction.

    MyInputFormat myInputFormat = new MyInputFormat();
    //configure job and provide input format configuration
    Job job = Job.getInstance(conf, "test");    
    conf = job.getConfiguration();
    
    // verify split type and count if you want to verify the input format also
    List<InputSplit> splits = myInputFormat.getSplits(job);
    
    TaskAttemptContext context = new TaskAttemptContextImpl(conf, new TaskAttemptID());
    RecordReader<LongWritable, Text> reader = myInputFormat.createRecordReader(splits.get(1), context);
    reader.initialize(splits.get(1), context);
    
    for (; number of expected value;) {
      assertTrue(reader.nextKeyValue());
      // verify key and value
      assertEquals(expectedLong, reader.getCurrentKey());
    }