Accessing a mapper’s counter from a reducer

In the Reducer’s configure(JobConf), you can use the JobConf object to look up the reducer’s own job id. With that, your reducer can create its own JobClient — i.e. a connection to the jobtracker — and query the counters for this job (or any job for that matter).

// in the Reducer class...
private long mapperCounter;

@Override
public void configure(JobConf conf) {
    JobClient client = new JobClient(conf);
    RunningJob parentJob = 
        client.getJob(JobID.forName( conf.get("mapred.job.id") ));
    mapperCounter = parentJob.getCounters().getCounter(MAP_COUNTER_NAME);
}

Now you can use mapperCounter inside the reduce() method itself.

You actually need a try-catch here. I’m using the old API, but it shouldn’t be hard to adapt for the new API.

Note that mappers’ counters should all be finalized before any reducer starts, so contrary to Justin Thomas’s comment, I believe you should get accurate values (as long as the reducers aren’t incrementing the same counter!)

Leave a Comment