用MRUnit对MapReduce程序进行单元测试 单元测试工具
很多人都为调试MapReduce程序感到头疼,因此有人贡献了MRUnit,专门进行MapReduce程序测试的工具。最新的hadoop-0.20已经包含这个工具,CloudEra甚至将他backport到0.18.x的发布中来。 MRUnit的使用方法非常简单,只要确定输入和期望的输出,剩下的工作交给MapDriver和ReduceDriver就okay了。下面以最简单的IdentityMapper和IdentityReducer做例子, 他们都是将输入直接传递给输出,不做任何的修改。 import junit.framework.TestCase; import java.util.List; import java.util.LinkedList; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapred.Mapper; import org.apache.hadoop.mapred.lib.IdentityMapper; import org.apache.hadoop.mapred.Reducer; import org.apache.hadoop.mapred.lib.IdentityReducer; import org.apache.hadoop.mrunit.MapDriver; import org.apache.hadoop.mrunit.ReduceDriver; import org.apache.hadoop.mrunit.types.Pair; import org.junit.Before; import org.junit.Test; public class TestExample extends TestCase { private Mapper mapper; private Reducer reducer; private MapDriver mapDriver; private ReduceDriver reduceDriver; @Before public void setUp() { mapper = new IdentityMapper(); reducer = new IdentityReducer(); mapDriver = new MapDriver(mapper); reduceDriver = new ReduceDriver(reducer); } @Test public void testIdentityMapper() { List> res; mapDriver.setInput(new Text(“key”), new Text(“keyword1″)); mapDriver.addOutput(new Text(“key”), new Text(“keyword1″)); mapDriver.runTest(); } @Test public void testIdentityReducer() { List> res; List input = new LinkedList(); input.add(new Text(“keyword1″)); input.add(new Text(“keyword2″)); reduceDriver.setInput(new Text(“key”), input); reduceDriver.addOutput(new Text(“key”), new Text(“keyword1″)); reduceDriver.addOutput(new Text(“key”), new Text(“keyword2″)); reduceDriver.runTest(); } }
文章来源于领测软件测试网 https://www.ltesting.net/