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", doc.info.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: , , , ,