Strata Gems: CouchDB in the browser

Get a feel for MapReduce without installing any software

We’re publishing a new Strata Gem each day all the way through to December 24. Yesterday’s Gem: Whirr makes Hadoop and Cassandra a snap.

Strata 2011 An earlier Strata Gem explained how to try MongoDB without installing any software. You can get a similar experience for CouchDB thanks to JSCouch, which also provides a great introduction to writing MapReduce functions.

CouchDB was the first document-store database to become popular under the “NoSQL” label, and offers some unique and interesting features.

  • RESTful JSON API — rather than requiring programming language-specific bindings, CouchDB can be accessed over HTTP, with JSON used as the data format
  • MapReduce — CouchDB provides query and view functionality via MapReduce operations, which are specified in JavaScript
  • Replication — with incremental replication and bi-directional conflict resolution, CouchDB is suitable for connecting replicated databases with intermittent connectivity. The Ubuntu One cloud service uses this feature

To demonstrate the basics of CouchDB, Mu Dynamics and Jan Lehnardt created a partial implementation of CouchDB that runs in the browser, JSCouch.

The sample database in the demonstration contains information about photos and their metadata, a few such documents are shown below.

{"name":"fish.jpg","created_at":"Fri, 24 Dec 2010 06:35:03 GMT",  "user":"bob","type":"jpeg","camera":"nikon",   "info":{"width":100,"height":200,"size":12345},"tags":["tuna","shark"]}  {"name":"trees.jpg","created_at":"Fri, 24 Dec 2010 06:33:45 GMT",  "user":"john","type":"jpeg","camera":"canon",  "info":{"width":30,"height":250,"size":32091},"tags":["oak"]}

If you click on the “map/reduce” tab, you will find a drop-down for selecting pre-written JavaScript functions for performing certain operations. Let’s take a look at a query to compute the sum of all the image sizes. Here’s the map function:

function(doc) { 	emit("size",; }

And the reduce function:

function(keys, values, rereduce) { 	return sum(values); }

With a variety of NoSQL databases to choose from, and developers the unwitting kingmakers, easy demonstration interfaces such as JSCouch can help smooth the path to technology adoption.

tags: , , ,
  • David Karger

    I’ve always been a fan of keeping things simple and in the browser. Along the lines of the photo browser you describe, we built a general-purpose information management tool that lives entirely in a web page. It includes a database, rich information visualizations (lists, albums, maps, timelines, charts), faceted browsing, and a WYSIWYG editor (based on tinymce) that lets you modify the way the information is presented. After you edit it, you persist the changes to the data and visualization by saving the web page.

    You can try it out at . It would be fun to combine it with couchDB, which would give it access to more powerful query operators than the ones we’ve implemented.