读书人

hadoop单元测试方法-施用和增强MRUnit

发布时间: 2012-12-21 12:03:49 作者: rapoo

hadoop单元测试方法--使用和增强MRUnit[1]

?

hadoop单元测试方法--使用和增强MRUnit


1前言

???????? hadoop的mapreduce提交到集群环境中出问题的定位是比较麻烦的,有时需要一遍遍的修改代码和打出日志来排查一个很小的问题,如果数据量大的话调试起来相当耗时间。因此有必要使用良好的单元测试手段来尽早的消除明显的bug(当然仅有单元测试是不够的,毕竟跟集群的运行环境还是不一样的)。

?????? 然而做mapreduce的单元测试会有一些障碍,比如Map和Reduce一些参数对象是在运行时由hadoop框架传入的,例如OutputCollector、Reporter、InputSplit等。这就需要有Mock手段。最初写mapreduce单元测试的时候自己写了几个简单的Mock也基本能满足需要,后来发现MRUnit比我写的要好用所以研究了一下就采用了。MRUnit是专门为hadoop mapreduce写的单元测试框架,API简洁明了,简单实用。但也有一些薄弱的地方,比如不支持MultipleOutputs(很多情况下我们会用MultipleOutputs作为多文件输出,后面将介绍如何增强MRUnit使之支持MultipleOutputs)。

2 MRUnit

???????? MRUnit针对不同测试对象分别使用以下几种Driver:

???????? 单独测试Map的例子,假设我们要计算一个卖家的平均发货速度。Map将搜集每一次发货的时间间隔。针对Map的测试,

??? private Map mapper;

??? private MapDriver<LongWritable,Text, Text, TimeInfo> mapDriver;

??? @Before

??? public void setUp() {

??????? mapper = new Map();

??????? mapDriver = new MapDriver<LongWritable, Text, Text,TimeInfo>();

??? @Test

??? public void testMap_timeFormat2() {

??????? String sellerId = "444";

??????? //模拟输入一行(withInput),假设从这行数据中我们可以获得卖家(sellerId)????? //某一次时间间隔 为10小时.

?????? //我们期望它输出sellerId为key,value为代表1次10小时的TimeInfo对象。????????? //(withOutput)

?????? //如果输入数据经过Map计算后为期望的结果,那么测试通过。

?????? Text mapInputValue = new Text("……");

???? ?? mapDriver.withMapper(mapper)

????????????? .withInput(null, mapInputValue)

????????????? .withOutput(new Text(sellerId), new TimeInfo(1, 10))

????????????? .runTest();

???????? 针对Reduce的单独测试,还是这个例子。Reduce为根据Map或Combiner输出的n次时间间隔的总和来计算平均时间。

??? private Reduce reducer;

??? @Before

??? public void setUp() {

??????? reducer = new Reduce();

??????? reduceDriver = new ReduceDriver<Text, TimeInfo, Text, ?????????????????????????????? ?????????? LongWritable>(reducer);

??? }

?

??? @Test

??? public void testReduce () {

??????? List<TimeInfo> values = newArrayList<TimeInfo>();

??????? values.add(new TimeInfo(1, 3));//一次3小时

??????? values.add(new TimeInfo(2, 5));//两次总共5小时

??????? values.add(new TimeInfo(3, 7));//三次总共7小时

?????? //values作为444这个卖家的reduce输入,

?????? //期望计算出平均为2小时

??????? reduceDriver.withReducer(reducer)

????????????????? .withInput(new Text("444"), values)

????????????????? .withOutput(new Text("444"),new ?LongWritable(2))

????????????????? .runTest();

??? 以下为Map和Reduce联合测试的例子,

??? privateMapReduceDriver<LongWritable, Text, Text, TimeInfo, Text, LongWritable> mrDriver;

??? private Map mapper;

??? @Before

??? public void setUp() {

??????? mapper = new Map();

??????? reducer = new Reduce();

??????? mrDriver = new MapReduceDriver<LongWritable, Text, Text,TimeInfo, ?????????????? ??? Text, LongWritable>(mapper, reducer);

??? @Test

??????? mrDriver.withInput(null, mapInputValue1)

?????????? .withInput(null, mapInputValue2)

?????????? .withInput(null, mapInputValue3)

?????????? .withOutput(new Text("444"),new LongWritable(2))

?????????? .runTest();

??? }

?

字数超出了,下一篇继续介绍增强MRUnit使他支持MultipleOutputs、从文件加载数据集和自动装配等几个特性http://jen.iteye.com/blog/1003868

读书人网 >编程

热点推荐