3. Python Libraries

3.1. Overview

The Python libraries (versions 2 and 3) for WebMapReduce expect the mapper and reducer to be defined by the following functions:

mapper(key, value)

Map the input pair composed of key and value. Both arguments are strings.

reducer(key, values)

Reduce the intermediate values associated with key. values is an iterator that yields string values, and key is a string.

Output is given through the Wmr.emit() function.

3.2. API

3.2.1. Class Wmr

class Wmr

The Wmr class contains one static method:

emit(key, value)

Outputs a pair composed of key and value from the mapper or reducer. Both arguments may have any type, and will be converted to strings using the built-in str() function.

3.3. Example Usage

The following mapper and reducer demonstrate WordCount in Python 3:

def mapper(key, value):
  words = key.split()
  for word in words:
    Wmr.emit(word, 1)
def reducer(key, values):
  count = 0
  for value in values:
    count += int(value)
  Wmr.emit(key, count)

Table Of Contents

Previous topic

2. The Web Interface

Next topic

4. Scheme Libraries

This Page