读书人

hadoop单元测试方法-施用和增强MRUnit

发布时间: 2012-12-23 11:28:15 作者: rapoo

hadoop单元测试方法--使用和增强MRUnit[2]

接上篇,居然非得分两篇

?

3?增强MRUnit

???????? 然而很多场景下我们需要使用MultipleOutputs作为reduce的多文件输出,MRUnit缺少支持。分析源码后为MRUnit增强扩展了两个Driver:ReduceMultipleOutputsDriver和MapReduceMultipleOutputDriver来支持MultipleOutputs。

?

ReduceMultipleOutputsDriver

???????? ReduceMultipleOutputsDriver是ReduceDriver的增强版本,假设前面例子中的Reduce使用了MultipleOutputs作为输出,那么Reduce的测试将出现错误。


?

使用ReduceMultipleOutputsDriver改造上面的测试用例(注意粗体部分),

private Reduce reducer;

??? @Before

??? public void setUp() {

??????? reducer = new Reduce();

?????? //注意这里ReduceDriver改为使用ReduceMultipleOutputsDriver

??????? reduceDriver = new ReduceMultipleOutputsDriver<Text, TimeInfo, ????????????????????? ????????????? Text,LongWritable>(reducer);

??? }

?

??? @Test

??? public void testReduce () {

??????? List<TimeInfo> values = newArrayList<TimeInfo>();

??????? values.add(new TimeInfo(1, 3));//一次3小时

??????? values.add(new TimeInfo(2, 5));//两次总共5小时

??????? values.add(new TimeInfo(3, 7));//三次总共7小时

?????? //values作为444这个卖家的reduce输入,

?????? //期望计算出平均为2小时

??????? reduceDriver.withReducer(reducer)

?????????? ??? .withInput(new Text("444"), values)

?????????? ??? //Note

?????????? ??? //假设使用id(444)%8的方式来分文件

????????????? //表示期望"somePrefix"+444%8这个collector将搜集到数据xxx

?????????? ??? . withMutiOutput ("somePrefix"+444%8,new Text("444"),new? ???? ????????????? ??????????????????????????????? LongWritable(2))

????????????? .runTest();

???????? 跟ReduceMultipleOutputsDriver类似,MapReduceMultipleOutputDriver用来支持使用了MultipleOutputs的Map-Reduce联合测试。MapReduceDriver一节中的例子将改为,

private MapReduceDriver<LongWritable, Text, Text, TimeInfo,Text, LongWritable> mrDriver;

??? private Map mapper;

??? @Before

??? public void setUp() {

??????? mapper = new Map();

??????? reducer = new Reduce();

?????? //改为使用ReduceMultipleOutputsDriver

?????? ?mrDriver = new ReduceMultipleOutputsDriver<LongWritable,Text, Text, ????????????? TimeInfo, Text,LongWritable>(mapper, reducer);

??? @Test

??????? 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)

??? @MapInputSet

??? @Test

??? @MapInputSet("ConsignTimeMRUseAnnotationTest.txt")//从这里加载输入数据

?????????? //只需要编写验证代码

?????? mrDriver.withMutiOutput ("somePrefix"+444%8,new Text("444"),new LongWritable(2))

????????????? ?????????????????? .runTest();

??? }

}

1 楼 heipark 2012-03-13 兄弟,说了半天,没有看到源码呀。 2 楼 Jen 2012-03-28 heipark 写道兄弟,说了半天,没有看到源码呀。
源码已经集成到官方新版本中了 3 楼 vanillaer 2012-05-03 请教一下,您提到的支持MultipleOutputs是怎么样的实现思路,如果是MultipleOutputs,那么mock 原来的outputcollector好像是不行的?

读书人网 >编程

热点推荐