接上篇,居然非得分两篇
3 增强MRUnit
下面介绍为MRUnit框架增加了支持MultipleOutputs、从文件加载数据集和自动装配等几个特性,使它更加便于使用。
如何支持MultipleOutputs
然而很多场景下我们需要使用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 = new
ArrayList<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();
}
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();
}
}
分享到:
相关推荐
MRUnit是一个专门为Hadoop MapReduce设计的单元测试框架,它提供了Mock对象来模拟Map和Reduce阶段的各种组件,简化了测试流程。 **1. MRUnit简介** MRUnit主要提供了四种Driver类,用于不同类型的测试场景: - **...
使用MRUnit时,首先需要在项目的pom.xml文件中添加MRUnit的依赖,区分Hadoop 1.x和2.x版本的库。Eclipse作为IDE,可以方便地进行MapReduce项目管理和MRUnit的单元测试。通过编写测试类,将Mapper和Reducer作为参数...
- 使用MRUnit编写测试单元。 - 输出提交机制。 - 分布式缓存功能。 - 任务内存监控。 - 通过MapReduce处理Avro数据。 - 在Oozie中运行简单的MapReduce工作流。 - **HDFS增强**: - 高可用性(HA)支持。 - ...
例如,YARN的引入代表了对资源管理和调度的改进,它使得Hadoop可以在多租户环境下使用,安全性能也得到了增强。而像Impala和Spark这样的新组件则给Hadoop带来了更为灵活、实时的数据查询和处理能力。这意味着Hadoop...
使用MRUnit和PowerMock进行模拟业务流程的单元测试。 分析建模阶段,以提高用户转化率为例,通过对大数据平台的支持,识别出旧车接近设计寿命的用户群体,并制定相应的营销策略,如短信通告和电话销售。根据营销...