6.3 MRUnit写Mapper和Reduce的单元测试
1.1 MRUnit写单元测试
作用:一旦MapReduce项目提交到集群之后,若是出现问题是很难定位和修改的,只能通过打印日志的方式进行筛选。又如果数据和项目较大时,修改起来则更加麻烦。所以,在将MapReduce项目提交到集群上之前,我们需要先对其进行单元测试。单元测试需要用到mrunit库,这个库中包含MapDriver、ReduceDriver、MapReduceDriver,可以通过三个类,输入简单的数据进行测试map和reduce的逻辑是否正确。
1.1.1 Mapper单元测试
(1)包含测试驱动库mrunit
在pom.xml文件中加入mrunit的依赖,保存会自动下载mrunit库。
<dependency>
<groupId>org.apache.mrunit</groupId>
<artifactId>mrunit</artifactId>
<version>1.1.0</version>
<!--<scope>test</scope>-->
<!--不加导包可能失败-->
<classifier>hadoop2</classifier>
</dependency>
(2)TemperatureMapper类
package Temperature; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
//import org.apache.hadoop.mapred.MapReduceBase;
//import org.apache.hadoop.mapred.Mapper;
//import org.apache.hadoop.mapred.OutputCollector;
//import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
//import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; //public class TemperatureMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
public class TemperatureMapper extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> { private static final int MISSING=9999;
public void map(LongWritable longWritable, Text text, OutputCollector<Text, IntWritable> outputCollector, Reporter reporter) throws IOException {
String line=text.toString();
String year=line.substring(15,19);
int airTemperture=MISSING;
if(line.charAt(87)=='+'){
airTemperture=Integer.parseInt(line.substring(88,92));
}else{
airTemperture=Integer.parseInt(line.substring(87,92));
}
String quality=line.substring(92,93);
if(airTemperture!=MISSING&&quality.matches("[01459]")){
outputCollector.collect(new Text(year),new IntWritable(airTemperture));
}
}
}
(3)maper测试类
package Temperature; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mrunit.MapDriver;
import org.junit.Test; import java.io.IOException; public class TemperatureMapperTest { @Test//注解表示为测试类
public void TestMapper() throws IOException,InterruptedException{
Text value=new Text("0057332130999991950010103004+51317+028783FM-12+017199999V0203201N00721004501CN0100001N9-01281-01391102681");//一行测试数据
new MapDriver<LongWritable, Text, Text, IntWritable>()
.withMapper(new TemperatureMapper())//传入要测试mapper
.withInput(new LongWritable(0), value)//输入值
.withOutput(new Text("1950"), new IntWritable(-128))//验证输出值是否这个,不是则测试出错
.runTest();//开始测试
}
}
(4)执行测试
右键TemperatureMapperTest.java,单击选项run TemperatureMapperTest。如果没有run选项,需要单击文件夹,点击Create run configuration按钮,创建run测试。再次右击TemperatureMapperTest.java就会出现run按钮。

单击run按钮就会运行测试程序,成功会显示tests passed

如果将-128改为-118,在运行测试,就会出现test failed

java.lang.AssertionError: 1 Error(s): (Missing expected output (1950, -118) at position 0, got (1950, -128).)
(5)新旧mapper
新旧Mapper和测试类型import要匹配,否则会出现错误。
旧的mapper
import org.apache.hadoop.mapred.Mapper;
public class TemperatureMapper extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
旧的测试Driver
import org.apache.hadoop.mrunit.MapDriver;
新的mapper
import org.apache.hadoop.mapreduce.Mapper;
public class TemperatureMapperNew extends Mapper<LongWritable, Text, Text, IntWritable> {
新的测试Driver
import org.apache.hadoop.mrunit.mapreduce.MapDriver;
(6)@Test的作用
@Test的使用是该方法可以不用main方法调用就可以测试出运行结果,是一种测试方法,一般函数都需要有main方法调用才能执行,注意被测试的方法必须是public修饰的。
1.1.2 Reduce单元测试
Reduce测试也需要依赖mrunit的库,
(1)reduce类
package Temperature; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter; import java.io.IOException;
import java.util.Iterator; public class MaxTempertureReduce extends MapReduceBase implements Reducer<Text, IntWritable,Text,IntWritable> {
public void reduce(Text text, Iterator<IntWritable> iterator, OutputCollector<Text, IntWritable> outputCollector, Reporter reporter) throws IOException {
int MaxValue = Integer.MIN_VALUE;
while (iterator.hasNext()) {
MaxValue = Math.max(MaxValue, iterator.next().get());
}
outputCollector.collect(text, new IntWritable(MaxValue));
}
}
(1)Reduce测试类
package Temperature;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mrunit.ReduceDriver;
import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
public class MaxtemperatureReduceTest {
@Test
public void ReduceTest() throws IOException{
new ReduceDriver<Text, IntWritable, Text, IntWritable>()
.withReducer(new MaxTempertureReduce())
.withInput(new Text("1950"), Arrays.asList(new IntWritable(10),new IntWritable(5)))
.withOutput(new Text("1950"),new IntWritable(10) )
.runTest();
}
}
自己开发了一个股票智能分析软件,功能很强大,需要的点击下面的链接获取:
https://www.cnblogs.com/bclshuai/p/11380657.html
6.3 MRUnit写Mapper和Reduce的单元测试的更多相关文章
- Hadoop 2:Mapper和Reduce
Hadoop 2:Mapper和Reduce Understanding and Practicing Hadoop Mapper and Reduce 1 Mapper过程 Hadoop将输入数据划 ...
- SpringBoot图文教程11—从此不写mapper文件「SpringBoot集成MybatisPlus」
有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+ ...
- java 写一个 map reduce 矩阵相乘的案例
1.写一个工具类用来生成 map reduce 实验 所需 input 文件 下面两个是原始文件 matrix1.txt 1 2 -2 0 3 3 4 -3 -2 0 2 3 5 3 -1 2 -4 ...
- mybatis写mapper文件注意事项(转)
原文链接:http://wksandy.iteye.com/blog/1443133 xml中某些特殊符号作为内容信息时需要做转义,否则会对文件的合法性和使用造成影响 < < > & ...
- mybatis_mybatis写mapper文件注意事项
xml中某些特殊符号作为内容信息时需要做转义,否则会对文件的合法性和使用造成影响 < < > > & & ' ' " " ...
- 如何写好、管好单元测试?基于Roslyn+CI分析单元测试,严控产品提测质量
上一篇文章中,我们谈到了通过Roslyn进行代码分析,通过自定义代码扫描规则,将有问题的代码.不符合编码规则的代码扫描出来,禁止签入,提升团队的代码质量. .NET Core技术研究-通过Roslyn ...
- 如何写好测试用例以及go单元测试工具testify简单介绍
背景 最近在工作和业余开源贡献中,和单元测试接触的比较频繁.但是在这两个场景之下写出来的单元测试貌似不太一样,即便是同一个代码场景,今天写出来的单元测试和昨天写的也不是很一样,我感受到了对于单元测 ...
- Hadoop 2.x从零基础到挑战百万年薪第一季
鉴于目前大数据Hadoop 2.x被企业广泛使用,在实际的企业项目中需要更加深入的灵活运用,并且Hadoop 2.x是大数据平台处理 的框架的基石,尤其在海量数据的存储HDFS.分布式资源管理和任务调 ...
- java写hadoop全局排序
前言: 一直不会用java,都是streaming的方式用C或者python写mapper或者reducer的可执行程序.但是有些情况,如全排序等等用streaming的方式往往不好处理,于是乎用原生 ...
随机推荐
- 安装mysql采坑记录
安装之前彻底卸载之前的mysql,再次安装,初始化数据库那一步失败. 再次彻底卸载mysql,把原先的安装路径的文件夹删除,文件夹路径:C:\ProgramData,再次安装,成功. 总结:重装mys ...
- js中undefined的几种情况
1.变量声明且没有赋值: 2.获取对象中不存在的属性时: 3.函数需要实参,但是调用时没有传值,形参是undefined: 4.函数调用没有返回值或者return后没有数据,接收函数返回的变量是und ...
- JQuery的事件处理、Jason
事件的处理: <body> <div id="aa" style="width:100px; height:100px; background-colo ...
- 5.创建执行线程的方式之三 :实现Callable 接口
Callable 接口 一.Java 5.0 在 java.util.concurrent 提供了 一个新的创建执行线程的方式(之前有继承Thread 和 实现Runnable):Callable 接 ...
- swore tcp服务学习
TcpServer.php <?php /** * Created by PhpStorm. * User: mac * Date: 2019/9/13 * Time: 20:33 */ cla ...
- css-两个div并排,左边宽度固定右边自适应的布局 的实现方法
<div class= "container"> <div class="left"></div> <div clas ...
- C语言Makefile文件制作
本文摘抄自“跟我一起写Makefile ”,只是原文中我自己感觉比较精要的一部分,并且只针对C语言,使用GCC编译器. 原文请看这里:http://wiki.ubuntu.org.cn/%E8%B7% ...
- 搭建nginx文件服务器
一.安装nginx服务 apt install nginx 二.修改nginx配置文件 cd /etc/nginx/conf.d/ vim download_server.conf server { ...
- Maven打包Web项目成war包——4
1. 需要安装maven 的war插件支持 在pom.xml里面配置war插件 2. 运行命令: mvn clean package 3.部署到Tomcat下面发布,并访问!
- 开源框架---通过Bazel编译使用tensorflow c++ API 记录
开源框架---通过Bazel编译使用tensorflow c++ API 记录 tensorflow python API,在python中借用pip安装tensorflow,真的很方便,几句指令就完 ...