How to do unit testing of custom RecordReader and InputFormat classes?

后端 未结 2 625
失恋的感觉
失恋的感觉 2021-01-14 14:38

I have developed one map-reduce program. I have written custom RecordReader and InputFormat classes.

I am using MR Unit and <

2条回答
  •  天命终不由人
    2021-01-14 14:48

    You'll need a test file to be available (i'm assuming your input format extends FileInputFormat). Once you have this you can configure a Configuration object to use the LocalFileSystem (fs.default.name or fs.defaultFS set to file:///). Finally you'll need to define a FileSplit with the path, offset and length of the flie (part of the file).

    // DISCLAIMER: untested or compiled
    Configuration conf = new Configuration(false);
    conf.set("fs.default.name", "file:///");
    
    File testFile = new File("path/to/file");
    FileSplit split = new FileSplit(
           testFile.getAbsoluteFile().toURI().toString(), 0, 
           testFile.getLength(), null); 
    
    MyInputFormat inputFormat = ReflectionUtils.newInstance(Myinputformat.class, conf);
    RecordReader reader = inputFormat.createRecordReader(split, 
           new TaskAttemptContext(conf, new TaskAttemptID()));
    

    Now you can assert the records returned from the reader match that of what you would expect. You should also test (if your file format supports it) changing the offset and length of the split, as well as creating a compressed version of the file.

提交回复
热议问题