Lets say you have the CouchDB classes (located here) all compiled together and included into your test.php script. Lets also say that you have created a database with the built-in web ui called “testing”. Finally let us say that your test.php has the following code in it, which would add a record to the db every time it is run. (i know that the data in the document serves no useful purpose… but really I just want to figure out this map/reduce thing so that I can make awesome views… so this suffices sufficiently.)
require_once dirname( dirname( __FILE__ ) ) . '/includes/couchdb.php';
$couchdb = new CouchDB('testing', 'localhost', 5984);
$key = microtime();
$result = $couchdb->send(
'/'.md5($key),
'put',
json_encode(
array(
"_id" => md5($key),
"time" => $key,
'md5' => md5($key),
'sha1' => sha1($key),
'crc' => crc32($key)
)
)
);
print_r($result->getBody(true));
After running the code a bunch of times you would end up with a bunch of documents which look more or less like this:
(click for full size)
Now lets say you want to write a view that told you what the first characters of the _id were and how many documents share that first letter. This is analogous to the following in MySQL
SELECT LEFT(md5, 1) AS `lchar`, count(md5) FROM `md5table` GROUP BY `lchar`
Your map function is easy, because you dont have any selection criteria, so we process all rows
function(doc){ emit(doc._id,doc); }
The reduce function is where the actual programming comes in… And it seems there aren’t many well explained examples of exactly how to do this (I just brute forced it by trial and error)
function(key, values, rereduce) {
var output = {};
if ( rereduce ) {
// key is null, and values are values returned by previous calls
//
// see http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views
//
// essentially we are taking the previously reduced view, and the
// reduced view for new records, and we are reducing those two things
// together. Summarizing two summaries, essentially
for ( var i in values ) {
// here we have multiple prebuilt output objects and we're simply combining them
// just like below we have an array with a numeric id and an output object
//
// retrieve a summary
var vals = values[i];
for ( var key in vals ) {
// debugging
// log(key);
//
// store in or increment our new output object
if ( output[key] == undefined )
output[key] = vals[key];
else
output[key] = output[key] + vals[key];
}
}
} else {
// key is an array, which we dont care about, and values are the
// values returned by the map
//
// see http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views
//
// we are taking each document and processing that, reducing it down
// to a summary object (output) for each of the rows passed
for ( var i in values ) {
// we have an array, values, with numeric ids and a document objects
//
// retrieve a document
var doc = values[i];
// get what we want from it, the first char of the md5
var key = doc._id.substr(0, 1);
// debugging
// log( key + " :: " + doc._id );
//
// store or increment the output object
if ( output[key] !== undefined )
output[key] = output[key] + 1;
else
output[key] = 1;
}
}
// done
return output;
}
and in code, using a temporary view, ( if you used this view all the time you would want to make it permanent… but this is about how to lay out a reduce function, nothing more ) so request code that looks like this
$view = array(
'map' => 'function(doc){ emit(doc._id,doc); }',
'reduce' => '
function(key, values, rereduce) {
var output = {};
if ( rereduce ) {
// key is null, and values are values returned by previous calls
for ( var i in values ) {
var vals = values[i];
for ( var key in vals ) {
// log(key);
if ( output[key] == undefined )
output[key] = vals[key];
else
output[key] = output[key] + vals[key];
}
}
} else {
// key is an array, which we dont care about, and values are the values returneb by the map
for ( var i in values ) {
var doc = values[i];
var key = doc._id.substr(0, 1);
// log( key + " :: " + doc._id );
if ( output[key] !== undefined )
output[key] = output[key] + 1;
else
output[key] = 1;
}
}
return output;
}
'
);
$result = $couchdb->send('/_temp_view', 'POST', json_encode($view) );
print_r($result->getBody(true));
would give you output that looks like this:
stdClass Object
(
[rows] => Array
(
[0] => stdClass Object
(
[key] =>
[value] => stdClass Object
(
[0] => 15
[1] => 17
[2] => 16
[3] => 13
[4] => 27
[5] => 18
[6] => 26
[7] => 15
[8] => 18
[9] => 21
[a] => 12
[b] => 23
[c] => 20
[d] => 27
[e] => 28
[f] => 26
)
)
)
)
I hope this helps somebody out.
You must be logged in to post a comment.