Unit Testing a Java Hadoop Job
Join the DZone community and get the full member experience.
Join For Freein my previous post i showed how to setup a complete maven based project to create a hadoop job in java. of course it wasn’t complete because it is missing the unit test part . in this post i show how to add mapreduce unit tests to the project i started previously. for the unit test i make use of the mrunit framework .
- add the necessary dependency to the pom
- add unit tests for testing the map reduce logic
- run the unit tests it
add the following dependency to the pom:
<dependency> <groupid>org.apache.mrunit</groupid> <artifactid>mrunit</artifactid> <version>1.0.0</version> <classifier>hadoop1</classifier> <scope>test</scope> </dependency>
this will made the mrunit framework available to the project.
the use of this framework is quite straightforward, especially in our business case. so i will just show the unit test code and some comments if necessary but i think it is quite obvious how to use it.
the unit test for the mapper ‘mappertest’:
package net.pascalalma.hadoop; import org.apache.hadoop.io.text; import org.apache.hadoop.mrunit.mapreduce.mapdriver; import org.junit.before; import org.junit.test; import java.io.ioexception; /** * created with intellij idea. * user: pascal */ public class mappertest { mapdriver<text, text, text, text> mapdriver; @before public void setup() { wordmapper mapper = new wordmapper(); mapdriver = mapdriver.newmapdriver(mapper); } @test public void testmapper() throws ioexception { mapdriver.withinput(new text("a"), new text("ein")); mapdriver.withinput(new text("a"), new text("zwei")); mapdriver.withinput(new text("c"), new text("drei")); mapdriver.withoutput(new text("a"), new text("ein")); mapdriver.withoutput(new text("a"), new text("zwei")); mapdriver.withoutput(new text("c"), new text("drei")); mapdriver.runtest(); } }
this test class is actually even simpler than the mapper implementation itself. you just define the input of the mapper and the expected output and then let the configured mapdriver run the test. in our case the mapper doesn’t do anything specific but you see how easy it is to setup a testcase.
for completeness here is the test class of the reducer:
package net.pascalalma.hadoop; import org.apache.hadoop.io.text; import org.apache.hadoop.mrunit.mapreduce.reducedriver; import org.junit.before; import org.junit.test; import java.io.ioexception; import java.util.arraylist; import java.util.list; /** * created with intellij idea. * user: pascal */ public class reducertest { reducedriver<text, text, text, text> reducedriver; @before public void setup() { alltranslationsreducer reducer = new alltranslationsreducer(); reducedriver = reducedriver.newreducedriver(reducer); } @test public void testreducer() throws ioexception { list<text> values = new arraylist<text>(); values.add(new text("ein")); values.add(new text("zwei")); reducedriver.withinput(new text("a"), values); reducedriver.withoutput(new text("a"), new text("|ein|zwei")); reducedriver.runtest(); } }
with the maven command “mvn clean test” we can run the tests:
with the unit tests in place i would say we are ready to build the project and deploy it to an hadoop cluster, which i will describe in the next post.
Published at DZone with permission of $$anonymous$$. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Automating the Migration From JS to TS for the ZK Framework
-
Java String Templates Today
-
API Governance: Best Practices and Strategies for Effective API Management
-
What to Pay Attention to as Automation Upends the Developer Experience
Comments