Mapper和Reducer源码
Mapper源码
@InterfaceAudience . Public
@InterfaceStability . Stable
public class Mapper < KEYIN, VALUEIN, KEYOUT, VALUEOUT> {
public abstract class Context
implements MapContext < KEYIN, VALUEIN, KEYOUT, VALUEOUT> {
}
protected void setup ( Context context
) throws IOException, InterruptedException {
}
@SuppressWarnings ( "unchecked" )
protected void map ( KEYIN key, VALUEIN value,
Context context) throws IOException, InterruptedException {
context. write ( ( KEYOUT) key, ( VALUEOUT) value) ;
}
protected void cleanup ( Context context
) throws IOException, InterruptedException {
}
public void run ( Context context) throws IOException, InterruptedException {
setup ( context) ;
try {
while ( context. nextKeyValue ( ) ) {
map ( context. getCurrentKey ( ) , context. getCurrentValue ( ) , context) ;
}
} finally {
cleanup ( context) ;
}
}
}
Reducer源码
@Checkpointable
@InterfaceAudience . Public
@InterfaceStability . Stable
public class Reducer < KEYIN, VALUEIN, KEYOUT, VALUEOUT> {
public abstract class Context
implements ReduceContext < KEYIN, VALUEIN, KEYOUT, VALUEOUT> {
}
protected void setup ( Context context
) throws IOException, InterruptedException {
}
@SuppressWarnings ( "unchecked" )
protected void reduce ( KEYIN key, Iterable< VALUEIN> values, Context context
) throws IOException, InterruptedException {
for ( VALUEIN value: values) {
context. write ( ( KEYOUT) key, ( VALUEOUT) value) ;
}
}
protected void cleanup ( Context context
) throws IOException, InterruptedException {
}
public void run ( Context context) throws IOException, InterruptedException {
setup ( context) ;
try {
while ( context. nextKey ( ) ) {
reduce ( context. getCurrentKey ( ) , context. getValues ( ) , context) ;
Iterator< VALUEIN> iter = context. getValues ( ) . iterator ( ) ;
if ( iter instanceof ReduceContext. ValueIterator ) {
( ( ReduceContext. ValueIterator< VALUEIN> ) iter) . resetBackupStore ( ) ;
}
}
} finally {
cleanup ( context) ;
}
}
}
☆