Hadoop单元测试方法—使用和增强MRUnit

发表于:2013-12-10 11:14

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:zxsted    来源:51Testing软件测试网采编

  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();
}
}
33/3<123
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号