Nov 15 2009
MapReduce API for MongoDB
Currently, I’ve been doing some stuffs using MongoDB. If you don’t know or haven’t use it, it’s a document based key-value database systems, that means it’s fundamentally different from traditional DBMS like MySQL, Oracle.
Systems like MongoDB along with similar technologies like CouchDB make significant use of MapReduce. MapReduce is basically a two step process consisting of Map and Reduce where Map is used for reducing a dataset to smaller sub-sets while Reduce is used for for some specific operations into that mapped or grouped data. You can find more information about it all over the web.
Since, PHP driver MongoDB does not provide any specific MapReduce API, I’ve created mine own using MongoDB::command. You can find it @ Github.
Simple Usage:
<?
$db_name = "test_dbs";
$mongodb = new MongoDB(new Mongo(), $db_name);
$map = <<<MAP
function() {
this.tags.forEach(
function(x) {
emit(x, 1);
}
);
}
MAP;
$reduce = <<<REDUCE
function(key, values) {
return {count: values.length };
}
REDUCE;
$map_reduce = new MongoMapReduce($map, $reduce);
$collection_name = "animal_tagsaa";
$response = $map_reduce->invoke($mongodb, $collection_name);
print_r($response->getRawResponse());
if ($response->valid()) {
echo "Total Execution Time: {$response->getTotalExecutionTime()} Milli Seconds\n";
$count_data = $response->getCountsData();
echo "Count Data\n";
foreach ($count_data as $key=>$value) {
echo "{$key}: {$value}\n";
}
echo "********************\n";
foreach ($response->getResultSet() as $tag) {
echo "{$tag["_id"]}\n";
echo "Count: {$tag["value"]["count"]}\n";
echo "****************\n";
}
}
Usage with Mongo Collections
<?php
function __autoload($class_name) {
require_once "../lib/".$class_name . '.php';
}
$db_name = "test_dbs";
$mongodb = new MongoDB(new Mongo(), $db_name);
class AnimalTag extends XMongoCollection {
const COLLECTION_NAME = "animal_tags";
public function __construct(MongoDB $mongoDB) {
$this->collectionName = self::COLLECTION_NAME;
parent::__construct($mongoDB, $this->collectionName);
}
}
$animal_tags = new AnimalTag($mongodb);
$map = <<<MAP
function() {
this.tags.forEach(
function(x) {
emit(x, 1);
}
);
}
MAP;
$reduce = <<<REDUCE
function(key, values) {
return {count: values.length };
}
REDUCE;
$response = $animal_tags->mapReduce(new MongoMapReduce($map, $reduce));
if ($response->valid()) {
foreach ($response->getResultSet() as $tag) {
echo "{$tag["_id"]}\n";
echo "Count: {$tag["value"]["count"]}\n";
echo "****************\n";
}
}
Enjoy!!!

-
prada


