【发布时间】:2018-03-27 07:09:29
【问题描述】:
我目前正在使用 Cloudera 上的 Counters 开发 MapReduce 仅地图程序。 Mapper 类将增加一个特定的计数器,我想在 MapReduce 作业完成后显示每个计数器的最终值。下面是我的 Mapper 类代码:
public class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
public static enum MY_COUNTER {
C1,
C2
}
//mapper logic that produces String variable 'final'
if (final.equals("Foo")) context.getCounter(MY_COUNTER.C1).increment(1);
else context.getCounter(MY_COUNTER.C2).increment(1);
//context.write() method
}
下面是我的驱动类代码:
public class MyDriver extends Configured implements Tool {
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new MyDriver(), args);
System.exit(exitCode);
}
public int run(String[] args) throws Exception {
Job job = Job.getInstance(getConf(), "My MapReduce");
//Job configuration:
//Sets mapper to MyMapper class
//Sets num of Reduce tasks to 0
//Other necessary job config
boolean success = job.waitForCompletion(true);
if (success) {
Counter counter1 = job.getCounters().findCounter("MY_COUNTER", "C1");
System.out.println(counter1.getDisplayName() + ": " + counter1.getValue());
Counter counter2 = job.getCounters().findCounter("MY_COUNTER", "C2");
System.out.println(counter2.getDisplayName() + ": " + counter2.getValue());
return 0;
}
else return 1;
}
}
当我运行 jar 文件时,作业成功执行。因为我将job.waitForCompletion() 参数设置为true,所以它将所有MapReduce 进度打印到终端。我可以从那里看到我的计数器的值。
18/03/27 09:59:58 INFO mapreduceJob: Counters: 35
//all built-in counters
MyMapper$MY_COUNTER
C1=837
C2=119
但是,当我在作业完成后打印计数器的值时(来自 MyDriver 类的 if(success) 部分),打印的值全为零。
C1: 0
C2: 0
关于我可能错在哪里的任何建议?
注意:我使用的是 Hadoop 2.6.0-cdh5.12.0
【问题讨论】:
-
我正在使用类似的代码,它工作正常。除了,我使用
context.getCounter("MY_COUNTER", "C1").increment(1);如果它有效,请尝试。 -
@Amita 那行得通!有了这个,我什至不需要在我的 Mapper 类中为我的计数器创建一个枚举。非常感谢!
标签: java hadoop mapreduce cloudera