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.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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
1 | 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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | function (key, values, rereduce) {
var output = {};
if ( rereduce ) {
for ( var i in values ) {
var vals = values[i];
for ( var key in vals ) {
if ( output[key] == undefined )
output[key] = vals[key];
else
output[key] = output[key] + vals[key];
}
}
} else {
for ( var i in values ) {
var doc = values[i];
var key = doc._id.substr(0, 1);
if ( output[key] !== undefined )
output[key] = output[key] + 1;
else
output[key] = 1;
}
}
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | $view = array (
'map' => 'function(doc){ emit(doc._id,doc); }' ,
'reduce' => '
function (key, values, rereduce) {
var output = {};
if ( rereduce ) {
for ( var i in values ) {
var vals = values[i];
for ( var key in vals ) {
if ( output[key] == undefined )
output[key] = vals[key];
else
output[key] = output[key] + vals[key];
}
}
} else {
for ( var i in values ) {
var doc = values[i];
var key = doc._id. substr (0, 1);
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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.