Prajwal Tuladhar’s Blog
 
programming, life and some random thoughts

Nov 15 2009

MapReduce API for MongoDB

Published by at 7:31 pm under 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!!!


4 responses so far

4 Responses to “MapReduce API for MongoDB”

  1. [...] http://prajwal-tuladhar.net.np/2009/11/15/496/mapreduce-api-for-mongodb/?utm_source=feedburner&... a few seconds ago from Choqok [...]

  2. millipo22on 19 Dec 2009 at 2:09 am

    Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of presentation.

    respect
    Clark
    ______________________________________________
    miracle cabbage soup diet | miracle cabbage soup diet | miracle cabbage soup diet | miracle cabbage soup diet | cayenne pepper detox | cayenne pepper detox | cayenne pepper detox | cayenne pepper detox | maple syrup detox | maple syrup detox | maple syrup detox | maple syrup detox | british heart foundation diet | british heart foundation diet | british heart foundation diet | british heart foundation diet | weight gain diets | weight gain diets | weight gain diets | weight gain diets | easy diet plans | easy diet plans | easy diet plans | easy diet plans | sugar busters diet | sugar busters diet | sugar busters diet | sugar busters diet | diets for quick weight loss | diets for quick weight loss | diets for quick weight loss | diets for quick weight loss | diets for quick weight loss

  3. pradaon 10 Jun 2010 at 9:24 pm

    Here elaborates the matter not only extensively but also detailly .I support the
    write's unique point.It is useful and benefit to your daily life.You can go those
    sits to know more relate things.They are strongly recommended by friends.Personally
    I feel quite well.. http://www.prada-outlet-store.com

  4. air jordan 13on 05 Jul 2010 at 1:26 am

    Mark S. is definitely on the right track. If you want to get a professional looking email address, Id recommend buying your name domain name, like or
    Gucci sweaters
    If its common it might be difficult to get, however, be creative and you can usually find something.

RSS Feed
Subscribe by email
Follow me @ Twitter