以前用java写MR程序总不习惯写单元测试,就是查错也只是在小规模数据上跑一下程序。昨天工作时,遇到一个bug,查了好久也查出来。估计是业务逻辑上的错误。后来没办法,只好写了个单元测试,一步步跟踪,瞬间找到问题所在。所以说,工作中还是要勤快些。

 import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mrunit.mapreduce.MapDriver;
import org.apache.hadoop.mrunit.mapreduce.MapReduceDriver;
import org.apache.hadoop.mrunit.mapreduce.ReduceDriver;
import org.apache.hadoop.mrunit.types.Pair;
import org.junit.Before;
import org.junit.Test;
import com.wanda.predict.GenerateCustomerNatureFeature.NatureFeatureMappper;
import com.wanda.predict.GenerateCustomerNatureFeature.NatureReducer;
import com.wanda.predict.pojo.Settings; /**
* MapReduce 单元测试的模板 , 依赖于junit环境(junit.jar), mrunit.jar , mockito.jar
*
*/
public class MapperReducerUnitTest {
// 一些设置,与正常的mr程序一样,不过这里主要是加载一些信息。性能优化之类的就不要在单元测试里设置了。
Configuration conf = new Configuration();
//Map.class 的测试驱动类
MapDriver<LongWritable, Text, Text, Text> mapDriver;
//Reduce.class 的测试驱动类
ReduceDriver<Text, Text, Text, Text> reduceDriver;
//Map.calss 、 Reduce.class转接到一起的流程测试驱动
MapReduceDriver<LongWritable, Text, Text, Text, Text, Text> mapReduceDriver; @Before
public void setUp() { //测试mapreduce
NatureFeatureMappper mapper = new NatureFeatureMappper();
NatureReducer reducer = new NatureReducer();
//添加要测试的map类
mapDriver = MapDriver.newMapDriver(mapper);
//添加要测试的reduce类
reduceDriver = ReduceDriver.newReduceDriver(reducer);
//添加map类和reduce类
mapReduceDriver = MapReduceDriver.newMapReduceDriver(mapper, reducer); //测试配置参数
conf.setInt(Settings.TestDataSize.getName(), 1);
conf.setInt(Settings.TrainDataSize.getName(), 6);
//driver之间是独立的,谁用到谁就设置conf
reduceDriver.setConfiguration(conf);
mapReduceDriver.setConfiguration(conf);
} @Test
public void testMapper() throws IOException {
mapDriver.withInput(new LongWritable(), new Text("map的输入"));
mapDriver.withOutput(new Text("期望的key"), new Text("期望的value")); //打印实际结果
List<Pair<Text , Text>> result = mapDriver.run();
for(Pair<Text , Text> kv : result){
System.out.println("mapper : " + kv.getFirst());
System.out.println("mapper : " + kv.getSecond());
}
//进行case测试,对比输入输出结果
mapDriver.runTest();
} @Test
public void testReducer() throws IOException {
List<Text> values = new ArrayList<Text>();
values.add(new Text("输入"));
reduceDriver.withInput(new Text("输入"), values);
reduceDriver.withOutput(new Text("期望的输出"), new Text("期望的输出"));
reduceDriver.runTest();
} @Test
public void testMapperReducer() throws IOException {
mapReduceDriver.withInput(new LongWritable(), new Text("输入"));
mapReduceDriver.withOutput(new Text("期望的输出"), new Text("期望的输出"));
//打印实际结果
List<Pair<Text, Text>> list = mapReduceDriver.run();
System.out.println("mapreducedriver size:" + list.size());
for(Pair<Text , Text> lst : list){
System.out.println(lst.getFirst());
System.out.println(lst.getSecond());
}
//进行case测试,对比输入输出结果
mapReduceDriver.runTest();
} @Test
public void testMapperCount() throws IOException {
mapDriver.withInput(new LongWritable(), new Text("输入"));
mapDriver.withOutput(new Text("期望的输出"), new Text("期望的输出"));
mapDriver.runTest();
//判断 map中的counter值是否与期望的相同
assertEquals("Expected 1 counter increment", 1, mapDriver.getCounters().findCounter("data", "suc").getValue());
}
}

MapReduce Unit Test的更多相关文章

  1. MapReduce和Spark写入Hbase多表总结

    作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 大家都知道用mapreduce或者spark写入已知的hbase中的表时,直接在mapreduc ...

  2. mapReduce编程之Recommender System

    1 协同过滤算法 协同过滤算法是现在推荐系统的一种常用算法.分为user-CF和item-CF. 本文的电影推荐系统使用的是item-CF,主要是由于用户数远远大于电影数,构建矩阵的代价更小:另外,电 ...

  3. Hadoop官方文档翻译——MapReduce Tutorial

    MapReduce Tutorial(个人指导) Purpose(目的) Prerequisites(必备条件) Overview(综述) Inputs and Outputs(输入输出) MapRe ...

  4. Hadoop 学习笔记3 Develping MapReduce

    小笔记: Mavon是一种项目管理工具,通过xml配置来设置项目信息. Mavon POM(project of model). Steps: 1. set up and configure the ...

  5. mapReduce编程之google pageRank

    1 pagerank算法介绍 1.1 pagerank的假设 数量假设:每个网页都会给它的链接网页投票,假设这个网页有n个链接,则该网页给每个链接平分投1/n票. 质量假设:一个网页的pagerank ...

  6. hadoop权威指南 chapter2 MapReduce

    MapReduce MapReduce is a programming model for data processing. The model is simple, yet not too sim ...

  7. Hadoop权威指南:MapReduce应用开发

    Hadoop权威指南:MapReduce应用开发 [TOC] 一般流程 编写map函数和reduce函数 编写驱动程序运行作业 用于配置的API Hadoop中的组件是通过Hadoop自己的配置API ...

  8. Hadoop Mapreduce 参数 (二)

    MergeManagerImpl 类 内存参数计算 maxInMemCopyUse 位于构造函数中 final float maxInMemCopyUse = jobConf.getFloat(MRJ ...

  9. MapReduce C++ Library

    MapReduce C++ Library for single-machine, multicore applications Distributed and scalable computing ...

随机推荐

  1. JavaScript实现网页安全登录(转)

    现在很多商业网站的用户登录都是明码传输的,而一般用户又习惯于所有帐号使用相同的密码来保存,甚至很多人使用的密码和自己的银行帐号都一样哦!所 以嘛还是有一定的安全隐患的,YAHOO的免费邮箱登录使用了M ...

  2. 关于VS2013的安装遇到的问题

    老师突然说实验一需要用代码实现,我之前配置的cocos的编程环境是cocos+VS2013,是很稳定的 但是,我安装unity5.5的时候,不小心选择了顺带安装了VS2015,就等于我电脑里面有了两个 ...

  3. go * 和 &的区别

    类型 *T 是指向 T 类型值的指针.其零值为 nil . var p *int & 操作符会生成一个指向其操作数的指针. i := 42 p = &i * 操作符表示指针指向的底层值 ...

  4. shell 中各种符号的含义

    http://yesjavame.iteye.com/blog/1062405 http://blog.csdn.net/taiyang1987912/article/details/39551385

  5. Spring_day04--Spring框架整合hibernate框架

    Spring框架整合hibernate框架 1 把hibernate核心配置文件中配置数据库信息,把数据库信息在spring进行配置 2 把hibernate里面的sessionFactory创建交给 ...

  6. shell脚本学习总结04--终端信息的获取和设置

    tput tput 命令将通过 terminfo 数据库对您的终端会话进行初始化和操作.通过使用 tput,您可以更改几项终端功能,如移动或更改光标.更改文本属性,以及清除终端屏幕的特定区域. 文本属 ...

  7. index封装

    就是求元素在父级当中的位置: 思路: <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  8. js如何遍历并取出对象的属性名?

    js如何遍历并取出对象的属性名? dataObj = {name : su,age : 26,height : 18cm }; for(var st in dataObj) {console.dir( ...

  9. 基于k8s的promethus监控

    没有监控 就没有眼睛. 除了k8s的基本监控外(pod运行状况.占用内存.cpu).为了对微服务项目中的(1)各种参数线程池.QPS.RT.业务指标(2)系统负载.thread.mem.class.t ...

  10. 170328、Maven+SpringMVC+Dubbo 简单的入门demo配置

    之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程序猿来说,真是一个好消息.最近有时间了,打算做一个demo把dubbo在本地跑起来先. 先copy一段dubbo ...