Install CouchDB & use with JSON and Map-Reduce
CouchDB is another offspring from the open-source, NoSQL, non-relational databases and is maintained under the Apache Foundation. It differs itself form the likes of MongoDB or Cassandra in many ways, except they all store data in so called “documents” that are in JSON format. Utilizing this format allows for data structures which can be hashes, lists, nested arrays and of course scalar values.
This added complexity results in more powerful features, mainly to have a db that is not just a single key/value pair, but it comes at a price of speed reduction. Interested? Learn how to install CouchDB.
CouchDB can be a little bit of a pain to install, because it needs a few prerequisites and they in turn have a few of their own quirks. This outline should help you get CouchDB with all it’s necessities installed. We’ve used MacOS, but you can substitute your OS where applicable.
The CouchDB source code and installer packages are downloadable here. As of this writing, version 0.11.2 is the latest stable version, with 1.0.1 just around the corner. You will also need Spider Monkey, Mozilla’s C implementation of JavaScript.
Installing SpiderMonkey
Once downloaded, extract the tarball and move into the sources folder:
tar -xzvf js-1.8.0-rc1.tar.gz cd js/src
If you’re installing SpiderMonkey on a Mac, there is a small hack to the jsprf.c file that has to be applied. Adding the following two lines (in orange below) after line 60 should resolve the build issue:
60: #define VARARGS_ASSIGN(foo, bar) VA_COPY(foo,bar)
61: #elif defined(va_copy)
62: #define VARARGS_ASSIGN(foo, bar)
63: #elif defined(HAVE_VA_LIST_AS_ARRAY)
Now we should have no problems running make:
make BUILD_OPT=1 -f Makefile.ref sudo sh sudo make BUILD_OPT=1 JS_DIST=/usr/local -f Makefile.ref export
Installing ICU
ICU is a library for International Components for Unicode and CouchDB relies on it. Let’s download it from icu-project.org here. Once downloaded, extract the package and move into the sources folder and change some permissions before building the code:
tar -xzvf icu4c-4_4_1-src.tgz cd icu/source chmod +x runConfigureICU configure install-sh ./runConfigreICU -h (select the suffix for your OS and and supply it as argument to runConfigureICU, eg: ./runConfigureICU MacOS) make sudo make install
Installing Erlang
Erlang is the language used to build CouchDB, and you’ll have to install it if it’s not yet present. Download Erlang version 14A (latest as of this writing) from erlang.org here or use wget or curl as in the steps below.
cd /tmp wget http://www.erlang.org/download/otp_src_R14A.tar.gz tar -xzvf otp_src_R14A.tar.gz cd otp_src_R14A ./configure --enable-hipe --enable-smp-support --enable-threads (--enable-darvin-universal for Mac) make sudo make install
Installing CouchDB
Now finally we’re ready to install CouchDB.
cd /tmp wget http://www.apache.org/dyn/closer.cgi?path=/couchdb/0.11.2/apache-couchdb-0.11.2.tar.gz tar -xzvf apache cd apache-couchdb-0.11.2 ./configure make sudo make install sudo couchdb start
Testing our CouchDB installation
Besides the countless client libraries written in almost any language, we can simply use Telnet to create a database and do some basic data manipulation to test our installation.
telnet localhost 5984
If there are no errors and you get a response like below, you’re all set.
Trying 127.0.0.1... Connected to localhost. Escape character is '^]'.
A good introduction to CouchDB and it’s commands can be found here.