Advertisement
Advertisement

More Related Content

Advertisement

Introduction to CouchDB

  1. Introduction to CouchDB By Bogdan Sabau, SoftVision
  2. INTRODUCTION
  3. Databases
  4. Databases Relational
  5. Databases Object oriented
  6. Databases Graph
  7. Databases Key-Value based
  8. Databases Document based
  9. WHAT IS COUCHDB?
  10. • A scalable, fault-tolerant, and schema-free document-oriented database • A RESTful HTTP/JSON API accessible from many programming libraries and tools • Incremental Map/Reduce queries written in any language (JavaScript support built-in) • Incremental and flexible replication with conflict management
  11. SCHEMA FREE DOCUMENT
  12. What does it means? • It means that you don’t need to think to much upfront about what your data will be structured like. You don’t need to think about relations or what might be needed for the future. You just put the data as it currently is into the database and in case it changes, you just change the documents.
  13. Relational model id firstName lastName 1 Danila Prepeleac 2 Gheorghe Doja customerId phoneTypeId phoneNumber 1 1 004075123321 1 2 064500100 2 1 +4072600300 Customers Phones id Type 1 Mobile 2 Home PhoneTypes
  14. Document based { "_id":"1", "_rev":"1c7762a779effe1398d8d2ea36ff958a6", "firstName":"Gheorghe", "lastName":"Doja", "phones": { "mobile":"004075123321", "home":"064500100„ } }
  15. Document based { "_id":"1", "_rev":"1c7762a779effe1398d8d2ea36ff958a6", "firstName":"Gheorghe", "lastName":"Doja", "phones": { "mobile":"004075123321", "home":"064500100„ } } Key, unique, it can me autogenerated (UUID) or the user can give it
  16. Document based { "_id":"1", "_rev":"1c7762a779effe1398d8d2ea36ff958a6", "firstName":"Gheorghe", "lastName":"Doja", "phones": { "mobile":"004075123321", "home":"064500100„ } } Key, unique, it can me autogenerated (UUID) or the user can give it Revision number. Yes! It has revisions!
  17. CRUD OPERATIONS
  18. CRUD Operations • CouchDB speaks the language of the web: REST, HTTP, and JSON are how CouchDB works natively • Calls are made to the database via HTTP (we can use anything that talks HTTP) • Responses come back as JSON
  19. Create database: bash$ curl -X PUT http://localhost:5984/contacts {"ok":true} bash$ curl -X PUT http://localhost:5984/contacts/1 -d '{"firstName":"Gheorghe","lastName":"Doja","phones":{"mobile":"004075123321","home":"4500100"}}' {"ok":true,"id":"1","rev":"1-c7762a779effe1398d8d2ea36ff958a6"} bash$ curl -X GET http://localhost:5984/contacts/_all_docs {"total_rows":1,"offset":0,"rows":[ {"id":"1","key":"1","value":{"rev":"1-c7762a779effe1398d8d2ea36ff958a6"}} ]} Get all documents from a database bash$ curl -X GET http://localhost:5984/contacts/1 {"_id":"1","_rev":"1- c7762a779effe1398d8d2ea36ff958a6","firstName":"Gheorghe","lastName":"Doja","phones":{"mobile":"004075123321","home":"064500100 "}} Get a specific document bash$ curl -X PUT http://localhost:5984/contacts/1 -d '{"_id":"1","_rev":"1-c7762a779effe1398d8d2ea36ff958a6","firstName":"Gheorgh e","lastName":"Doja","phones":{"mobile":"004075123000","home":"+4064500100"}}‘ {"ok":true,"id":"1","rev":"2-875f4f380fe5e3eeb5fbaeb495fc3599"} Updates a document bash$ curl -X DELETE http://localhost:5984/contacts/1?rev="2-875f4f380fe5e3eeb5fbaeb495fc3599" {"ok":true,"id":"1","rev":"3-de88253ac3cb9463aac609d5fdfd3135"} Delete a document Create a new document:
  20. Updating documents • Highly concurent • It can serve a high number of parallel requests. • MVCC (Multiversion concurrency control)
  21. Updating a document _rev: 1 CouchDB
  22. Updating a document _rev: 1 CouchDB _rev: 1_rev: 1 DerpDerpina
  23. Updating a document _rev: 1 CouchDB _rev: 1_rev: 1 DerpDerpina
  24. Updating a document _rev: 1 CouchDB _rev: 1_rev: 1 DerpDerpina
  25. Updating a document _rev: 2 CouchDB _rev: 1 Derpina
  26. Updating a document _rev: 2 CouchDB _rev: 1 Derpina
  27. Updating a document _rev: 2 CouchDB _rev: 1 Derpina
  28. Updating a document _rev: 2 CouchDB _rev: 1 Derpina
  29. Updating a document _rev: 2 CouchDB _rev: 1 Derpina _rev: 2 Derpina
  30. Updating a document _rev: 2 CouchDB _rev: 2 Derpina
  31. Updating a document _rev: 2 CouchDB _rev: 2 Derpina
  32. Updating a document _rev: 3 CouchDB
  33. MAP/REDUCE FUNCTIONS
  34. Views • Are stored as an accessible web resource on disk and incrementally updated as well as replicated with the database • Each view defines a map function, and can define a reduce function
  35. One simple view function(doc) { if (doc.ocupation == "domnitor") { emit(doc.ocupation, doc.firstName + ' ' + doc.lastName); } } input output key value
  36. { _id: "534ce91c445da85f636284856c000c77", _rev: "1-36d8948a4dc06aad20dfc9a95ad12de1", firstName: "Bogdan", lastName: "Sabau", ocupation: "sportiv" } { _id: "534ce91c445da85f636284856c001087", _rev: "2-eb737c5882c8e7e351a4f19678eeb9f7", firstName: "Gheorghe", lastName: "Doja", ocupation: "nobil" } { _id: "534ce91c445da85f636284856c001c49", _rev: "1-35972e6ee7c7ab828d4110c3dbc3a4de", firstName: "Iacob", lastName: "Heraclide", ocupation: "domnitor" } { _id: "534ce91c445da85f636284856c001eb2", _rev: "1-91c5e4f0cc0836b5f4d0e4bcf3d66467", firstName: "Alexandru", lastName: "Lapusneanu", ocupation: "domnitor" } { _id: "534ce91c445da85f636284856c002529", _rev: "1-a58e090b6138abf648eb65cbcd077ed7", firstName: "Constantin", lastName: "Karadja", ocupation: "diplomat" }
  37. Output in Futon
  38. Reduce function
  39. Reduce function
  40. REPLICATION
  41. Replication • Peer to peer • Online/Offline • Conflict detection and resolution • Any number of nodes
  42. Unidirectional
  43. Bidirectional
  44. Continuous 8
  45. Filtered Filter function
  46. QUESTIONS?
Advertisement