Download presentation
Presentation is loading. Please wait.
1
- Sai Divya Panditi - Priyanka Yechuri
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
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.
5
CAP theorem
6
Introduction Created By : Damien Katz Year : 2005 Language : Erlang
License : Apache Software Foundation(2008)
7
Introduction... NoSQL Database
.....Uses Map/Reduce queries written in javascript
8
Architecture
9
NoSQL Databases Schema-Free Distributed Open Source
Horizontally Scalable Easy Replication Support
10
NoSQL Timeline
11
Document-Oriented DBMS
Data is stored in documents ......and not in relations like an RDBMS
12
SQL vs CouchDB SQL CouchDB Relational Non-Relational Tables
Documents with types Rows and Columns Document Fields SQL Query Engine Map / Reduce Engine
13
CouchDB Features Data Representation - Using JSON
Interaction - Futon / CouchDB API Querying - Map / Reduce Design Documents - Application code(Language : Javascript) Documents can have attachments
14
JSON Stands for Javascript Object Notation
Derived from Javascript scripting language Used for representing simple data structures and associative arrays
15
JSON..... Example: { "firstName": "John", "lastName": "Smith",
"age": 25, "phoneNumber": [ { "type": "home", "number": " " }, "type": "fax", "number": " " } ] }
16
CouchDB Core API (Command Line Utility )
Server API Database API Document API Replication API
17
HTTP API Messages are self-described via HTTP Headers and HTTP Status Codes. URIs identify resources. HTTP Methods define operations on the resources.
18
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.
19
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.
20
Curl Command - Server API
Command to check if CouchDB is working at all? curl Response : {"couchdb":"Welcome","version":"0.10.1"}
21
Curl Command - Database API
Command to get a list of Databases : curl -X GET
22
Creating and retrieving
Creating the database "albums": curl -X PUT Creating the document "album1": curl -X PUT { "artista": "Megadeth", "titulo": "Endgame", "anio": 2009 } <EOF> // en Windows es ^z y en Unix ^d Retrieving the created document: curl -X GET
23
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 { "artista": "Megadeth", "titulo": "Endgame", "anio": 2010 } ^z
24
Updating (2) The attribute "_rev" specifies the version that will be updated: curl -X PUT { "_rev": " dc8c583cda2a1f292c ", "artista": "Megadeth", "titulo": "Endgame", "anio": 2010 } ^z
25
Deleting (1) Delete the document "album1":
curl -X DELETE d05127b44500ec19a2e5a25adc610380 If you try to retrieve it, an error is generated: curl -X GET {"error":"not_found","reason":"deleted"} You have access to the version generated by the deletion operation: curl -X GET fac16c94309ed5ff842ffa89cc6048b1 {"_id":"album1","_rev":"3- fac16c94309ed5ff842ffa89cc6048b1","_deleted":true}
26
Deleting (2) We purge the document from the database:
curl -X POST -H "Content-Type: application/json" { "album1": ["3-fac16c94309ed5ff842ffa89cc6048b1"] } We try to query the version again: curl -X GET fac16c94309ed5ff842ffa89cc6048b1 {"error":"not_found","reason":"missing"}
27
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 { "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' Endgame_album_art.jpg a015dd af66f05542cb540b2"
28
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 31e1ce62601aac5b9de " > tmp.jpg
29
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.
30
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 l_docs
31
Example: defining a view
curl -X PUT { "language": "javascript", "views": { "por_anio": { "map": "function( doc ) { if( doc.anio ) { emit( doc.anio, 1 );}}", "reduce": "function( keys, values, rereduce ) {return sum( values );}" }
32
Reduce values retrieved without considering the keys:
Example: using a view Reduce values retrieved without considering the keys: curl curl -X GET Reduce the values retrieved considering the values of the different keys: curl
33
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
34
Futon Interface Demo...
35
Design Documents Contains application code
They are like normal json documents but prefixed by _design/ CouchDB looks for views and other application functions here...
36
Views Used for extracting data we need for a specific purpose
Example : function(doc){ if(doc.Bname) { emit(doc.id,doc.Bname); }
37
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
38
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"
39
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
40
Map Functions… Output : Key Value 1 Oracle 2 Networks 3 Circuits 4 AI
41
Map Functions… Map Function : map:function(doc) {
emit(doc.Bname, doc.id); } Output : AI 4 Circuits 3 Networks 2 Oracle 1
42
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 }
43
Security Database Admins Validation Functions
44
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.
45
Who uses CouchDB?
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
References CouchDB - The Definitive Guide , J. Chris Anderson, Jan Lehnardt & Noah Slater Beginning CouchDB, Joe Lennon
48
Thank You...
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.