SquanderingTime

Icon

Now with more slouching

Simple stats average with CouchDB

Basic statistics seem to be giving people new to Map/Reduce difficulty, including myself. Here’s a short example on how to get the average value of a group of records.

Suppose you have a number of records in the format:

{ 'value' : 345 }

The goal is to write a mapping function that emits a key pair with a non-unique key and a value representing the thing you want to average. The non-unique key is important because the values will be appended in the intermediate hash table in a list-style fashion, rather than being replaced like a normal hash table.

function(doc) {
emit('average', doc.value);
}

This mapping function makes a single key of “average” available to the reducing function.

Since all the numerical values we want were emitted under the “average” key the reducing function only has to sum the values and divide by the length. In a more sophisticated case we could change the result to utilize the rereduce flag and allow incremental processing.


function(key,values,rereduce)
{
return sum(values)/values.length;
}

Advertisement

Filed under: CouchDB

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.