Cloudera MapReduce 计数器获取值错误
Cloudera MapReduce Counter getValue Error
我目前正在 Cloudera 上使用 Counters 开发一个 MapReduce 纯地图程序。 Mapper class 将增加一个特定的计数器,我想在 MapReduce 作业完成后显示每个计数器的最终值。下面是我的 Mapper class 代码:
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
}
下面是我的驱动程序 class 代码:
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 class 的 if(success)
部分),打印的值全为零。
C1: 0
C2: 0
对我可能哪里错了有什么建议吗?
注意:我使用的是 Hadoop 2.6.0-cdh5.12.0
找到问题了。我应该使用字符串参数而不是枚举来增加计数器。增量过程将是这样的:
context.getCounter("MY_COUNTER","C1").increment(1);
有了它,我什至不需要在我的 Mapper class 上为我的计数器声明一个枚举。感谢 Amita 帮助我。
我目前正在 Cloudera 上使用 Counters 开发一个 MapReduce 纯地图程序。 Mapper class 将增加一个特定的计数器,我想在 MapReduce 作业完成后显示每个计数器的最终值。下面是我的 Mapper class 代码:
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
}
下面是我的驱动程序 class 代码:
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 class 的 if(success)
部分),打印的值全为零。
C1: 0
C2: 0
对我可能哪里错了有什么建议吗?
注意:我使用的是 Hadoop 2.6.0-cdh5.12.0
找到问题了。我应该使用字符串参数而不是枚举来增加计数器。增量过程将是这样的:
context.getCounter("MY_COUNTER","C1").increment(1);
有了它,我什至不需要在我的 Mapper class 上为我的计数器声明一个枚举。感谢 Amita 帮助我。