MapReduceMultipleOutputDriver
跟ReduceMultipleOutputsDriver类似,MapReduceMultipleOutputDriver用来支持使用了MultipleOutputs的Map-Reduce联合测试。MapReduceDriver一节中的例子将改为,
private MapReduceDriver<LongWritable, Text, Text, TimeInfo, Text, LongWritable> mrDriver; private Map mapper; private Reduce reducer; @Before public void setUp() { mapper = new Map(); reducer = new Reduce(); //改为使用ReduceMultipleOutputsDriver mrDriver = new ReduceMultipleOutputsDriver<LongWritable, Text, Text,TimeInfo, Text, LongWritable>(mapper, reducer); } @Test public void testMapReduce_3record_1user() { Text mapInputValue1 = new Text("……"); Text mapInputValue2 = new Text("……"); Text mapInputValue3 = new Text("……"); //我们期望从以上三条Map输入计算后, //从reduce输出得到444这个卖家的平均时间为2小时. mrDriver.withInput(null, mapInputValue1) .withInput(null, mapInputValue2) .withInput(null, mapInputValue3) //表示期望"somePrefix"+444%8这个collector将搜集到数据xxx . withMutiOutput ("somePrefix"+444%8,new Text("444"),new LongWritable(2)) .runTest(); } |
如何从文件加载输入
从以上例子看到使用MRUnit需要重复写很多类似的代码,并且需要把输入数据写在代码中,显得不是很优雅,如果能从文件加载数据则会方便很多。因此通过使用annotation和扩展JUnit runner,增强了MRUnit来解决这个问题。
改造上面的例子,使得map的输入自动从文件加载,并且消除大量使用MRUnit框架API的代码。
@RunWith(MRUnitJunit4TestClassRunner.class) public class XXXMRUseAnnotationTest { //表示自动初始化mrDriver,并加载数据(如果需要) @MapInputSet @MapReduce(mapper = Map.class, reducer = Reduce.class) private MapReduceDriver<LongWritable, Text, Text, TimeInfo, Text, LongWritable> mrDriver; @Test @MapInputSet("ConsignTimeMRUseAnnotationTest.txt")//从这里加载输入数据 public void testMapReduce_3record_1user() { //只需要编写验证代码 mrDriver. withMutiOutput ("somePrefix"+444%8,new Text("444"),new LongWritable(2)) .runTest(); } } |