Chapter 2. MapReduce

As the name suggests, MapReduce consists of a Map step and a Reduce step. Both the Map and Reduce steps can each be distributed in a way that takes advantage of the multiple processor cores that are found in modern hardware, allowing CouchDB to efficiently index your data. As documents are created, updated, and deleted, CouchDB is smart enough to run only modified documents through the Map step, reindexing only what has changed. The results of Reduce functions can often be cached as well.

We will use an example database named books in this chapter. To create this database using Futon (assuming CouchDB is installed on your local machine):

  1. Navigate to http://localhost:5984/_utils/ using your web browser.

  2. Click “Create Database …”.

  3. Enter books for the value of the “Database Name” field and click “Create” (see Figure 2-1).

Creating a new database using Futon

Figure 2-1. Creating a new database using Futon

Alternatively, you can create the books database using cURL:

curl -X PUT http://localhost:5984/books

The response:

{"ok":true}

Temporary Views

Map and Reduce are written as JavaScript functions that are defined within views. You can use a temporary view during development but should switch to using a view that is saved permanently for any real-world application. Temporary views can be very slow once you have more than a handful of documents. Views that are saved permanently are defined within design document ...

Get Writing and Querying MapReduce Views in CouchDB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.