7. Java Library

7.1. Overview

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);
}

7.2. API

7.2.1. Class Wmr

public class Wmr
{
  public static void emit(String, String);
}

The Wmr class contains a single method:

public static void emit(String, String)
Prints the two provided String objects to standard output separated by a tab character.

7.2.2. Class WmrIterator

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 WmrIterator iterator()
Returns this WmrIterator when called.
public boolean hasNext()
Returns true when there is at least one additional value contained in the list (standard input) managed by the iterator. Otherwise returns false. If hasNext() returns true, it is safe to call the next() method.
public String next()
Returns the next value in the list, if available. If there is no next value (as indicated by hasNext()), a NoSuchElementException is thrown.
public void remove()
This method is not implemented, and will throw an UnsupportedOperationException if called.

7.3. Example Usage

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());
  }
}

Table Of Contents

Previous topic

6. C Library

Next topic

8. C# Library

This Page