博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MRUNIT hadoop逐步调试工具!
阅读量:6984 次
发布时间:2019-06-27

本文共 2595 字,大约阅读时间需要 8 分钟。

hot3.png

MRUNIT hadoop MapReduce逐步调试工具!

MRUnit简介

MRUnit是一款由Couldera公司开发的专门针对Hadoop中编写MapReduce单元测试的框架。可以用MapDriver单独测试
Map,用ReduceDriver单独测试Reduce,用MapReduceDriver测试MapReduce作业。
实战
我们将利用MRUnit对本系列上篇文章MapReduce基本编程中的字数统计功能进行单元测试。
加入MRUnit依赖<dependency>
<groupId>com.cloudera.hadoop</groupId>
<artifactId>hadoop-mrunit</artifactId>
<version>0.20.2-320</version>
<scope>test</scope>
</dependency>
单独测试Map
public class WordCountMapperTest {
private Mapper mapper;
private MapDriver driver;
@Before
public void init(){
mapper = new WordCountMapper();
driver = new MapDriver(mapper);
}
@Test
public void test() throws IOException{
String line = "Taobao is a great website";
driver.withInput(null,new Text(line))
.withOutput(new Text("Taobao"),new IntWritable(1))
.withOutput(new Text("is"), new IntWritable(1))
.withOutput(new Text("a"), new IntWritable(1))
.withOutput(new Text("great"), new IntWritable(1))
.withOutput(new Text("website"), new IntWritable(1))
.runTest();
}
}
上面的例子通过MapDriver的withInput和withOutput组织map函数的输入键值和期待的输出键值,通过runTest方法运行
作业,测试Map函数。测试运行通过。
单独测试Reduce
public class WordCountReducerTest {
private Reducer reducer;
private ReduceDriver driver;
@Before
public void init(){
reducer = new WordCountReducer();
driver = new ReduceDriver(reducer);
}
@Test
public void test() throws IOException{
String key = "taobao";
List values = new ArrayList();

values.add(new IntWritable(2));

values.add(new IntWritable(3));
driver.withInput(new Text("taobao"), values)
.withOutput(new Text("taobao"), new IntWritable(5))
.runTest();
}
}
上面的例子的测试Map函数的写法类似,测试reduce函数,
因为reduce函数实现相加功能,因此我们假设输入为<taobao,[2,3]>,
则期待结果应该为<taobao,5>.测试运行通过。
测试MapReduce
public class WordCountTest {
private Mapper mapper;
private Reducer reducer;
private MapReduceDriver driver;
@Before
public void init(){
mapper = new WordCountMapper();
reducer = new WordCountReducer();
driver = new MapReduceDriver(mapper,reducer);
}
@Test
public void test() throws RuntimeException, IOException{
String line = "Taobao is a great website, is it not?";
driver.withInput("",new Text(line))
.withOutput(new Text("Taobao"),new IntWritable(1))
.withOutput(new Text("a"),new IntWritable(1))
.withOutput(new Text("great"),new IntWritable(1))
.withOutput(new Text("is"),new IntWritable(2))
.withOutput(new Text("it"),new IntWritable(1))
.withOutput(new Text("not"),new IntWritable(1))
.withOutput(new Text("website"),new IntWritable(1))
.runTest();
}
}
这次我们测试MapReduce的作业,通过MapReduceDriver的withInput构造map函数的输入键值,通过withOutput构造
reduce函数的输出键值。来测试这个字数统计功能,这次运行测试时抛出了异常,测试没有通过但没有详细junit异常信
息,在控制台显示

转载于:https://my.oschina.net/u/1169079/blog/272701

你可能感兴趣的文章
[UML]UML系列——包图Package
查看>>
TYVJ 矩阵取数 Label:高精度+dp
查看>>
Google Code Jam 2014 Round 1 A:Problem C. Proper Shuffle
查看>>
YYHS-魏传之长坂逆袭(梦回三国系列T1)
查看>>
jquery 获取Select option 选择的Text和Value
查看>>
后海日记(8)
查看>>
百度云满速下载(转)
查看>>
HTML5学习之二:HTML5中的表单2
查看>>
CSS盒模型及边距问题
查看>>
UVa 167(八皇后)、POJ2258 The Settlers of Catan——记两个简单回溯搜索
查看>>
AlexNet 网络详解及Tensorflow实现源码
查看>>
day07 -文件的基本操作
查看>>
关于BIO | NIO | AIO的讨论
查看>>
linux 重命名文件和文件夹
查看>>
java基础回顾
查看>>
Java语法基础-序列化
查看>>
docker 安装 RabbitMQ
查看>>
阿里巴巴开源技术汇总:115个软件(一)
查看>>
ios开发之系统信息
查看>>
遮罩效果的实现
查看>>