使用MRUnit,Mockito和PowerMock进行Hadoop MapReduce作业的单元测试
0、preliminary 环境搭建
Setup development environment
Download the latest version of MRUnit jar from Apache website: https://repository.apache.org/content/repositories/releases/org/apache/mrunit/mrunit/. For example if you are using the Hadoop version 1.0.3, download mrunit-x.x.x-incubating-hadoop2.jar.
Include the jar in your IDE classpath. Also download the latest verison of mokito (http://code.google.com/p/mockito/) and JUnit jar and add them to your class path of development environment.
请导入jar包到CentOS中的eclipse
download site: https://mrunit.apache.org/general/downloads.html
download web site https://repository.apache.org/content/repositories/releases/org/apache/mrunit/mrunit/1.1.0/
You should Attention:
hadoop 框架包里面有自带的mock jar包,吧他们全部删除,否则要产生jar包兼容性异常(编译器不晓得调哪个jar包为好)
转自: http://www.infoq.com/cn/articles/HadoopMRUnit
(请关心里面的干货——测试用例文末)
引言
Hadoop MapReduce作业有着独一无二的代码架构,这种代码架构拥有特定的模板和结构。这样的架构会给测试驱动开发和单元测试带来一些麻烦。这篇文章是运用MRUnit,Mockito和PowerMock的真实范例。
我会介绍
使用MRUnit来编写Hadoop MapReduce应用程序的JUnit测试
使用PowerMock和Mockito模拟静态方法
模拟其他类型中的业务逻辑(译注:也就是编写测试驱动模块)
查看模拟的业务逻辑是否被调用(译注:测试驱动模块是否运行正常)
计数器
测试用例与log4j的集成
异常处理
本文的前提是读者应该已经熟悉JUnit 4的使用。
使用MRUnit可以把测试桩输入到mapper和/或reducer中,然后在JUnit环境中判断是否通过测试。这个过程和任何JUnit测试一样,你可以调试你的代码。MRUnit中的MapReduce Driver可以测试一组Map/Reduce或者Combiner。 PipelineMapReduceDriver可以测试Map/Reduce作业工作流。目前,MRUnit还没有Partitioner对应的驱动。MRUnit使开发人员在面对Hadoop特殊的架构的时候也能进行TDD和轻量级的单元测试。
测试用例(MapTest + ReduceTest + MapReduceTest)
publicclass MapTest {
private Mapper mapper;
private MapDriver driver;
@Before
publicvoid init(){
mapper = new WordCount.Map();
driver = new MapDriver(mapper);
}
@Test
publicvoid test() throws IOException{
String line = "this is a test case for map";
driver.withInput(new LongWritable(1),new Text(line))
.withOutput(new Text("this"), new IntWritable(1))
.withOutput(new Text("is"), new IntWritable(1))
.withOutput(new Text("a"), new IntWritable(1))
.withOutput(new Text("test"), new IntWritable(1))
.withOutput(new Text("case"), new IntWritable(1))
.withOutput(new Text("for"), new IntWritable(1))
.withOutput(new Text("map"), new IntWritable(1))
.runTest();
}
}
publicclass ReduceTest {
private Reducer reducer;
private ReduceDriver driver;
@Before
publicvoid init(){
reducer = new WordCount.Reduce();
driver = new ReduceDriver(reducer);
}
@Test
publicvoid test() throws IOException{
String key = "test";
List<IntWritable> values = new ArrayList();
values.add(new IntWritable(2));
values.add(new IntWritable(3));//5
driver.withInput(new Text(key),values)
.withOutput(new Text("test"), new IntWritable(5))
.runTest();
}
}
publicclass MapReduceTest {
private Reducer reducer;
private Mapper mapper;
private MapReduceDriver driver;
@Before
publicvoid init(){
reducer = new WordCount.Reduce();
mapper = new WordCount.Map();
driver = new MapReduceDriver(mapper,reducer);
}
@Test
publicvoid test() throws IOException{
String line = "chinacache is a great CDN is it not";
driver.withInput(new LongWritable(1),new Text(line))
.withOutput(new Text("CDN"), new IntWritable(1))
.withOutput(new Text("a"), new IntWritable(1))
.withOutput(new Text("chinacache"), new IntWritable(1))
.withOutput(new Text("great"), new IntWritable(1))
.withOutput(new Text("is"), new IntWritable(2))
.withOutput(new Text("it"), new IntWritable(1))
.withOutput(new Text("not"), new IntWritable(1))
.runTest();
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
使用MRUnit,Mockito和PowerMock进行Hadoop MapReduce作业的单元测试的更多相关文章
- Hadoop MapReduce编程 API入门系列之挖掘气象数据版本2(十)
下面,是版本1. Hadoop MapReduce编程 API入门系列之挖掘气象数据版本1(一) 这篇博文,包括了,实际生产开发非常重要的,单元测试和调试代码.这里不多赘述,直接送上代码. MRUni ...
- 谈谈Hadoop MapReduce和Spark MR实现
谈谈MapReduce的概念.Hadoop MapReduce和Spark基于MR的实现 什么是MapReduce? MapReduce是一种分布式海量数据处理的编程模型,用于大规模数据集的并行运算. ...
- Hadoop MapReduce开发最佳实践(上篇)
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- Hadoop MapReduce执行过程详解(带hadoop例子)
https://my.oschina.net/itblog/blog/275294 摘要: 本文通过一个例子,详细介绍Hadoop 的 MapReduce过程. 分析MapReduce执行过程 Map ...
- hadoop MapReduce Yarn运行机制
原 Hadoop MapReduce 框架的问题 原hadoop的MapReduce框架图 从上图中可以清楚的看出原 MapReduce 程序的流程及设计思路: 首先用户程序 (JobClient) ...
- Hadoop Mapreduce分区、分组、二次排序过程详解[转]
原文地址:Hadoop Mapreduce分区.分组.二次排序过程详解[转]作者: 徐海蛟 教学用途 1.MapReduce中数据流动 (1)最简单的过程: map - reduce (2) ...
- Hadoop MapReduce编程 API入门系列之薪水统计(三十一)
不多说,直接上代码. 代码 package zhouls.bigdata.myMapReduce.SalaryCount; import java.io.IOException; import jav ...
- Hadoop MapReduce编程 API入门系列之压缩和计数器(三十)
不多说,直接上代码. Hadoop MapReduce编程 API入门系列之小文件合并(二十九) 生成的结果,作为输入源. 代码 package zhouls.bigdata.myMapReduce. ...
- Hadoop MapReduce例子-新版API多表连接Join之模仿订单配货
文章为作者原创,未经许可,禁止转载. -Sun Yat-sen University 冯兴伟 一. 项目简介: 电子商务的发展以及电商平台的多样化,类似于京东和天猫这种拥有过亿用户的在线购 ...
随机推荐
- JedisPool连接池实现难点
[http://jiangwenfeng762.iteye.com/blog/1280700] [可改进的问题] 问题是jedispool有没有办法监控状态,比如说当前连接有多少,当前idle连接 ...
- dojo grid 分页
dojo很强大,也很方便,但是缺少文档,只能看源代码,也挺好的,就是费时间... 网上找了一段代码(找不到原出处了,不好意思),也看了dojo自带的demo,放一段可以执行的页面代码这里.把ip换成自 ...
- C# 反射类型转换
/// <summary> /// 泛型类型转换 /// </summary> /// <typeparam name="T">要转换的基础类型 ...
- Windows编写bat执行文件
1:建立TXT文件 Rem nping用来测试IP地址的连通性 Rem nping --tcp -p 80 --flags rst --ttl 2 192.168.1.1 date 2:重命名为bat ...
- 减少远程ssh的延迟
今天搞了个很廉价的vps,ssh上去之后操作卡顿得不行,有时候输入一行命令后需要等五六秒才显示出来,蛋疼得不行. 然后想找一个解决方案,先是看到了mosh. 搜索了下教程,看了下说明,因为我这个廉价的 ...
- .Net连接到SAP【转载】
刚开始接触SAP了,感觉很陌生,清一色的TCode,不过里面的功能确实强大,不得不佩服啊,之前我一直是搞WinForm和WebForm的,现在能够接触到SAP那我还是想多学习一下,看了一下ABAP的语 ...
- 使用NSData来加载文件
1. Loading Data from Files and URLs // Assuming that there is a text file at /Examples/Test.txt: NSS ...
- BZOJ 1045: [HAOI2008] 糖果传递 数学
1045: [HAOI2008] 糖果传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1045 Description 有n个小朋友坐 ...
- CDOJ 第七届ACM趣味程序设计竞赛第三场(正式赛) 题解
宝贵资源 题目连接: http://acm.uestc.edu.cn/#/problem/show/1265 题意 平面上给n个点(n<=1000),要求找一个面积最小的正方形,将所有的点都囊括 ...
- Codeforces Round #306 (Div. 2) E. Brackets in Implications 构造
E. Brackets in Implications Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...