Sunday, September 20, 2015

Couch DB

What is CouchDB ?
  • It's an open source NO-SQL database as we don't use the legacy SQL Statements to query the DB.
  • It's a document storage Database as it holds the collection of independent documents [Document holds the data as the JSON Objects.].
  • Uses HTTP based API  for CRUD Operations.
Why CouchDB?
  • Have HTTP Based API to retrive the Data.
  • Follows Atomicity while saving the Data into Documents. Either all Data is saved or not saved at all.
  • Have Views that would allow to join many Documents to have multiple view of Data.
Setting up CouchDB
  • We can either have our own DB at Cloud using http://www.iriscouch.com/
  • Else Avail the local CouchDB from http://couchdb.apache.org/. Once Setup, access the Futon(admin console) at http://127.0.0.1:5984/_utils/ if it's been locally setup.
  • CouchDB allows you to write a client side application that talks directly to the Couch without the need for a server side middle layer, significantly reducing development time.
cURL
  • cURL is a tool available in Linux, Mac, Windows etc Platforms and can be used to transfer the data to a CouchDB via HTTP, Telnet, HTTPS, FTP and LDAP Protocols.  
  • Below command returns the UI part of accessed URL.

curl https://www.facebook.com

Flags in Curl

-X Flag = tells curl to use the provided request method and not the default i.e. GET
-d Flag = helps in passing the data along with  Request methods.

curl -X PUT http://heckteck.com -d userid=ttt -d password=ttt

-o Flag writes the requested url into a file

curl -o index.html https://www.facebook.com

Interactions with CouchDB via cURL
  • Check Connections with CouchDB [ curl http://127.0.0.1:5984/ ]
  • List all DBs of CouchDB [curl -X GET http://127.0.0.1:5984/_all_dbs ]
  • Create a new DB in CouchDB [curl -X PUT http://127.0.0.1:5984/myDB ]
  • Verify if the new DB been created [curl -X GET http://127.0.0.1:5984/_all_dbs]
  • Get the Information about the newly Created DB [curl -X GET http://127.0.0.1:5984/myDB ]
  • Create a new Document in CouchDB with Document ID[ curl -X PUT http://127.0.0.1:5984/test_suite_db/"testerid" -d '{"lockerno": "123411","facility":"inot"}' ]
  • Return values of a Document with Document ID. [ curl -X GET http://127.0.0.1:5984/test_suite_db/"testerid" ]
  • Delete a Document with Document Id and Revision Id [ curl -X DELETE http://127.0.0.1:5984/test_suite_db/testerid?rev=4-a58140641ffbeffa9c5d552265067016 ]

Maps & Reduce

Open CouchDB -> Select any DB -> Navigate to Views Drop Down on right and Select Temporary Views.

Sample Maps

function (doc) {
    if (doc.name === "Ananda") {
        emit(doc.name,doc.value);
    }
}

Sample Reduce

function (keys,value) {
    return sum(value);
}

[ Note: This would iterate through all the Documents in that DB and return the filtered result. The filtered result then get reduced by reduce function. ]

Map and Reduce Functions offer great variations to the Document to the document Structure and the resultant is called View in CouchDB Terminology.

Default Sort Order is based on the IDs to makes the Views efficient.

Value from the Map Function gets passed onto the second attribute [normally marked as value as above] of Reduce Function.

Group Level Re-reduce:

In certain map function, you may have group of keys as Array as below:

function(doc) {
var store, price, value;
if (doc.item && doc.price)
{
for (store in doc.price) {
                   emit([doc._id,store],{store:store,price:doc.price[store]});
        }
}
}

Array of Keys in Map Emit Function can be helpful in performing group level re-reduce emit([doc.name,doc._id],doc.value);

Let's then have the sample reduce function as below:

function(keys, values) {
    return values.length;
}

Now if we enable reduce check button, we can find the value filtered producing the key as [doc._id,store]. The default group level reduce is exact. In case of group level 1 reduce, we have only one key [doc._id] available. When on group level 2 reduce, we have all two keys available.


B Tree Index:

CouchDB employs B Tree indexing to store and retrive the Values internally. You can have a look at Wiki Page for more info on B Tree [https://en.wikipedia.org/wiki/B%2B_tree]











No comments:

Post a Comment