Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need Explanation of couchdb reduce function

From http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views

The couchdb reduce function is defined as

function (key, values, rereduce) {
    return sum(values);
}
  • key will be an array whose elements are arrays of the form [key,id]
  • values will be an array of the values emitted for the respective elements in keys
  • i.e. reduce([ [key1,id1], [key2,id2], [key3,id3] ], [value1,value2,value3], false)

I am having trouble understanding when/why the array of keys would contain different key values. If the array of keys does contain different key values, how would I deal with it?

As an example, assume that my database contains movements between accounts of the form.

{"amount":100, "CreditAccount":"account_number", "DebitAccount":"account_number"}

I want a view that gives the balance of an account.

My map function does:

emit( doc.CreditAccount, doc.amount )
emit( doc.DebitAccount, -doc.amount )

My reduce function does:

return sum(values);

I seem to get the expected results, however I can't reconcile this with the possibility that my reduce function gets different key values.

Is my reduce function supposed to group key values first? What kind of result would I return in that case?

like image 424
Alan Avatar asked May 03 '10 18:05

Alan


People also ask

What is MapReduce in CouchDB?

In order to retrieve data with CouchDB, we use a process called MapReduce, to create views. A view contains rows of data that is sorted by the row's key (you might use date as a key, for example, to sort your data based on the date). MapReduce is a combination of two concepts Map and Reduce.

What is the need of CouchDB?

CouchDB is an open source NoSQL database based on common standards to facilitate Web accessibility and compatibility with a variety of devices. NoSQL databases are useful for very large sets of distributed data, especially for the large amounts of non-uniform data in various formats that is characteristic of big data.

How many arguments does emit accept in CouchDB?

The emit() function always takes two arguments: the first is key , and the second is value .


1 Answers

By default, Futon "groups" your results, which means you get a fresh reduce per key—in your case, an account. The group feature is for exactly this situation.

Over the raw HTTP API, you will get one total reduce for all accounts which is probably not useful. So remember to use group=true in your own application to be sure you get summaries per account.

like image 191
JasonSmith Avatar answered Sep 27 '22 19:09

JasonSmith