- Sai Divya Panditi - Priyanka Yechuri 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
CAP theorem – Pick two Consistency All database clients see the same data, even with concurrent updates. Availability All database clients are able to access some version of the data. Partition tolerance The database can be split over multiple servers.
CAP theorem
Introduction Created By : Damien Katz Year : 2005 Language : Erlang License : Apache Software Foundation(2008)
Introduction... NoSQL Database .....Uses Map/Reduce queries written in javascript
Architecture
NoSQL Databases Schema-Free Distributed Open Source Horizontally Scalable Easy Replication Support
NoSQL Timeline
Document-Oriented DBMS Data is stored in documents ......and not in relations like an RDBMS
SQL vs CouchDB SQL CouchDB Relational Non-Relational Tables Documents with types Rows and Columns Document Fields SQL Query Engine Map / Reduce Engine
CouchDB Features Data Representation - Using JSON Interaction - Futon / CouchDB API Querying - Map / Reduce Design Documents - Application code(Language : Javascript) Documents can have attachments
JSON Stands for Javascript Object Notation Derived from Javascript scripting language Used for representing simple data structures and associative arrays
JSON..... Example: { "firstName": "John", "lastName": "Smith", "age": 25, "phoneNumber": [ { "type": "home", "number": "212 555-1234" }, "type": "fax", "number": "646 555-4567" } ] }
CouchDB Core API (Command Line Utility ) Server API Database API Document API Replication API
HTTP API Messages are self-described via HTTP Headers and HTTP Status Codes. URIs identify resources. HTTP Methods define operations on the resources.
HTTP Request Methods Method Description PUT GET POST DELETE COPY PUT requests are used to create new resources where the URI of the request is different to the resource that is to be created. GET requests are used to request data from the database. POST requests are used to update the existing data, at the same resource the URI is requested from. DELETE requests to delete databases and documents. Copies one resource to another resource.
HTTP Status Codes Status Code Description 200 (OK) 201 (Created) 304 (Not Modified) 400 (Bad Request) 404 (Not Found) 405 (Method Not Allowed) 409 (Conflict) 412 (Precondition Failed) 500 (Internal Server Error) The request was successfully processed. The document was successfully created. The document has not been modified since the last update. The syntax of the request was invalid. The request was not found. The request was made using an incorrect request method. The request failed because of a database conflict. could not create a database- a database with that name already exists. The request was invalid and failed, or an error occurred within the CouchDB server.
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"}
Curl Command - Database API Command to get a list of Databases : curl -X GET http://127.0.0.1:5984/_all_dbs
Creating and retrieving Creating the database "albums": curl -X PUT http://localhost:5984/albums Creating the document "album1": curl -X PUT http://localhost:5984/albums/album1 -d @- { "artista": "Megadeth", "titulo": "Endgame", "anio": 2009 } <EOF> // en Windows es ^z y en Unix ^d Retrieving the created document: curl -X GET http://localhost:5984/albums/album1
Updating (1) For updating a document: Give the last version Otherwise an error (code 409) will be generated, as shown in the following example: curl -X PUT http://localhost:5984/albums/album1 -d @- { "artista": "Megadeth", "titulo": "Endgame", "anio": 2010 } ^z
Updating (2) The attribute "_rev" specifies the version that will be updated: curl -X PUT http://localhost:5984/albums/album1 -d @- { "_rev": "1-142438dc8c583cda2a1f292c62291215", "artista": "Megadeth", "titulo": "Endgame", "anio": 2010 } ^z
Deleting (1) Delete the document "album1": curl -X DELETE http://localhost:5984/albums/album1?rev=2- d05127b44500ec19a2e5a25adc610380 If you try to retrieve it, an error is generated: curl -X GET http://localhost:5984/albums/album1 {"error":"not_found","reason":"deleted"} You have access to the version generated by the deletion operation: curl -X GET http://localhost:5984/albums/album1?rev=3- fac16c94309ed5ff842ffa89cc6048b1 {"_id":"album1","_rev":"3- fac16c94309ed5ff842ffa89cc6048b1","_deleted":true}
Deleting (2) We purge the document from the database: curl -X POST -H "Content-Type: application/json" http://localhost:5984/albums/_purge -d @- { "album1": ["3-fac16c94309ed5ff842ffa89cc6048b1"] } We try to query the version again: curl -X GET http://localhost:5984/albums/album1?rev=3- fac16c94309ed5ff842ffa89cc6048b1 {"error":"not_found","reason":"missing"}
Any binary type can be stored by adding it to a document Attachments (1) Any binary type can be stored by adding it to a document Let us create again "album1": curl -X PUT http://localhost:5984/albums/album1 -d @- { "artista": "Megadeth", "titulo": "Endgame", "anio": 2010 } The method HTTP PUT is used for attaching a file to the document using the attribute "cover.jpg": curl -X PUT -H 'Content-Type: image/jpg' --data-binary @300px- Endgame_album_art.jpg http://localhost:5984/albums/album1/cover.jpg?rev="1- 8a015dd26403219af66f05542cb540b2"
On adding an attachment to a document its version number changes: Attachments (2) On adding an attachment to a document its version number changes: For adding an attachment it is imperative to specify the version number of the document object Whe an attachment is created, the special attribute "_attachments” is created The method GET enables the retrieval of the attachment through the corresponding attribute: curl -X GET http://localhost:5984/albums/album1/cover.jpg?rev="2- 31e1ce62601aac5b9de7059788361641" > tmp.jpg
Views are useful for many purposes: Filtering the documents in your database to find those relevant to a particular process. Extracting data from your documents and presenting it in a specific order. Building efficient indexes (B-Trees)to find documents by any value or structure that resides in them. Use these indexes to represent relationships among documents. Views you can make all sorts of calculations on the data in your documents. E.g., if documents represent your company’s financial transactions, a view can answer the question of what the spending was in the last week, month, or year.
Defining views (1) Views are based on the working model MapReduce: Map and reduce function are specified in javascript Built-in views are provided curl -X GET http://localhost:5984/albums/_al l_docs
Example: defining a view curl -X PUT http://localhost:5984/albums/_design/vistas1 -d @- { "language": "javascript", "views": { "por_anio": { "map": "function( doc ) { if( doc.anio ) { emit( doc.anio, 1 );}}", "reduce": "function( keys, values, rereduce ) {return sum( values );}" }
Reduce values retrieved without considering the keys: Example: using a view Reduce values retrieved without considering the keys: curl http://localhost:5984/albums/_design/vistas1/_view/por_anio curl -X GET http://localhost:5984/albums/_design/vistas1/_view/por_anio Reduce the values retrieved considering the values of the different keys: curl http://localhost:5984/albums/_design/vistas1/_view/por_anio?group=true
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
Futon Interface Demo...
Design Documents Contains application code They are like normal json documents but prefixed by _design/ CouchDB looks for views and other application functions here...
Views Used for extracting data we need for a specific purpose Example : function(doc){ if(doc.Bname) { emit(doc.id,doc.Bname); }
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
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"
Map Functions For Example : Consider the following documents Bname : Oracle Category : CS Author : abc Edition : 2007 Document-2 id : 2 Bname : Networks Category : CS Author : xyz Edition : 2001 Document-3 id : 3 Bname : Circuits Category : Electronics Author : abcd Edition : 2004 Document-4 id : 4 Bname : AI Category : CS Author : pqrs Edition : 2010
Map Functions… Output : Key Value 1 Oracle 2 Networks 3 Circuits 4 AI
Map Functions… Map Function : map:function(doc) { emit(doc.Bname, doc.id); } Output : AI 4 Circuits 3 Networks 2 Oracle 1
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 }
Security Database Admins Validation Functions
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.
Who uses CouchDB?
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
References CouchDB - The Definitive Guide , J. Chris Anderson, Jan Lehnardt & Noah Slater Beginning CouchDB, Joe Lennon
Thank You...