Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Couchbase TLV Dev track 03- document your world


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Couchbase TLV Dev track 03- document your world

  1. 1. Developing with Couchbase: Document Your World Michael Nitschinger Engineer, Developer Solutions
  2. 2. What to Expect: • JSON Basics & Documents • Mind-set Changes • Building an application around JSON • Document Structuring & Modeling • Views and Indexes within Couchbase
  3. 3. JSON Basics – what is JSON? Java Script Object Notation • Created by Douglas Crockford • Text Based Format • Subset of JavaScript • Human Readable
  4. 4. JSON Basics – Why JSON? • Compact • Understandable • Data structures map nicely • Widely adopted
  5. 5. Supported JSON Types: Numbers: – (Int. & Floating Point) 22 & 55.2 Object: { String: ”name" : “Michael N.” “twitter" : “@daschl", ”age" : 25, "title" : ”Engineer" "A String" Boolean: {“value” : false} }
  6. 6. Supported JSON Types - Lists: List of Objects: Array: ["one", "two", "three"] Complex, Nested Objects: { tweet, tweet… } foos : [ { ”bar1":"value1", ”bar2":"value2" }, { ”bar3":"value3", ”bar4":"value4" } ]
  7. 7. JSON Documents within Couchbase • Documents • have a unique ID • can store JSON (also binary) as the value • have meta information attached • If it’s JSON, the document can be used in Views (secondary indexing)
  8. 8. JSON Document Structure meta Meta Information Including Key (ID) All Keys Unique and Kept in RAM at all times. { “id”: “”, “rev”: “1-0002bce0000000000”, “flags”: 0, “expiration”: 0, “type”: “json” } document Document Value Most Recent In Ram And Persisted To Disk { “uid”: 1234, “firstname”: “Michael”, “lastname”: “Nitschinger”, “age”: 25, “favorite_colors”: [“blue”, “green”], “email”: “” }
  9. 9. Objects Serialized to JSON and Back User Object string uid string firstname string lastname int age array favorite_colors string email User Object string uid string firstname string lastname int age array favorite_colors string email { “uid”: 1234, “firstname”: “Michael”, “lastname”: “Nitschinger”, “age”: 17, “favorite_colors”: [“blue”, “green”], “email”: “” } { “uid”: 1234, “firstname”: “Michael”, “lastname”: “Nitschinger”, “age”: 25, “favorite_colors”: [“blue”, “green”], “email”: “” } set() get()
  10. 10. The Mind-Set Change
  11. 11. The Move from Relational Modeling • Information spread across (multiple) Tables, • Rigid and inflexible schema • Records need to look the same • Complex JOINS, WHERE Clauses and ORDER BY Clauses Our ‘Recipe’ table uses “JOINS” to aggregate info from other Tables.
  12. 12. The Move to NoSQL • Gets and Sets are quick, do not fear them! • Schema is dictated by the application, not the database. • No ALTER_TABLE needed! • Each Document can look different.
  13. 13. Modeling an Application… The JSON way
  14. 14. Rate My Vine… Application in which people can • vote on other User’s Videos • see a Global Ranking of the Best and Worst Videos! Top Rated Vines Cooking w/ Hugh Fearnley-Whittingstall 176 I love doing Housework 143 What happened to Amanda Bynes 120 Random Access Memories 112 I don’t even know 107 Twerking gone wrong 98 Too cold to Dance 74 How To Scare Your Friends 37
  15. 15. Technology Used: • Open Source Sample App for Couchbase • Built on Ruby, Rails & Couchbase - Couchbase-Model Ruby Gem for Active-Record style (easy) data modeling - Puma as web server for concurrent connections
  16. 16. User.rb • Users must Auth with Twitter before Submitting Vines • We simply register their Name, Twitter Username & Avatar upon T-auth
  17. 17. How that looks as JSON in Couchbase: Key created by a hash of Twitter UID Explicit ‘type’ of Document • Standard JSON structure with simple String fields • This JSON is editable within the Couchbase Console
  18. 18. Vine.rb • Vines need a Name, A Video URL, a User and a Score
  19. 19. The Joys of a Flexible Schema! • Product Manager has informed us that we need to add a new field for Facebook Sharing into our Vine Videos • In a relational world, we would have to do schema migrations • In the Couchbase world, it’s much easier!
  20. 20. Again, the JSON within Couchbase Random Hash generated Key User_ID reference • User_ID included so we know who each Vine belongs to • Score is inside each Vine document. This brings it’s own challenges, but Couchbase solves them!
  21. 21. Optimistic Concurrency • We have chosen to have the Score inside each Vine doc. • We need to be able to deal with concurrent score updates. { “score" : 174 }
  22. 22. CAS – Compare and Swap • To handle the Concurrent updates, we can utilise Couchbase’s built in CAS concurrency controls. • We simply write a new Update method in our application controller to use the CAS value on update.
  23. 23. Document Relationships • Just as in SQL, our JSON Documents also have various types of ‘Relationship’. • For example, a User can own many Videos as a 1 to many relationship. video:1 { type: “vine”, title: “My Epic Video”, owner: “ingenthr” } video:2 { type: “vine”, title: “I NEED A HORSE!”, owner: “ingenthr” } user:ingenthr { type: “user”, name: “Matt Ingenthron”, id: “ingenthr” }
  24. 24. Single vs. Multiple Documents • Product Manager has informed us we need to add a Comment mechanism to our Vine Videos. • We need to decide the best way to approach this in JSON document design. Single Multiple Document Comment { vs. } Comment Comment
  25. 25. Single vs. Multiple - Single • Comments are nested within their respective Vine documents. • Great when we know we have a finite amount of Results. 7b18b847292338bc29 { "type": "vine", "user_id": "145237874", "title": "I NEED A HORSE", "vine_url": "", "video_url": "……, "score": 247, "comments": [ {"format": "markdown", "body": "I LOVE this video!"}, {"format": "markdown", "body": "BEST video I have ever seen!"}, ] }
  26. 26. Single vs. Multiple - Multiple • Comments are split from the parent document. • Comments use referential ID’s, incremented by 1 ::1 7b18b847292338bc29 7b18b847292338bc29 { "format": "markdown", "body": "I LOVE this video!” } { "type": "vine", "user_id": "145237874", "title": "I NEED A HORSE", "score": 247, } ::2 7b18b847292338bc29 { "format": "markdown", "body": “BEST video ever!” }
  27. 27. Versioning our Documents: • Couchbase has no built in mechanism for Versioning. • There are many ways to approach document Versioning. - Copy the versions of the document into new documents, - Copy the versions of the document into a list of nested documents, - Store the list of mutated / modified attributes: • In nested Element, • In separate Documents. • In this case, we’re going to look at the simplest way…
  28. 28. Versioning our Documents: Current Version: mykey Version 1: mykey::v1 Version 2: mykey::v2 • Get the current version of the document, • Increment the version number, • Create the version with the new key "mykey::v1”, • Save the document in it’s current version.
  29. 29. Questions?