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. 第九周课程总结&实验报告七

    实验任务详情: 完成火车站售票程序的模拟. 要求: (1)总票数1000张: (2)10个窗口同时开始卖票: (3)卖票过程延时1秒钟: (4)不能出现一票多卖或卖出负数号票的情况. package ...

  2. Linear Discriminant Analysis

    Suppose that we model each class density as multivariate Gaussian, in practice we do not know the pa ...

  3. UESTC-1059 秋实大哥与小朋友(离散化+线段树)

    秋实大哥与小朋友 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  ...

  4. P2523 [HAOI2011]Problem c

    传送门 先考虑如何判断无解,设 $sum[i]$ 表示确定的人中,编号大于 $i$ 的人的人数 如果 $sum[i]>n-i+1$ 则无解,进一步考虑设 $f[i][j]$ 表示当前确定完编号大 ...

  5. ubuntu install xsltproc docbook-xsl docbook-xml

    问题一: $ makexsltproc --output phtml/ param.xsl ./pxml/mainbook.xmlmake: xsltproc: Command not foundma ...

  6. 解决nodejs环境下端口号被占用的方法

    假设被占用的端口号是8081 1.进入cmd命令窗口 输入netstat -ano|findstr "8081" cmd窗口给我的信息尾部有一个和端口8081对应的PID值 '51 ...

  7. github配置及使用

    安装git 对于linux系统,不同发行版本的安装方法不一样,请参考https://git-scm.com/download/linux.以ubuntu为例: sudo add-apt-reposit ...

  8. Sql中使用With创建多张临时表

    CREATE PROC [dbo].[sp_VisitCount] ( @count INT ) AS BEGIN DECLARE @current DATETIME SET @current=GET ...

  9. N^2取N

    序列合并 有两个长度都是N的序列A和B,在A和B中各取一个数相加可以得到N^2个和,求这N^2个和中最小的N个. 先把A B排序 然后pushA[1]+B[i](1<=i<=n)每次取出一 ...

  10. Composer\Downloader\TransportException ... Failed to enable crypto,failed to open stream: operation failed

    failed to open stream: operation failed 错误详细信息: [Composer\Downloader\TransportException] The "h ...