Showing posts with label couchdb-1_4_0. Show all posts
Showing posts with label couchdb-1_4_0. Show all posts

Sunday, November 8, 2015

Light learning into CouchDB

Today we will explore another opensource database, CouchDB. First, let's understand what is Apache CouchDB

Apache CouchDB, commonly referred to as CouchDB, is an open source database that focuses on ease of use and on being "a database that completely embraces the web".[1] It is a document-oriented NoSQL database that uses JSON to store data, JavaScript as its query language using MapReduce, and HTTP for an API.[1] CouchDB was first released in 2005 and later became an Apache project in 2008.

Actually couch is an acronym for Cluster Of Unreliable Commodity Hardware.  If you are using debian based linux distribution, it will be very easy, just apt-get install couchdb . If not, you can check out this link on how to install for other linux distribution. Once installed, make sure couchdb is running.

 root@localhost:~# /etc/init.d/couchdb status  
 ● couchdb.service - LSB: Apache CouchDB init script  
   Loaded: loaded (/etc/init.d/couchdb)  
   Active: active (exited) since Thu 2015-08-20 23:07:30 MYT; 14s ago  
    Docs: man:systemd-sysv-generator(8)  
 Aug 20 23:07:28 localhost systemd[1]: Starting LSB: Apache CouchDB init script...  
 Aug 20 23:07:28 localhost su[14399]: Successful su for couchdb by root  
 Aug 20 23:07:28 localhost su[14399]: + ??? root:couchdb  
 Aug 20 23:07:28 localhost su[14399]: pam_unix(su:session): session opened for user couchdb by (uid=0)  
 Aug 20 23:07:30 localhost couchdb[14392]: Starting database server: couchdb.  
 Aug 20 23:07:30 localhost systemd[1]: Started LSB: Apache CouchDB init script.  

very easy, you can quickly check the version of couchdb that is running. just put the following link into the browser.

I have version 1.4.0 running. Let's create a database. If you have terminal ready, you can copy and paste below and check the database created.

 user@localhost:~$ curl -X PUT  
 user@localhost:~$ curl -X PUT  
 {"error":"file_exists","reason":"The database could not be created, the file already exists."}  
 user@localhost:~$ curl  
 user@localhost:~$ curl -X GET  

couchdb has json output, key value output and error handling is pretty good. Very speedy too! okay now, let's try on crud on couchdb and we will do that using curl.

 user@localhost:~$ curl -X POST -H "Content-Type: application/json" --data '{ "text" : "Wikipedia on CouchDB", "rating": 5 }'  
 user@localhost:~$ curl -X GET -H "Content-Type: application/json"  
 {"_id":"4c6a6dce960e16aba7e50d02c9001241","_rev":"1-80fd6f7aeb55c83c8999b4613843af5d","text":"Wikipedia on CouchDB","rating":5}  

first, we create a new document by http post with data in json format. Then using the id that couchdb generated, we get that document data back.

 user@localhost:~$ curl -X PUT -H "Content-Type: application/json" --data '{ "text" : "Wikipedia on CouchDB", "rating": 6, "_rev": "1-80fd6f7aeb55c83c8999b4613843af5d" }'  
 user@localhost:~$ curl -X GET -H "Content-Type: application/json"  
 {"_id":"4c6a6dce960e16aba7e50d02c9001241","_rev":"2-b7248b6af9b6efcea5a8fe8cc299a85c","text":"Wikipedia on CouchDB","rating":6}  
 user@localhost:~$ curl -X PUT -H "Content-Type: application/json" --data '{ "views" : 10, "_rev": "2-b7248b6af9b6efcea5a8fe8cc299a85c" }'  
 user@localhost:~$ curl -X GET -H "Content-Type: application/json"  

As you can read above, we update this document twice, first, we update by increase rating to 6 and appending _rev using a unique id generated from couchdb. If this update is a success, notice that rev is increase by 1? note 2- on rev. On the second update, we update the view to 10 but essentially in couchdb, everything in this doc is wipe and insert a new key called views. So note if you want to update a document, you should send in all existing value before do the update.

Finally, now we delete this document. Very easy, see below.

 user@localhost:~$ curl -X DELETE -H "Content-Type: application/json"  
 user@localhost:~$ curl -X GET -H "Content-Type: application/json"  
 user@localhost:~$ curl -X DELETE  
 user@localhost:~$ curl -X GET  

couchdb is very good and efficient of handling document, it certainly definitely earn it spot for beginner to dwelve deeper of its capability. If you think so, take a look at this link. That's it for today, have fun learning!