The Java library for WMR is based on two user classes, and using an iterator to manage the list of values passed to the reducer. The two classes that users must provide are:
public class Mapper
{
public void map(String, String);
}
public class Reducer
{
public void reduce(String, WmrIterator);
}
public class Wmr
{
public static void emit(String, String);
}
The Wmr class contains a single method:
public class WmrIterator implements Iterator<String>, Iterable<String>
{
public WmrIterator iterator();
public boolean hasNext();
public String next() throws NoSuchElementException;
public void remove() throws UnsupportedOperationException;
}
The WmrIterator class provides the list of values to the reducer, and is compatible with Java-style iterators.
public class Mapper
{
public void map(String key, String value)
{
String words[] = key.split(" ");
for (int i = 0; i < words.length; i++)
{
Wmr.emit(words[i], "1");
}
}
}
public class Reducer
{
public void reduce(String key, WmrIterator values)
{
int sum = 0;
for (String value : values)
{
sum += Integer.parseInt(value);
}
Wmr.emit(key, Integer.valueOf(sum).toString());
}
}