1 / 51

CouchDB

CouchDB. - Sai Divya Panditi - Priyanka Yechuri. Overview. Introduction SQL vs CouchDB CouchDB Features CouchDB Core API Futon Security Application. Overview. Demo Code Advantages DisAdvantages Iris Couch Conclusion References. Introduction. Created By : Damien Katz

ismet
Download Presentation

CouchDB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CouchDB - Sai Divya Panditi - Priyanka Yechuri

  2. Overview • Introduction • SQL vs CouchDB • CouchDB Features • CouchDB Core API • Futon • Security • Application

  3. Overview • Demo • Code • Advantages • DisAdvantages • Iris Couch • Conclusion • References

  4. Introduction • Created By : Damien Katz • Year : 2005 • Language : Erlang • License : Apache Software Foundation(2008)

  5. Introduction... • NoSQL Database .....Uses Map/Reduce queries written in javascript

  6. NoSQL Databases • Schema-Free • Distributed • Open Source • Horizontally Scalable • Easy Replication Support

  7. NoSQL Timeline

  8. Document-Oriented DBMS • Data is stored in documents ......and not in relations like an RDBMS

  9. SQL vs CouchDB

  10. CouchDB Features • Data Representation - Using JSON • Interaction - Futon / CouchDB API • Querying - Map / Reduce • Design Documents - Application code(Language : Javascript) • Documents can have attachments

  11. JSON • Stands for Javascript Object Notation • Derived from Javascript scripting language • Used for representing simple data structures and associative arrays

  12. JSON..... • Example: { • "firstName": "John", • "lastName": "Smith", • "age": 25, • "phoneNumber": [ • { • "type": "home", • "number": "212 555-1234" • }, • { • "type": "fax", "number": "646 555-4567" • } ] • }

  13. CouchDB Core API (Command Line Utility ) • Server API • Database API • Document API • Replication API

  14. HTTP API • Messages are self-described via HTTP Headers and HTTP Status Codes. • URIs identify resources. • HTTP Methods define operations on the resources.

  15. HTTP Request Methods

  16. HTTP Status Codes

  17. Curl Command - Server API • Command to check if CouchDB is working at all? curl http://127.0.0.1:5984/ • Response : {"couchdb":"Welcome","version":"0.10.1"}

  18. Curl Command - Database API • Command to get a list of Databases : curl -X GET http://127.0.0.1:5984/_all_dbs • Command to create a Database : curl -X PUT http://127.0.0.1:5984/DB_name • Curl's -v option : curl -vX PUT http://127.0.0.1:5984/DB_name • Command to destroy a Database : curl -X DELETE http://127.0.0.1:5984/DB_name

  19. Curl Command - Document API • Command to create a document : curl -X PUT http://127.0.0.1:5984/albums/ 6ert2gh45ji6h6tywe324743rtbhgtrg \ -d '{"title":"abc","artist":"xyz"}' • Command to get a UUID : curl -X GET http://127.0.0.1:5984/_uuids • Command to retrieve a Document : curl -X GET http://127.0.0.1:5984/albums/ 6ert2gh45ji6h6tywe324743rtbhgtrg

  20. Curl Command - Document API • Command for attachments : curl -vX PUT http://127.0.0.1:5984/albums/ 6ert2gh45ji6h6tywe324743rtbhgtrg/ \ artwork.jpg?rev=2-2739352689 --data-binary @artwork.jpg -H "Content-Type: image/jpg" • To view the image in the browser, URL is : http://127.0.0.1:5984/albums/6ert2gh45ji6h6 tywe324743rtbhgtrg/artwork.jpg

  21. Curl Command-Replication API • Command to replicate a Database : curl -vX POST http://127.0.0.1:5984/ _replicate \ -d '{"source":"albums","target": "albums_replica"}'

  22. Futon • Built-in admin interface • Access to all CouchDB features • Create and Destroy databases • Create, View and Edit Documents • Compose and run Map / Reduce Views • Replicate a Database

  23. Futon Interface Demo...

  24. Design Documents • Contains application code • They are like normal json documents but prefixed by _design/ • CouchDB looks for views and other application functions here...

  25. Views • Used for extracting data we need for a specific purpose • Example : function(doc){ if(doc.Bname) { emit(doc.id,doc.Bname); } }

  26. View Functions... • Map - single parameter - doc • emit(key,value) - built-in function • Results of emit() are sorted by key • We query the views to produce the desired result • When we query a view, it's run on every document in the database for which view is defined • View result is stored in a B-tree

  27. View Functions… • B-tree for the view is built only once and all the subsequent queries will just read the B-tree instead of executing the map function again • Used to find documents by any value or structure that resides in them • Using the URI, we can retrieve the exact data we need. For Example : /books/_design/docs/_view/by_Bname?key="Circuits"

  28. Map Functions • For Example : Consider the following documents

  29. Map Functions… • Output :

  30. Map Functions… • Map Function : map:function(doc) { emit(doc.Bname, doc.id); } • Output :

  31. Reduce Function • This function operates on the sorted rows emitted by map view functions. • Predefined Reduce Functions: • _sum, _count etc. • Example: • function(keys,values){ • return sum(values); //gives aggregate values • }

  32. Security • Database Admins • Validation Functions

  33. Database Admin Demo…

  34. Validation Function • Uses the function validate_doc_update(). • If the validation function raises an exception, the update is denied else the updates are accepted. • Document validation is optional.

  35. Who uses CouchDB?

  36. Application • GSUBooks.com

  37. Code - add.js add.js: $(document).ready( function() { //Event handler crud stuff $('input#addId').click(function(e) { if ($('#bookId').val().length == 0) { return; } var bookdoc = { booknm: $('#bookId').val(), authornm: $('#authorId').val(), category: $('#categoryId').val(), edition: $('#editionId').val(),quantity: $('#quantityId').val() }

  38. Code - add.js... • db.saveDoc(bookdoc, { success: function(resp) • { checkList(); //refreshes the database with new book } • });

  39. Code-delete.js delete.js $(document).ready ( function() { $('input#borrowId').click(function(e) { if ($('#idId').val().length == 0) { return; } var bookdoc = { _id: $('#idId').val(), _rev: $('#revId').val() } db.removeDoc(bookdoc, { success: function(resp) {

  40. Code-delete.js checkList(); //refreshes the database with the remaining books alert("Book has been borrowed Successfully!!"); }}); clearDocument(); });}); function clearDocument() { $('#idId').val(''); $('#revId').val(''); $('#bookId').val(''); $('#authorId').val(''); $('#categoryId').val(''); $('#editionId').val(''); $('#quantityId').val('');};

  41. Code - Update.js • update.js • $(document).ready(function() • { $('input#updateId').click(function(e) • { if ($('#idId').val().length == 0) • { return; }varbookdoc = { • _id: $('#idId').val(), _rev: $('#revId').val(),booknm: $('#bookId').val(),

  42. update.js... authornm:$('#authorId').val(), category:$('#categoryId').val(), edition:$('#editionId').val(), quantity:$('#quantityId').val() } db.saveDoc(bookdoc, { success: function(resp) { checkList(); } }); });

  43. Code - Display.js display.js $(document).ready(function() { checkList();}); function checkList() { $("table#itemData").empty(); db.view("myfirstDesign/myfirstView", { success: function(data) { $('table#itemData').append('<tr><th><font color="black">Book Name</font></th><th><font color="black">Author Name</font></th><th><font color="black">Category</font></th><th><font color="black">Edition</font></th>

  44. Code - Display.js <th><font color="black">Quantity</font></th></tr>'); data.rows.map(function(row) { $('table#itemData').append('<tr><td id="'+row.value._id+'"align="center"><font +row.value.booknm +'</font></td><td align="center"><font color="navy">' +row.value.authornm +'</font></td><td align="center"><font color="navy">' +row.value.category +'</font></td><td align="center"><font color="navy">' +row.value.edition +'</font></td><td align="center"><font color="navy">' +row.value.quantity +'</font></td></tr>'); $('#'+row.value._id).click(function() { $('#idId').val(row.value._id);

  45. Code - Display.js $('#revId').val(row.value._rev); $('#bookId').val(row.value.booknm); $('#authorId').val(row.value.authornm); $('#categoryId').val(row.value.category); $('#editionId').val(row.value.edition); $('#quantityId').val(row.value.quantity); return false; }); }); } });}

  46. Advantages / DisAdvantages • Features • Not easy to learn especially if the user is familiar with SQL • Security is weak • Temporary views on large datasets are very slow. • Replication of large databases may fail • Documents are quite large as the data is represented using “JSON” format

  47. Iris Couch • Cloud CouchDB hosting • Free service

  48. Iris Couch Demo…

  49. References • CouchDB - The Definitive Guide , J. Chris Anderson, Jan Lehnardt & Noah Slater • Beginning CouchDB, Joe Lennon

More Related