O'Reilly logo

Clojure Programming by Brian Carper, Christophe Grand, Chas Emerick

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Views

CouchDB does not offer SQL or anything similar for performing ad-hoc queries. On their own, documents may only be indexed by a single “primary” string key.

The alternative provided by CouchDB is called views. Views are very similar in concept to the materialized views offered by some relational database systems. Views:

  • Are stored and accessed separately from their “source” database

  • Are defined programmatically (using almost any language, including Clojure as we’ll see shortly) and ahead of time

  • Reflect changes to the source database’s documents upon access

The primary insight behind CouchDB’s views is that nearly all of an application’s data access can be enumerated, and so the flexibility (and attendant runtime cost) associated with SQL and other ad-hoc query mechanisms is generally unwarranted. In contrast, CouchDB views must always be defined in the database prior to accessing them,[380] with the benefit that accessing them will always be extremely fast, regardless of the amount of processing needed to produce the views’ data.

To get started with views, let’s load a dataset of hypothetical logging messages into a new database, named logging. We’ll use Clutch’s bulk-update function for this, which hooks into CouchDB’s _bulk_docs API; this is the most efficient route for loading large amounts of data into CouchDB:

(clutch/bulk-update (create-database "logging") [{:evt-type "auth/new-user" :username "Chas"} {:evt-type "auth/new-user" :username "Dave"} {:evt-type "sales/purchase" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required