org.apache.hadoop.mapred
Class Counters

java.lang.Object
  extended by org.apache.hadoop.mapred.Counters
All Implemented Interfaces:
Iterable<Counters.Group>, Writable

public class Counters
extends Object
implements Writable, Iterable<Counters.Group>

A set of named counters.

Counters represent global counters, defined either by the Map-Reduce framework or applications. Each Counter can be of any Enum type.

Counters are bunched into Counters.Groups, each comprising of counters from a particular Enum class.


Nested Class Summary
static class Counters.Counter
          A counter record, comprising its name and value.
static class Counters.CountersExceededException
          Counter exception thrown when the number of counters exceed the limit
 class Counters.Group
          Group of counters, comprising of counters from a particular counter Enum class.
 
Field Summary
static int MAX_COUNTER_LIMIT
          limit on counters
static int MAX_GROUP_LIMIT
          the max groups allowed
 
Constructor Summary
Counters()
           
 
Method Summary
 boolean equals(Object obj)
           
 Counters.Counter findCounter(Enum key)
          Find the counter for the given enum.
 Counters.Counter findCounter(String group, int id, String name)
          Deprecated.  
 Counters.Counter findCounter(String group, String name)
          Find a counter given the group and the name.
static Counters fromEscapedCompactString(String compactString)
          Convert a stringified counter representation into a counter object.
 long getCounter(Enum key)
          Returns current value of the specified counter, or 0 if the counter does not exist.
 Counters.Group getGroup(String groupName)
          Returns the named counter group, or an empty group if there is none with the specified name.
 Collection<String> getGroupNames()
          Returns the names of all counter classes.
 int hashCode()
           
 void incrAllCounters(Counters other)
          Increments multiple counters by their amounts in another Counters instance.
 void incrCounter(Enum key, long amount)
          Increments the specified counter by the specified amount, creating it if it didn't already exist.
 void incrCounter(String group, String counter, long amount)
          Increments the specified counter by the specified amount, creating it if it didn't already exist.
 Iterator<Counters.Group> iterator()
           
 void log(org.apache.commons.logging.Log log)
          Logs the current counter values.
 String makeCompactString()
          Convert a counters object into a single line that is easy to parse.
 String makeEscapedCompactString()
          Represent the counter in a textual format that can be converted back to its object form
 void readFields(DataInput in)
          Read a set of groups.
 int size()
          Returns the total number of counters, by summing the number of counters in each group.
static Counters sum(Counters a, Counters b)
          Convenience method for computing the sum of two sets of counters.
 String toString()
          Return textual representation of the counter values.
 void write(DataOutput out)
          Write the set of groups.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

MAX_COUNTER_LIMIT

public static int MAX_COUNTER_LIMIT
limit on counters


MAX_GROUP_LIMIT

public static final int MAX_GROUP_LIMIT
the max groups allowed

Constructor Detail

Counters

public Counters()
Method Detail

getGroupNames

public Collection<String> getGroupNames()
Returns the names of all counter classes.

Returns:
Set of counter names.

iterator

public Iterator<Counters.Group> iterator()
Specified by:
iterator in interface Iterable<Counters.Group>

getGroup

public Counters.Group getGroup(String groupName)
Returns the named counter group, or an empty group if there is none with the specified name.


findCounter

public Counters.Counter findCounter(Enum key)
Find the counter for the given enum. The same enum will always return the same counter.

Parameters:
key - the counter key
Returns:
the matching counter object

findCounter

public Counters.Counter findCounter(String group,
                                    String name)
Find a counter given the group and the name.

Parameters:
group - the name of the group
name - the internal name of the counter
Returns:
the counter for that name

findCounter

@Deprecated
public Counters.Counter findCounter(String group,
                                               int id,
                                               String name)
Deprecated. 

Find a counter by using strings

Parameters:
group - the name of the group
id - the id of the counter within the group (0 to N-1)
name - the internal name of the counter
Returns:
the counter for that name

incrCounter

public void incrCounter(Enum key,
                        long amount)
Increments the specified counter by the specified amount, creating it if it didn't already exist.

Parameters:
key - identifies a counter
amount - amount by which counter is to be incremented

incrCounter

public void incrCounter(String group,
                        String counter,
                        long amount)
Increments the specified counter by the specified amount, creating it if it didn't already exist.

Parameters:
group - the name of the group
counter - the internal name of the counter
amount - amount by which counter is to be incremented

getCounter

public long getCounter(Enum key)
Returns current value of the specified counter, or 0 if the counter does not exist.


incrAllCounters

public void incrAllCounters(Counters other)
Increments multiple counters by their amounts in another Counters instance.

Parameters:
other - the other Counters instance

sum

public static Counters sum(Counters a,
                           Counters b)
Convenience method for computing the sum of two sets of counters.


size

public int size()
Returns the total number of counters, by summing the number of counters in each group.


write

public void write(DataOutput out)
           throws IOException
Write the set of groups. The external format is: #groups (groupName group)* i.e. the number of groups followed by 0 or more groups, where each group is of the form: groupDisplayName #counters (false | true counter)* where each counter is of the form: name (false | true displayName) value

Specified by:
write in interface Writable
Parameters:
out - DataOuput to serialize this object into.
Throws:
IOException

readFields

public void readFields(DataInput in)
                throws IOException
Read a set of groups.

Specified by:
readFields in interface Writable
Parameters:
in - DataInput to deseriablize this object from.
Throws:
IOException

log

public void log(org.apache.commons.logging.Log log)
Logs the current counter values.

Parameters:
log - The log to use.

toString

public String toString()
Return textual representation of the counter values.

Overrides:
toString in class Object

makeCompactString

public String makeCompactString()
Convert a counters object into a single line that is easy to parse.

Returns:
the string with "name=value" for each counter and separated by ","

makeEscapedCompactString

public String makeEscapedCompactString()
Represent the counter in a textual format that can be converted back to its object form

Returns:
the string in the following format {(groupname)(group-displayname)[(countername)(displayname)(value)][][]}{}{}

fromEscapedCompactString

public static Counters fromEscapedCompactString(String compactString)
                                         throws ParseException
Convert a stringified counter representation into a counter object. Note that the counter can be recovered if its stringified using makeEscapedCompactString().

Returns:
a Counter
Throws:
ParseException

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals in class Object


Copyright © 2009 The Apache Software Foundation