Product SiteDocumentation Site

5.3. Example Usage

This section presents the code to run a word count job using WebMapReduce and the C++ library.
#include <string>
#include <vector>

using std::string;
using std::vector;

class mapper
{
public:
    void map(string key, string value)
    {
        // split the line into a vector of words
        vector<string> words = wmr::utility::split(key, ' ');

        // loop through the vector, and emit each word with a 1
        for (size_t i = 0; i < words.size(); ++i)
        {
            wmr::emit(words.at(i), 1);
        }
    }
};
Figure 5.1. Example Mapper

#include <string>

using std::string;

class reducer
{
public:
    void reduce(std::string key, wmr::datastream stm)
    {
        long sum = 0;
        string value;

        // grab data from the stream until there are no more values
        while (!stm.eof())
        {
            stm >> value;

            // convert the value to a number, then add it to the
            // running total
            sum += wmr::utility::fromString<long>(value);
        }

        // emit the key with its total count
        wmr::emit(key, sum);
    }
};
Figure 5.2. Example Reducer