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的单元测试的更多相关文章

  1. Hadoop 2:Mapper和Reduce

    Hadoop 2:Mapper和Reduce Understanding and Practicing Hadoop Mapper and Reduce 1 Mapper过程 Hadoop将输入数据划 ...

  2. SpringBoot图文教程11—从此不写mapper文件「SpringBoot集成MybatisPlus」

    有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+ ...

  3. 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 ...

  4. mybatis写mapper文件注意事项(转)

    原文链接:http://wksandy.iteye.com/blog/1443133 xml中某些特殊符号作为内容信息时需要做转义,否则会对文件的合法性和使用造成影响 < < > & ...

  5. mybatis_mybatis写mapper文件注意事项

    xml中某些特殊符号作为内容信息时需要做转义,否则会对文件的合法性和使用造成影响 < < > > & & &apos; ' " " ...

  6. 如何写好、管好单元测试?基于Roslyn+CI分析单元测试,严控产品提测质量

    上一篇文章中,我们谈到了通过Roslyn进行代码分析,通过自定义代码扫描规则,将有问题的代码.不符合编码规则的代码扫描出来,禁止签入,提升团队的代码质量. .NET Core技术研究-通过Roslyn ...

  7. 如何写好测试用例以及go单元测试工具testify简单介绍

    背景 ​ 最近在工作和业余开源贡献中,和单元测试接触的比较频繁.但是在这两个场景之下写出来的单元测试貌似不太一样,即便是同一个代码场景,今天写出来的单元测试和昨天写的也不是很一样,我感受到了对于单元测 ...

  8. Hadoop 2.x从零基础到挑战百万年薪第一季

    鉴于目前大数据Hadoop 2.x被企业广泛使用,在实际的企业项目中需要更加深入的灵活运用,并且Hadoop 2.x是大数据平台处理 的框架的基石,尤其在海量数据的存储HDFS.分布式资源管理和任务调 ...

  9. java写hadoop全局排序

    前言: 一直不会用java,都是streaming的方式用C或者python写mapper或者reducer的可执行程序.但是有些情况,如全排序等等用streaming的方式往往不好处理,于是乎用原生 ...

随机推荐

  1. 【原创】大叔经验分享(89)docker启动openjdk执行jmap报错

    docker启动openjdk后,可以查看进程 # docker exec -it XXX jps 10 XXX.jar 可见启动的java进程id一直为10,然后可以执行jvm命令,比如 # doc ...

  2. MongoDB知识小结

    一.术语 RDBMS MongoDB 数据库 数据库 表格 集合 行 文档 列 字段 表联合 嵌套文档 主键 主键 (MongoDB 提供了 key 为 _id ) 数据库 数据库名可以是满足以下条件 ...

  3. Django-2.0 汉化

    1.语言 LANGUAGE_CODE = 'zh-hans' 2.时区 TIME_ZONE = 'Asia/Shanghai' 3.字段名汉化 models.CharFielf(verbose_nam ...

  4. SAP Kyma(Extension Factory on SAP Cloud Platform)的架构简介

    SAP kyma主要分三大块组成: (1) Application connector simplify and securely connect external systems to Kyma a ...

  5. 【问题】XShell连接不上Debian root用户

    类似文章:https://www.lianst.com/3231.html 修改此文件 重启ssh服务 ssh restart有问题,换一条命令OK 你的Linux发行版可能不一样,针对CentOS参 ...

  6. ORACLE SQL性能优化汇总

    ORACLE SQL语句共享 Oracle SQL语句具备共享特性,为了不让ORACLE数据库重复解析相同的简单单表SQL语句,ORACLE在SGA系统共享区域内SBP共享池内存放的SQL语句将被所有 ...

  7. k2系列-安装篇

    K2介绍: K2是基于BPM的流程开发平台,它支持在net开发环境/visio/moss等不同环境下进行流程开发. K2本身部署简单,操作灵活,非常适合大中型企业流程开发和部署. K2安装步骤: 首先 ...

  8. Codeforces 567D - One-Dimensional Battle Ships - [树状数组+二分]

    题目链接:https://codeforces.com/problemset/problem/567/D 题意: 在一个 $1 \times n$ 的网格上,初始摆放着 $k$ 只船,每只船的长度均为 ...

  9. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

  10. 笔记本电脑安装jupyterthemes

    上午准备在老笔记本上也装上jupyter themes,竟然遇到一堆问题: 首先直接 pip install jupyterthemes 参考:https://blog.csdn.net/Jinlon ...