大数据技术学习笔记(12)使用MRUnit进行单元测试
沉沙 2018-10-08 来源 : 阅读 1679 评论 0

摘要:本篇教程探讨了大数据技术学习笔记(12)使用MRUnit进行单元测试,希望阅读本篇文章以后大家有所收获,帮助大家对大数据技术的理解更加深入。

本篇教程探讨了大数据技术学习笔记(12)使用MRUnit进行单元测试,希望阅读本篇文章以后大家有所收获,帮助大家对大数据技术的理解更加深入。

<

package demo.wc;

import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.io.IntWritable;
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.junit.Test;

public class MRUnitWordCount {

    @Test
    public void testMapper() throws Exception{
        //设置一个环境变量(没有可能会报错)
        System.setProperty("hadoop.home.dir", "D:\\temp\\hadoop-2.4.1\\hadoop-2.4.1");
        
        //创建一个测试对象
        WordCountMapper mapper = new WordCountMapper();
        
        //创建一个MapDriver进行单元测试
        MapDriver<LongWritable, Text, Text, IntWritable> driver = new MapDriver<>(mapper);
        
        //ָ指定Map的输入值: k1  v1
        driver.withInput(new LongWritable(1), new Text("I love Beijing"));
        
        //ָ指定Map的输出值:k2   v2  ----> 期望值
        driver.withOutput(new Text("I"), new IntWritable(1))
              .withOutput(new Text("love"), new IntWritable(1))
              .withOutput(new Text("Beijing"), new IntWritable(1));
        
        //ִ执行单元测试,对比 期望的结果和实际的结果
        driver.runTest();
    }
    
    @Test
    public void testReducer() throws Exception{
        //设置一个环境变量
        System.setProperty("hadoop.home.dir", "D:\\temp\\hadoop-2.4.1\\hadoop-2.4.1");
        
        //创建一个测试对象
        WordCountReducer reducer = new WordCountReducer();
        
        //创建一个ReduceDriver进行单元测试
        ReduceDriver<Text, IntWritable, Text, IntWritable> driver = new ReduceDriver<>(reducer);
        
        //构造v3:List
        List<IntWritable> value3 = new ArrayList<>();
        value3.add(new IntWritable(1));
        value3.add(new IntWritable(1));
        value3.add(new IntWritable(1));
        
        
        //指定reducer的输入
        driver.withInput(new Text("Beijing"), value3);
        
        
        //指定reducer的输出
        driver.withOutput(new Text("Beijing"), new IntWritable(3));
        
        
        //ִ执行测试
        driver.runTest();
    }
    
    @Test
    public void testJob() throws Exception{
        //设置一个环境变量
        System.setProperty("hadoop.home.dir", "D:\\temp\\hadoop-2.4.1\\hadoop-2.4.1");
        
        //创建一个测试对象
        WordCountMapper mapper = new WordCountMapper();        
        WordCountReducer reducer = new WordCountReducer();        
        
        //创建一个Driver
        //MapReduceDriver<K1, V1, K2, V2, K4, V4>
        MapReduceDriver<LongWritable, Text, Text, IntWritable, Text, IntWritable>
                driver = new MapReduceDriver<>(mapper,reducer);
        
        //指定Map输入的数据
        driver.withInput(new LongWritable(1), new Text("I love Beijing"))
              .withInput(new LongWritable(4), new Text("I love China"))
              .withInput(new LongWritable(7), new Text("Beijing is the capital of China"));
        
        //ָ指定Reducer的输出
//        driver.withOutput(new Text("I"), new IntWritable(2))
//              .withOutput(new Text("love"), new IntWritable(2))
//              .withOutput(new Text("Beijing"), new IntWritable(2))
//              .withOutput(new Text("China"), new IntWritable(2))
//              .withOutput(new Text("is"), new IntWritable(1))
//              .withOutput(new Text("the"), new IntWritable(1))
//              .withOutput(new Text("capital"), new IntWritable(1))
//              .withOutput(new Text("of"), new IntWritable(1));
        
        //指定Reducer的输出(默认排序规则)
        driver.withOutput(new Text("Beijing"), new IntWritable(2))
              .withOutput(new Text("China"), new IntWritable(2))
              .withOutput(new Text("I"), new IntWritable(2))
              .withOutput(new Text("capital"), new IntWritable(1))
              .withOutput(new Text("is"), new IntWritable(1))
              .withOutput(new Text("love"), new IntWritable(2))
              .withOutput(new Text("of"), new IntWritable(1))
              .withOutput(new Text("the"), new IntWritable(1));
        
        driver.runTest();
    }
}    

本文由职坐标整理发布,学习更多的大数据技术相关知识,请关注职坐标大技术云计算大技术技术频道!

本文由 @沉沙 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved