6. C Library

6.1. Overview

The C libraries for WebMapReduce expect the mapper and the reducer to be defined by two methods with the following signatures:

void mapper(char* key, char* val);
void reducer(char* key, wmr_handle handle);

One should also include the file “wmr.h”

6.2. API

The wmr.h file provides the following types and functions:

typedef const int wmr_handle;
void wmr_emit(const char* key, const char* val);
const char* wmr_get_val(wmr_handle h);
typdef const int wmr_handle
This “handle” type is used to retrieve values in the reducer. It is passed to the wmr_get_val(wmr_handle) to get the actual value.
void wmr_emit(const char* key, const char* val)
Prints the two provided null-terminated characters to the command line, separated by a tab character. Used to emit a key-value pair from the mapper or the reducer.
const char* wmr_get_val(wmr_handle h);
Retrieve the next value associated with the current key. Return NULL if no more values are available. The pointer returned is only valid until the next call to this function.

6.3. Example Usage

#include <string.h>
#include "wmr.h"

void mapper(char* key, char* val) {
  while (key != NULL) {
    wmr_emit(strsep(&key, " "), "1");
  }
}
#include "wmr.h"
#include <stdlib.h>

void reducer(char* key, wmr_handle handle) {
  long sum = 0;  /* sum of values encountered so far */
  char *str;  /* to hold string representation of an integer value */
  char sumstr[20];  /* to hold string representation of sum */

  while (str = wmr_get_val(handle))
    sum += strtol(str, NULL, 10);
  sprintf(sumstr, "%d", sum);
  wmr_emit(key, sumstr);
}

Table Of Contents

Previous topic

5. C++ Library

Next topic

7. Java Library

This Page