Skip to content

Instantly share code, notes, and snippets.

@Chouser
Created May 5, 2011 21:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chouser/958046 to your computer and use it in GitHub Desktop.
Save Chouser/958046 to your computer and use it in GitHub Desktop.
Making CouchDB map/reduce a little less nutty...
// This is the map function to give to CouchDB:
function(doc) {
function xemit(key, val) {
emit(key, {key: key, val: val});
}
// Your code goes here, working with the doc provided.
// Use xemit(key, value) instead of CouchDB's emit
// ...
}
// This is the reduce function to give to CouchDB:
function (keys, docs) {
function xreduce(key, values) {
// Your code goes here, working with a single key and list of values,
// as we discussed in class. Note you can rename the args 'key' and
// 'values' of this xreduce function if you'd like. Remember to
// return a single value of the same type as you passed as value to xemit().
// ...
}
// This stuff is just to make reduce behave sensibly:
var vals = [];
for(var i = 0; i < docs.length; ++i) {
vals.push(docs[i].val);
}
return {key: docs[0].key, val: xreduce(docs[0].key, vals)};
}
// Note that the results of this will have an extra level of object in the output.
// For example, if you do something like xemit('Smith', 25), the resulting
// couchdb view will actually be more like:
// { Smith: { key: 'Smith', val: 25 } }
// Here's an example of using the template above to compute the
// average year each author's books were published. The parts
// marked between BEGIN and END should look like the map/reduce
// for average that we looked at in class -- the only difference
// is that this code is avg_year for the book collection instead
// of avg_age for a person collection:
// map
function(doc) {
function xemit(key, val) {
emit(key, {key: key, val: val});
}
// BEGIN Code to do the average:
for(var i = 0; i < doc.books.length; ++i) {
xemit(doc.author.last_name,
{count: 1, year_sum: doc.books[i].year});
}
// END
}
// reduce
function (keys, docs) {
function xreduce(key, values) {
// BEGIN Code to do the average:
var result = {count: 0, year_sum: 0};
for(var i = 0; i < values.length; ++i) {
result.count += values[i].count;
result.year_sum += values[i].year_sum;
}
return result;
// END
}
var vals = [];
for(var i = 0; i < docs.length; ++i) {
vals.push(docs[i].val);
}
return {key: docs[0].key, val: xreduce(docs[0].key, vals)};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment