在Hadoop中使用MRUnit进行单元测试
作者:网络转载 发布时间:[ 2013/9/9 14:44:56 ] 推荐标签:
前提
1. 了解JUnit4.x的使用。
2. 了解Mock的概念在单元测试中的应用。
3. 了解Hadoop中MapReduce的编程模型。
如果您对Junit和Mock不了解,可以先阅读Unit testing with JUnit 4.x and EasyMock in Eclipse - Tutorial 。
如果您对Hadoop中MapReduce的编程模型不了解,可以先阅读Map/Reduce Tutorial 。
介绍
MRUnit是一款由Couldera公司开发的专门针对Hadoop中编写MapReduce单元测试的框架。
它可以用于0.18.x版本中的经典org.apache.hadoop.mapred.*的模型,也能在0.20.x版本org.apache.hadoop.mapreduce.*的新模型中使用。
官方的介绍如下:
MRUnit is a unit test library designed to facilitate easy integration between your MapReduce development process and standard development and testing tools such as JUnit. MRUnit contains mock objects that behave like classes you interact with during MapReduce execution (e.g., InputSplit and OutputCollector) as well as test harness "drivers" that test your program's correctness while maintaining compliance with the MapReduce semantics. Mapper and Reducer implementations can be tested individually, as well as together to form a full MapReduce job.
安装
在目前Hadoop的发行版中,并没有默认包含MRUnit。你需要去Couldera公司的官网中去下载一个由他们再次发行的版本。
推荐的版本为:hadoop-0.20.1+133.tar.gz 。
下载这个文件后,你将在hadoop-0.20.1+133/contrib/mrunit目录中找到我们需要的jar包:hadoop-0.20.1+133-mrunit.jar。
为了使用MRUnit,我们需要将hadoop-0.20.1+133-mrunit.jar和Junit4.x使用的jar包:junit.jar都添加到我们开发Hadoop程序项目的classpath中。
示例
代码是好的文档,我们先看一个简单的map单元测试示例,代码如下:
package gpcuster.cnblogs.com;
import junit.framework.TestCase;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.lib.IdentityMapper;
import org.junit.Before;
import org.junit.Test;
import org.apache.hadoop.mrunit.MapDriver;
public class TestExample extends TestCase {
private Mapper<Text, Text, Text, Text> mapper;
private MapDriver<Text, Text, Text, Text> driver;
@Before
public void setUp() {
mapper = new IdentityMapper<Text, Text>();
driver = new MapDriver<Text, Text, Text, Text>(mapper);
}
@Test
public void testIdentityMapper() {
driver.withInput(new Text("foo"), new Text("bar"))
.withOutput(new Text("foo"), new Text("bar"))
.runTest();
}
}
相关推荐

最新发布
性能测试之测试环境搭建的方法
2020/7/21 15:39:32软件测试是从什么时候开始被企业所重视的呢?
2020/7/17 9:09:11Android自动化测试框架有哪些?有什么用途?
2020/7/17 9:03:50什么样的项目适合做自动化?自动化测试人员应具备怎样的能力?
2020/7/17 8:57:06几大市面主流性能测试工具测评
2020/7/17 8:52:11RPA机器人能够快速响应企业需求,是怎么做到的?
2020/7/17 8:48:05Bug可以真正消灭吗?为什么?
2020/7/17 8:43:03软件测试基本概念是怎么来的?软件测试生命周期的形成历经了什么?
2020/7/16 9:11:10