public class Counter extends Object implements Writable
Counter相关类的关系:
Counters 是 CounterGroup 的集合CounterGroup 是 Counter 的集合示例代码:
public class UserDefinedCounterExample {
enum MyCounter {
DIRTY_RECORDS
}
public static class MyMapper extends Mapper<Text, Text> {
Counter counter;
protected void setup(MapContext<Text, LongWritable> context)
throws IOException, InterruptedException {
counter = context.getCounter(MyCounter.DIRTY_RECORDS);
}
public void map(LongWritable recordNum, Record record,
MapContext<Text, Text> context) throws IOException,
InterruptedException {
if (dirty) {
counter.increment(1);
}
}
}
public static void main(String[] args) throws Exception {
JobConf job = new JobConf();
... ...
RunningJob rJob = JobClient.runJob(job);
Counters counters = rJob.getCounters();
long count = counters.findCounter(MyCounter.DIRTY_RECORDS).getValue();
}
}
Counters,
CounterGroup,
TaskAttemptContext#getCounter(Enum)| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object genericRight) |
String |
getDisplayName()
获取Counter的显示名称
|
String |
getName()
获取Counter名称
|
long |
getValue()
获取当前Counter计数值
|
int |
hashCode() |
void |
increment(long incr)
增加Counter计数值
|
void |
readFields(DataInput in)
从
DataInput读取Counter信息 |
void |
setValue(long value)
设置Counter计数值
|
void |
write(DataOutput out)
写当前Counter到
DataOutput |
public String getName()
public String getDisplayName()
public long getValue()
public void setValue(long value)
value - Counter计数值public void increment(long incr)
incr - Counter计数值的增量public void readFields(DataInput in) throws IOException
DataInput读取Counter信息readFields in interface WritableIOExceptionpublic void write(DataOutput out) throws IOException
DataOutputwrite in interface WritableIOExceptionCopyright © 2024 Alibaba Cloud Computing. All rights reserved.