org.apache.hadoop.mrunit.mapreduce
Class MapDriver<K1,V1,K2,V2>

java.lang.Object
  extended by org.apache.hadoop.mrunit.TestDriver<K1,V1,K2,V2,T>
      extended by org.apache.hadoop.mrunit.MapDriverBase<K1,V1,K2,V2,MapDriver<K1,V1,K2,V2>>
          extended by org.apache.hadoop.mrunit.mapreduce.MapDriver<K1,V1,K2,V2>
All Implemented Interfaces:
org.apache.hadoop.mrunit.internal.mapreduce.ContextDriver

public class MapDriver<K1,V1,K2,V2>
extends MapDriverBase<K1,V1,K2,V2,MapDriver<K1,V1,K2,V2>>
implements org.apache.hadoop.mrunit.internal.mapreduce.ContextDriver

Harness that allows you to test a Mapper instance. You provide the input (k, v)* pairs that should be sent to the Mapper, and outputs you expect to be sent by the Mapper to the collector for those inputs. By calling runTest(), the harness will deliver the input to the Mapper and will check its outputs against the expected results.


Field Summary
static org.apache.commons.logging.Log LOG
           
 
Fields inherited from class org.apache.hadoop.mrunit.MapDriverBase
inputKey, inputs, inputVal, mapInputPath, mockOutputCreator
 
Fields inherited from class org.apache.hadoop.mrunit.TestDriver
counterWrapper, expectedEnumCounters, expectedOutputs, expectedStringCounters
 
Constructor Summary
MapDriver()
           
MapDriver(org.apache.hadoop.mapreduce.Mapper<K1,V1,K2,V2> m)
           
 
Method Summary
 org.apache.hadoop.mapreduce.Mapper.Context getContext()
          Obtain Context object for furthering mocking with Mockito.
 org.apache.hadoop.mapreduce.Counters getCounters()
           
 org.apache.hadoop.mapreduce.Mapper<K1,V1,K2,V2> getMapper()
           
static
<K1,V1,K2,V2>
MapDriver<K1,V1,K2,V2>
newMapDriver()
          Returns a new MapDriver without having to specify the generic types on the right hand side of the object create statement.
static
<K1,V1,K2,V2>
MapDriver<K1,V1,K2,V2>
newMapDriver(org.apache.hadoop.mapreduce.Mapper<K1,V1,K2,V2> mapper)
          Returns a new MapDriver without having to specify the generic types on the right hand side of the object create statement.
 List<Pair<K2,V2>> run()
          Runs the test but returns the result set instead of validating it (ignores any addOutput(), etc calls made before this)
 void setCounters(org.apache.hadoop.mapreduce.Counters ctrs)
          Sets the counters object to use for this test.
 void setMapper(org.apache.hadoop.mapreduce.Mapper<K1,V1,K2,V2> m)
          Set the Mapper instance to use with this test driver
 String toString()
           
 MapDriver<K1,V1,K2,V2> withCounters(org.apache.hadoop.mapreduce.Counters ctrs)
          Sets the counters to use and returns self for fluent style
 MapDriver<K1,V1,K2,V2> withMapper(org.apache.hadoop.mapreduce.Mapper<K1,V1,K2,V2> m)
          Sets the Mapper instance to use and returns self for fluent style
 MapDriver<K1,V1,K2,V2> withOutputFormat(Class<? extends org.apache.hadoop.mapreduce.OutputFormat> outputFormatClass, Class<? extends org.apache.hadoop.mapreduce.InputFormat> inputFormatClass)
          Configure Mapper to output with a real OutputFormat.
 
Methods inherited from class org.apache.hadoop.mrunit.MapDriverBase
addAll, addInput, addInput, clearInput, getInputKey, getInputValue, getMapInputPath, preRunChecks, printPreTestDebugLog, setInput, setInput, setInputFromString, setInputKey, setInputValue, setMapInputPath, withAll, withInput, withInput, withInputFromString, withInputKey, withInputValue, withMapInputPath
 
Methods inherited from class org.apache.hadoop.mrunit.TestDriver
addAllOutput, addCacheArchive, addCacheArchive, addCacheFile, addCacheFile, addOutput, addOutput, addOutputFromString, cleanupDistributedCache, copy, copyPair, formatValueList, getConfiguration, getExpectedEnumCounters, getExpectedOutputs, getExpectedStringCounters, getOutputSerializationConfiguration, initDistributedCache, parseCommaDelimitedList, parseTabbedPair, resetExpectedCounters, resetOutput, run, runTest, runTest, setCacheArchives, setCacheFiles, setConfiguration, setOutputSerializationConfiguration, thisAsTestDriver, validate, validate, withAllOutput, withCacheArchive, withCacheArchive, withCacheFile, withCacheFile, withConfiguration, withCounter, withCounter, withOutput, withOutput, withOutputFromString, withOutputSerializationConfiguration, withStrictCounterChecking
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.apache.hadoop.mrunit.internal.mapreduce.ContextDriver
getConfiguration, getOutputSerializationConfiguration
 

Field Detail

LOG

public static final org.apache.commons.logging.Log LOG
Constructor Detail

MapDriver

public MapDriver(org.apache.hadoop.mapreduce.Mapper<K1,V1,K2,V2> m)

MapDriver

public MapDriver()
Method Detail

setMapper

public void setMapper(org.apache.hadoop.mapreduce.Mapper<K1,V1,K2,V2> m)
Set the Mapper instance to use with this test driver

Parameters:
m - the Mapper instance to use

withMapper

public MapDriver<K1,V1,K2,V2> withMapper(org.apache.hadoop.mapreduce.Mapper<K1,V1,K2,V2> m)
Sets the Mapper instance to use and returns self for fluent style


getMapper

public org.apache.hadoop.mapreduce.Mapper<K1,V1,K2,V2> getMapper()
Returns:
the Mapper object being used by this test

getCounters

public org.apache.hadoop.mapreduce.Counters getCounters()
Specified by:
getCounters in interface org.apache.hadoop.mrunit.internal.mapreduce.ContextDriver
Returns:
the counters used in this test

setCounters

public void setCounters(org.apache.hadoop.mapreduce.Counters ctrs)
Sets the counters object to use for this test.

Parameters:
ctrs - The counters object to use.

withCounters

public MapDriver<K1,V1,K2,V2> withCounters(org.apache.hadoop.mapreduce.Counters ctrs)
Sets the counters to use and returns self for fluent style


withOutputFormat

public MapDriver<K1,V1,K2,V2> withOutputFormat(Class<? extends org.apache.hadoop.mapreduce.OutputFormat> outputFormatClass,
                                               Class<? extends org.apache.hadoop.mapreduce.InputFormat> inputFormatClass)
Configure Mapper to output with a real OutputFormat. Set InputFormat to read output back in for use with run* methods

Parameters:
outputFormatClass -
inputFormatClass -
Returns:
this for fluent style

run

public List<Pair<K2,V2>> run()
                      throws IOException
Description copied from class: TestDriver
Runs the test but returns the result set instead of validating it (ignores any addOutput(), etc calls made before this)

Specified by:
run in class MapDriverBase<K1,V1,K2,V2,MapDriver<K1,V1,K2,V2>>
Returns:
the list of (k, v) pairs returned as output from the test
Throws:
IOException

toString

public String toString()
Overrides:
toString in class Object

getContext

public org.apache.hadoop.mapreduce.Mapper.Context getContext()

Obtain Context object for furthering mocking with Mockito. For example, causing write() to throw an exception:

 import static org.mockito.Matchers.*;
 import static org.mockito.Mockito.*;
 doThrow(new IOException()).when(context).write(any(), any());
 

Or implement other logic:

 import static org.mockito.Matchers.*;
 import static org.mockito.Mockito.*;
 doAnswer(new Answer() {
    public Object answer(final InvocationOnMock invocation) {
    ...
     return null;
   }
 }).when(context).write(any(), any());
 

Returns:
the mocked context

newMapDriver

public static <K1,V1,K2,V2> MapDriver<K1,V1,K2,V2> newMapDriver()
Returns a new MapDriver without having to specify the generic types on the right hand side of the object create statement.

Returns:
new MapDriver

newMapDriver

public static <K1,V1,K2,V2> MapDriver<K1,V1,K2,V2> newMapDriver(org.apache.hadoop.mapreduce.Mapper<K1,V1,K2,V2> mapper)
Returns a new MapDriver without having to specify the generic types on the right hand side of the object create statement.

Parameters:
mapper - passed to MapDriver constructor
Returns:
new MapDriver


Copyright © 2013 The Apache Software Foundation. All Rights Reserved.