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();
}
}

大数据笔记(十二)——使用MRUnit进行单元测试的更多相关文章

  1. 大数据笔记(二十四)——Scala面向对象编程实例

    ===================== Scala语言的面向对象编程 ======================== 一.面向对象的基本概念:把数据和操作数据的方法放到一起,作为一个整体(类 c ...

  2. 大数据笔记(二十九)——RDD简介、特性及常用算子

    1.什么是RDD? 最核心 (*)弹性分布式数据集,Resilent distributed DataSet (*)Spark中数据的基本抽象 (*)结合源码,查看RDD的概念 RDD属性 * Int ...

  3. 大数据笔记(二十六)——Scala语言的高级特性

    ===================== Scala语言的高级特性 ========================一.Scala的集合 1.可变集合mutable 不可变集合immutable / ...

  4. 大数据笔记(二十五)——Scala函数式编程

    ===================== Scala函数式编程 ======================== 一.Scala中的函数 (*) 函数是Scala中的头等公民,就和数字一样,可以在变 ...

  5. 大数据笔记(二十二)——大数据实时计算框架Storm

    一. 1.对比:离线计算和实时计算 离线计算:MapReduce,批量处理(Sqoop-->HDFS--> MR ---> HDFS) 实时计算:Storm和Spark Sparki ...

  6. 大数据笔记(二十)——NoSQL数据库之MemCached

    一.为什么要把数据存入内存? 1.原因:快2.常见的内存数据库 (*)MemCached:看成Redis的前身,严格来说Memcached的不能叫数据库,原因:不支持持久化 (*)Redis:内存数据 ...

  7. 大数据笔记(二十八)——执行Spark任务、开发Spark WordCount程序

    一.执行Spark任务: 客户端 1.Spark Submit工具:提交Spark的任务(jar文件) (*)spark提供的用于提交Spark任务工具 (*)example:/root/traini ...

  8. 大数据笔记(二十一)——NoSQL数据库之Redis

    一.Redis内存数据库 一个key-value存储系统,支持存储的value包括string(字符串).list(链表).set(集合).zset(sorted set--有序集合)和hash(哈希 ...

  9. 大数据笔记(二)——Apache Hadoop的体系结构

    一.分布式存储 NameNode(名称节点) 1.维护HDFS文件系统,是HDFS的主节点. 2.接收客户端的请求:上传.下载文件.创建目录等. 3.记录客户端操作的日志(edits文件),保存了HD ...

  10. 大数据笔记(二十七)——Spark Core简介及安装配置

    1.Spark Core: 类似MapReduce 核心:RDD 2.Spark SQL: 类似Hive,支持SQL 3.Spark Streaming:类似Storm =============== ...

随机推荐

  1. Springboot2.x集成单节点Redis

    Springboot2.x集成单节点Redis 说明 在Springboot 1.x版本中,默认使用Jedis客户端来操作Redis,而在Springboot 2.x 版本中,默认使用Lettuce客 ...

  2. [DS+Algo] 007 希尔排序及其代码实现

    步骤 将数组列在一个表(一行多列)中,按特定的步长进行插入排序 步长从 length/2 到 1,每次除 2 将数组转换至表是为了更好地理解这算法,算法本身还是使用数组进行排序 算法性能 (根据步长序 ...

  3. [Web 前端] 033 Vue 的简单使用

    目录 0. 方便起见,定个轮廓 1. v-model 举例 2. v-for 举例 3. v-if 举例 4. 事件绑定 举例 5. v-show 举例 0. 方便起见,定个轮廓 不妨记下方的程序为 ...

  4. [Jupyter Notebook] 01 这么多快捷键,我可顶不住!先记个八成吧

    0. 一些说明 为了入门 Python3 安装了 Anaconda,它集成了 Jupyter Notebook 1. 调出快捷键表 打开 Jupyter Notebook,新建一个 Python3(我 ...

  5. Skiing POJ 3037 很奇怪的最短路问题

    Skiing POJ 3037 很奇怪的最短路问题 题意 题意:你在一个R*C网格的左上角,现在问你从左上角走到右下角需要的最少时间.其中网格中的任意两点的时间花费可以计算出来. 解题思路 这个需要发 ...

  6. Java第三阶段复习

    Java第三阶段复习: 1. Spring 1. IOC: 定义:Inverse Of Controller:反转控制,将bean对象的创建和对象之间的关联关系的维护由原来我们自己创建.自己维护反转给 ...

  7. 2018-8-10-VisualStudio-修改配色

    title author date CreateTime categories VisualStudio 修改配色 lindexi 2018-08-10 19:16:52 +0800 2018-2-1 ...

  8. 二、在 ASP.NET Core 中使用 SignalR之类库

    一.前段代码: @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="view ...

  9. 1Mbps代表每秒传输1,000,000位(bit

    1Mbps代表每秒传输1,000,000位(bit

  10. 表达式,数据类型和变量(Expressions,Data Types & Variables)

    (一)表达式: 1)4+4就是表达式,它是程序中最基本的编程指令:表达式包含一个值(4)和操作符号(+),然后就会计算出一个单独的值; 2)一个单独的值没有包含操作符号也可以叫表达式,尽管它只计算它本 ...