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. CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败

    今天在使用 C# 操作 Excel 时,一直在报错误: 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下 ...

  2. 【原创】大叔经验分享(87)marathon重启应用过程服务不可用

    marathon提供多种健康检查方式 常用的有TCP和HTTP, TCP检查端口是否存在,存在则认为实例健康: HTTP检查指定URL的HTTP返回码,返回码正常(2xx.3xx)则认为实例健康: 这 ...

  3. kong网关命令(一)

    上次在虚拟机里安装kong网关后,因为版本(1.4)太高,目前Kong Dashboard无法支持, 后续发现Git上有个开源工具Kong admin ui,下载源码并部署到NGINX. 但是发现使用 ...

  4. springboot启动流程(十)springboot自动配置机制

    所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 正文 在第七篇文章中我们了解到,refresh过程将会调用ConfigurationClass ...

  5. 【小知识点】如何通过jq获取textarea的值

    textarea和input本质差不多,只不过input是一行文本,textarea是多行文本. 所以获取textarea的值的方法和获取input的值的方法是一致的, $('textarea').v ...

  6. kbmMW 5.10.01试用报告

    1.FileClient.SameFile 调用这个方法,当本地文件不存在时,会一直等待.跟踪代码,发现: function TkbmMWCustomFileClient.SameFileEx(Loc ...

  7. MacOS文本编辑无法打不开GB18030

      不要直接双击打开   而是 打开sublime text或者其他文本编辑后,从软件里面的open选型打开 

  8. 03 WIndows编程——手绘函数调用过程

    源码 #include<Windows.h> #include<stdio.h> int MessageBoxPrint(char *szFormat, ...); int W ...

  9. xargs 使用详解

    参考转载:https://www.cnblogs.com/f-ck-need-u/p/5925923.html#auto_id_12 xargs 作用:将管道传输过来的stdin进行处理(分割.分批) ...

  10. python爬虫伪装技术应用

    版权声明:本文为博主原创文章,转载 请注明出处: https://blog.csdn.net/sc2079/article/details/82423865 -写在前面 本篇博客主要是爬虫伪装技术的应 ...