couchdb

CouchDB – a NoSQL DB With a Powerfull Rest API

CouchDB is an open source NoSQL database, which comes with a powerfull Rest API. It stores data in JSON format. uses Javascripts as its query language using MapReduce. JSON data’s are very easy to understand and can be parsed very easily. This time i got a requirement to build a commandline utility to display results for an internal monitoring tool. The tool is a bit old, and it does not have any api. And finding latest the result is consuming more time using the front end. But one advantage was the frontend can display results in HTML and XML. So curl was able to query the server using the url and it can display the xml output. The url is unique for each, as it composed of few details like Location, SerialNo, Domain, OS etc, which is unique for each hosts. So i decided to have a local database which contains all these unique host details. For this purpose, i dont need any relational databases, and NoSQL databases are sufficient. So i decided to use CouchDB, because it comes with a wonderfull rest api.

Since i’m a RUBY lover, i decided to use this couchdb ruby wrapper. SInce my requirement with CouchDB was pretty simple, this wrapper itself was sufficient for me. This wrapper can be used to perform any operations like, reading,writing,deleting Documents as well as Databases. This wrapper basically uses the ruby http library to talk to the CouchDB’s rest api. By default couchdb is available in most of the Linux respositories. for windows and mac, the files are available in the CouchDB website itself.

for example, for ubuntu/debian, below command will install couchdb server.

sudo apt-get install couchdb -y

By default, the couchDB package comes with a web frontend called “Futon” which can be accessed via browsers ugin the below url,

http://localhost:5984/_utils/

Below is the screenshot of the Futon frontend.

futon

We can perform all the operations through this web interface also. So i created a database, and created a document for each host with required fields. Now these documents can be accessed via the rest api in JSON format. so that my ruby script can fetch all the necessary data and can compose the exact url for fetching the check results for corresponding hosts.

Below is a sample read operation that we can perform through our ruby scripts. But before using the operation, we need to include the wrapper into our script. For that we need to add ‘require ./couch.rb’, we can use both relative or absolute path. once the wrapper is included,we can start performing operations with the CouchDB.

server = Couch::Server.new("localhost", "5984")
res = server.get("/foo/document_id")
json = res.body
puts json

The operation will read the document and will convert it into JSON using the json ruby library. Now i can use the JSON data to collect the required fields so that my script can form the exact fetch url for each host and can perform the result fetch. There is couchdb gem for ruby, which can perform more operations.

Apart from this, CouchDB has another important feature ”MAPREDUCE”, yes it can perform MapReduce, i’m trying to find if mapreduce can help me, so that i can extend my app to do more things. Once if i get any weird idea with MapReduce i will udapte this blog with that also.

Standard