Upcoming SlideShare
×

# Getting Started with Couchbase App Development:

1,422 views
1,358 views

Published on

4 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,422
On SlideShare
0
From Embeds
0
Number of Embeds
459
Actions
Shares
0
50
0
Likes
4
Embeds 0
No embeds

No notes for slide
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• Most of you are probably familiar with the table layout. A table is defined with a set of column. And each record in the table conforms to the schema. If you wish to capture different data in the future, the table schema must be changed using the alter table statement. \n\nTypically data is normalized in the 3rd normal form reduce duplication. Large tables are split into smaller tables.using foreign keys \n
• \n
• \n
• \n
• \n
• \n
• \n
• Example. \n\nNormalized schema \n2 tables \nFk connects the two. \nTo get information about a specific error, you will perform and join across the two tables \n
• Example. \n\nNormalized schema \n2 tables \nFk connects the two. \nTo get information about a specific error, you will perform and join across the two tables \n
• Example. \n\nNormalized schema \n2 tables \nFk connects the two. \nTo get information about a specific error, you will perform and join across the two tables \n
• Example. \n\nNormalized schema \n2 tables \nFk connects the two. \nTo get information about a specific error, you will perform and join across the two tables \n
• Example. \n\nNormalized schema \n2 tables \nFk connects the two. \nTo get information about a specific error, you will perform and join across the two tables \n
• Example. \n\nNormalized schema \n2 tables \nFk connects the two. \nTo get information about a specific error, you will perform and join across the two tables \n
• Example. \n\nNormalized schema \n2 tables \nFk connects the two. \nTo get information about a specific error, you will perform and join across the two tables \n
• Example. \n\nNormalized schema \n2 tables \nFk connects the two. \nTo get information about a specific error, you will perform and join across the two tables \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• this could be the ruby form POST example\n
• this could be the ruby form POST example\n
• this could be the ruby form POST example\n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• ### Getting Started with Couchbase App Development:

1. 1. Developing with Couchbase I Jasdeep Jaitla Technical Evangelist email: twitter: @scalabl3jasdeep@couchbase.com
2. 2. Developing with Couchbase I Jasdeep Jaitla Technical Evangelist email: twitter: @scalabl3jasdeep@couchbase.com
3. 3. Developing with Couchbase Series www.couchbase.com/webinars
4. 4. Presentation Summary• Setting Up Your Development Environment• Quick Server Architecture Overview• Intro to JSON Documents• Thinking in NoSQL• Intro to Couchbase Operations• Modeling & Agile Development
5. 5. SETTING UP TO DEVELOP
6. 6. Install Couchbase Server ✴ 2.0 www.couchbase.com/couchbase-server/beta OR, for the Latest 2.0 Build ✴ www.couchbase.com/download ✴ select 2.0.0 recent builds in dropdownUbuntuRedHatMac OS XWindows
7. 7. Install Couchbase Server ✴ 2.0 www.couchbase.com/couchbase-server/beta OR, for the Latest 2.0 Build ✴ www.couchbase.com/download ✴ select 2.0.0 recent builds in dropdownUbuntuRedHat select 2.0.0 recent buildsMac OS XWindows
8. 8. Install Couchbase Server ✴ 2.0 www.couchbase.com/couchbase-server/beta OR, for the Latest 2.0 Build ✴ www.couchbase.com/download ✴ select 2.0.0 recent builds in dropdownUbuntuRedHatMac OS XWindows
9. 9. Install Couchbase Server ✴ 2.0 www.couchbase.com/couchbase-server/beta OR, for the Latest 2.0 Build ✴ www.couchbase.com/download ✴ select 2.0.0 recent builds in dropdownUbuntuRedHatMac OS XWindows
10. 10. Official SDKswww.couchbase.com/develop Community SDKs
11. 11. Official SDKs Ruby Pythonwww.couchbase.com/develop Community SDKs
12. 12. Official SDKs Ruby Pythonwww.couchbase.com/develop Community SDKs Go Clojure
13. 13. Official SDKsRuby libcouchbase Clojure Python www.couchbase.com/develop Go
14. 14. Smart Client SDKsCouchbase App Server
15. 15. Smart Client SDKs o n necti co n mak eCouchbase App Server
16. 16. Smart Client SDKs n ctio o nne ma ke c logy topo r ec eiveCouchbase App Server
17. 17. Smart Client SDKs po logy o c ei ve t reCouchbase App Server
18. 18. Smart Client SDKsCouchbase App Server
19. 19. Smart Client SDKsCouchbase App Server
20. 20. Smart Client SDKsCouchbase App Server
21. 21. Smart Client SDKsCouchbase App Server
22. 22. Smart Client SDKsCouchbase App Server
23. 23. Smart Client SDKsCouchbase App Server
24. 24. Smart Client SDKsCouchbase App Server
25. 25. Smart Client SDKsCouchbase App Server
26. 26. Smart Client SDKsCouchbase App Server
27. 27. Smart Client SDKsCouchbase App Server
28. 28. Smart Client SDKsCouchbase App Server
29. 29. Smart Client SDKsCouchbase App Server
30. 30. Smart Client SDKsCouchbase App Server
31. 31. Smart Client SDKsCouchbase App Server
32. 32. Smart Client SDKsCouchbase App Server
33. 33. Smart Client SDKsCouchbase App Server
34. 34. Smart Client SDKsCouchbase App Server
35. 35. Smart Client SDKsCouchbase App Server
36. 36. Smart Client SDKsCouchbase App Server
37. 37. Smart Client SDKs CouchbaseCouchbase Topology App Server Update
38. 38. Smart Client SDKs CouchbaseCouchbase Topology App Server Update
39. 39. Smart Client SDKs CouchbaseCouchbase Topology App Server Update
40. 40. Smart Client SDKsCouchbase App Server
41. 41. Smart Client SDKsCouchbase App Server
42. 42. Smart Client SDKsCouchbase App Server
43. 43. Smart Client SDKsCouchbase App Server
44. 44. Smart Client SDKsCouchbase App Server
45. 45. Smart Client SDKsCouchbase App Server
46. 46. JSON DOCUMENTS
47. 47. Aggregate View of Data http://martinfowler.com/bliki/
48. 48. What is an aggregate?When you need to retrieve data from RDBMS, youare "aggregating" or "denormalizing" the data foryour application through queries with joins, whereclauses and order by clauses.In Document Databases, instead of breaking datainto tables and foreign keys, you store theaggregate data together in JSON document(s).
49. 49. Store and Retrieve Aggregates order::1001 { uid: ji22jd, customer: Ann, line_items: [ { sku: 0321293533, quan: 3, unit_price: 48.0 }, { sku: 0321601912, quan: 1, unit_price: 39.0 }, { sku: 0131495054, quan: 1, unit_price: 51.0 } ], payment: { type: Amex, expiry: 04/2001, last5: 12345 } }• Easier to Distribute Data• More Flexibility
50. 50. Relational vs Document Model C1 C2 C3 C4 { JSON JSON } JSONRelational data model Document data model Highly-structured table Collection of complex documents with organization with rigidly- arbitrary, nested data formats and deﬁned data formats and varying “record” format. record structure. 14
51. 51. SQL Normalized Tables Users Addresses KEY First Last ZIP_ID ZIP_ID CITY STATE ZIP 1 Jasdeep Jaitla 2 1 DEN CO 30303 2 Joe Smith 2 2 MV CA 94040 3 Ali Dodson 2 3 CHI IL 60609 4 John Doe 3 4 NY NY 10010To get information about speciﬁc user, you perform a join across two tables 15
52. 52. SQL Normalized Tables Users Addresses KEY First Last ZIP_ID ZIP_ID CITY STATE ZIP 1 Jasdeep Jaitla 2 1 DEN CO 30303 2 Joe Smith 2 2 MV CA 94040 3 Ali Dodson 2 3 CHI IL 60609 4 John Doe 3 4 NY NY 10010To get information about speciﬁc user, you perform a join across two tables 15
53. 53. SQL Normalized Tables foreign key Users Addresses KEY First Last ZIP_ID ZIP_ID CITY STATE ZIP 1 Jasdeep Jaitla 2 1 DEN CO 30303 2 Joe Smith 2 2 MV CA 94040 3 Ali Dodson 2 3 CHI IL 60609 4 John Doe 3 4 NY NY 10010To get information about speciﬁc user, you perform a join across two tables 15
54. 54. SQL Normalized Tables foreign key Users Addresses KEY First Last ZIP_ID ZIP_ID CITY STATE ZIP 1 Jasdeep Jaitla 2 1 DEN CO 30303 2 Joe Smith 2 2 MV CA 94040 3 Ali Dodson 2 3 CHI IL 60609 4 John Doe 3 4 NY NY 10010 To get information about speciﬁc user, you perform a join across two tablesSELECT * FROM Users u INNER JOIN Addresses a ON u.zip_id = a.zip_id WHERE key=1 15
55. 55. Documents are Aggregates Users AddressesKEY First Last ZIP_ID ZIP_ID CITY STATE ZIP1 Jasdeep Jaitla 2 1 DEN CO 303032 Joe Smith 2 2 SF CA 941033 Ali Dodson 2 3 CHI IL 606094 John Doe 3 4 NY NY 10010 All data in a single document 16
56. 56. Documents are Aggregates Users AddressesKEY First Last ZIP_ID ZIP_ID CITY STATE ZIP1 Jasdeep Jaitla 2 1 DEN CO 303032 Joe Smith 2 2 SF CA 941033 Ali Dodson 2 3 CHI IL 606094 John Doe 3 4 NY NY 10010 All data in a single document 16
57. 57. Documents are Aggregates Users AddressesKEY First Last ZIP_ID ZIP_ID CITY STATE ZIP1 Jasdeep Jaitla 2 1 DEN CO 3030323 Joe Ali Smith Dodson 2 2 + 2 3 SF CHI CA IL 94103 606094 John Doe 3 4 NY NY 10010 All data in a single document 16
58. 58. Documents are Aggregates Users AddressesKEY First Last ZIP_ID ZIP_ID CITY STATE ZIP1 Jasdeep Jaitla 2 1 DEN CO 3030323 Joe Ali Smith Dodson 2 2 + 2 3 SF SF CHI CA CA IL 94103 94103 606094 John Doe 3 4 NY NY 10010 All data in a single document 16
59. 59. Documents are Aggregates Users AddressesKEY First Last ZIP_ID ZIP_ID CITY STATE ZIP1 Jasdeep Jaitla 2 1 DEN CO 3030323 Joe Ali Smith Dodson 2 2 + 2 3 SF SF CHI CA CA IL 94103 94103 606094 John Doe 3 4 NY NY 10010 All data in a single document 16
60. 60. Documents are Aggregates Users AddressesKEY First Last ZIP_ID ZIP_ID CITY STATE ZIP1 Jasdeep Jaitla 2 1 DEN CO 3030323 Joe Ali Smith Dodson 2 2 + 2 3 SF SF CHI CA CA IL 94103 94103 60609 =4 John Doe 3 4 NY NY 10010 All data in a single document 16
61. 61. Documents are Aggregates Users AddressesKEY First Last ZIP_ID ZIP_ID CITY STATE ZIP1 Jasdeep Jaitla 2 1 DEN CO 30303  { “ID”: 1, + = “First”:2 Joe Smith 2 2 SF SF CA CA 94103 94103 “Jasdeep”, “Last”: “Jaitla”, “ZIP”: “94103”,3 Ali Dodson 2 3 CHI IL 60609 “CITY”: “SF”, “STATE”: “CA” JSON }4 John Doe 3 4 NY NY 10010 All data in a single document 16
62. 62. Documents are Aggregates Users AddressesKEY First Last ZIP_ID ZIP_ID CITY STATE ZIP1 Jasdeep Jaitla 2 1 DEN CO 30303  { “ID”: 1, + = “First”:2 Joe Smith 2 2 SF SF CA CA 94103 94103 “Jasdeep”, “Last”: “Jaitla”, “ZIP”: “94103”,3 Ali Dodson 2 3 CHI IL 60609 “CITY”: “SF”, “STATE”: “CA” JSON }4 John Doe 3 4 NY NY 10010 Document Data is an Aggregate All data in a single document 16
63. 63. Documents are Aggregates Users AddressesKEY First Last ZIP_ID ZIP_ID CITY STATE ZIP1 Jasdeep Jaitla 2 1 DEN CO 30303  { “ID”: 1, + = “First”:2 Joe Smith 2 2 SF SF CA CA 94103 94103 “Jasdeep”, “Last”: “Jaitla”, “ZIP”: “94103”,3 Ali Dodson 2 3 CHI IL 60609 “CITY”: “SF”, “STATE”: “CA” JSON }4 John Doe 3 4 NY NY 10010 Document Data is an Aggregate All data in a single document couchbase.get(“user::1”) 16
64. 64. JSON Document Structure meta {Meta Information “id”: “u::jasdeep@couchbase.com”, Including Key “rev”: “1-0002bce0000000000”, “ﬂags”: 0, “expiration”: 0,All Keys Unique “type”: “json”and Kept in RAM } document { “uid”: 123456,Document Value “ﬁrstname”: “jasdeep”, “lastname”: “Jaitla”, “age”: 22, Most Recent In “favorite_colors”: [“blue”, “black”],Ram And Persisted “email”: “jasdeep@couchbase.com” To Disk }
65. 65. Document Keys Keys have to be unique in a Bucket (database) which acts as a namespace.Keys tend to have two categories:human-readable and random generatedHuman readable keys tend toward uniquedescriptors:email addresses, social media id’s, phonenumbers, blog post titles, usernames, sku’s, etc.
66. 66. Objects Serialized to JSON andUser Object Back u::jasdeep@couchbase.com { set(string uid “uid”: 123456,string ﬁrstname “ﬁrstname”: “jasdeep”,string lastname “lastname”: “Jaitla”, “age”: 22,int age “favorite_colors”: [“blue”,array favorite_color “black”], s “email”:string emailUser u::jasdeep@couchbase.com get(string uid { “uid”: 123456,string ﬁrstname “ﬁrstname”: “jasdeep”,string lastname “lastname”: “Jaitla”, “age”: 22,int age “favorite_colors”: [“blue”,array favorite_color “black”], s “email”:string email
67. 67. JSON only? Nope! Couchbase can store straight strings,binary blobs, JSON Documents and a special structure called an Atomic Counter (a positive integer value with its own special operations). Maximum size of a Document is 20MB.
68. 68. Indexing & QueryingCouchbase 2.0’s Views (Distributed Map/Reduce) and Elastic Search Integration enable different types of Indexing, Querying and Searching for Documents We cover the fundamentals of this in the upcomingwebinars, but you can also refer to our previous webinars on the subjects: Indexing/Querying Incremental Map Reduce for Real-Time Analytics Full Text Search Integration (Elastic Search)
69. 69. COUCHBASEOPERATIONS
70. 70. •get (key) – Retrieve a document•set (key, value) – Store a document, overwrites if exists•add (key, value) – Store a document, error/exception if exists•replace (key, value) – Store a document, error/exception if doesn’t exist•cas (key, value, cas) – Compare and swap, mutate document only if it hasn’t changed while executing this operation
71. 71. Atomic Counter Operations These operations are always executed in order atomically.• set (key, value) – Use set to initialize the counter •cb.set(“my_counter”, 1)•incr (key) – Increase an atomic counter value, default by 1 •cb.incr(“my_counter”) # now it’s 2•decr (key) – Decrease an atomic counter value, default by 1 •cb.decr(“my_counter”) # now it’s 1
72. 72. Mental Adjustments• In SQL we tend to want to avoid hitting the database as much as possible – Even with caching and indexing tricks, and massive improvements over the years, SQL still gets bogged down by complex joins and huge indexes• In Couchbase, get’s and set’s are so fast they are trivial, and not bottlenecks, this is hard for many people to accept at ﬁrst; Multiple get statements are commonplace, don’t avoid it!
73. 73. EXAMPLE
74. 74. Ruby Example# example.rb # user.rbrequire “./user.rb” require “rubygems” require “couchbase”u1 = User.new({ :email => “jasdeep@scalabl3.com”, class User :name => “Jasdeep Jaitla”, attr_accessor :name, :email, :title, :title => “Scalability Sherpa”, :twitter :twitter => “@scalabl3 }) def initialize(attr = {}) attr.each do |name, value|u1.save setter = "#{name}=" next unless respond_to?(setter) send(setter, value) end end def save c = Couchbase.bucket c. set(@email.downcase, self.to_json) end end
75. 75. Add Lookup Class Method# example.rb # user.rbrequire “./user.rb” require “rubygems” require “couchbase”u1 = User.new({ :email => “jasdeep@scalabl3.com”, class User :name => “Jasdeep Jaitla”, :title => “Scalability Sherpa”, attr_accessor :name, :email, :title, :twitter => “@scalabl3 :twitter }) def initialize(attr = {}) ... endu1.save def saveu1 = User.ﬁnd_by_email(“jasdeep@scalabl3.com”) c = Couchbase.bucketif u1 c.set(@email.downcase, self.to_json) puts “User Found!” end puts u1.inspectelse def self.ﬁnd_by_email(email) puts “User Not Registered!”end c = Couchbase.bucket doc = c.get(email.downcase) return doc ? User.new(doc) : nil end end
76. 76. Agile Model Development# example.rb # user.rbrequire “./user.rb” require “rubygems” require “couchbase”u1 = User.ﬁnd_by_email(“jasdeep@scalabl3.com”) class Userif u1 u1.fb_id = “682829292” attr_accessor :name, :email, :title, u1.fb_token = “f02jdjd020d8373730djd02” :twitter u1.save attr_accessor :fb_id, :fb_tokenelse # create user def initialize(attr = {}) ... endend def save ... end def self.ﬁnd_by_email(email) c = Couchbase.bucket doc = c.get(email.downcase) return doc ? User.new(doc) : nil end end
77. 77. MULTIPLEDOCUMENTS
78. 78. • User proﬁle BLOG •• Blog Post Object – Contains the Content (ID, Title, Body Text, Author)• Blog Comment – Comments from other users • Option 1 - Keep it with Blog Document • Option 2 - Separate it Out
79. 79. Single Document {Blog Post Object “id”: “scalabl3_Hello_World”,string id “author”: “scalabl3”,string author “type”: “post”string type “title”: “Hello World”, “format”: “markdown”,string title “body”: “Hello from [Couchbase](http://string format couchbase.com).”,string body “html”: “<p>Hello from <a href=“http: …,string html “comments”: [array comments {“format”: “markdown”, “body”:”Awesome post!”}, {“format”: “markdown”, “body”:”Like it.” } ] }
80. 80. Multiple Documents {Blog Post Object “id”: “scalabl3_Hello_World”,string id “author”: “scalabl3”,string author “type”: “post” “title”: “Hello World”,string type “format”: “markdown”,string title “body”: “Hello from [Couchbase](http://string format couchbase.com).”, “html”: “<p>Hello from <a href=“http: …,string body “num_comments”: 2string html }int comments { “id”: “scalabl3_Hello_World::1”,Comment Object “format”: “markdown”, “body”: “Awesome post!”string format }string body {int id “id”: “scalabl3_Hello_World::2”, “format”: “markdown”, “body”: “Love It!” }
81. 81. Multiple Documents {Blog Post Object “id”: “scalabl3_Hello_World”,string id “author”: “scalabl3”,string author “type”: “post” “title”: “Hello World”,string type “format”: “markdown”,string title “body”: “Hello from [Couchbase](http://string format couchbase.com).”, “html”: “<p>Hello from <a href=“http: …,string body }string htmlint comments { “id”: “scalabl3_Hello_World::comments”, 2 } { “id”: “scalabl3_Hello_World::1”,Comment Object “format”: “markdown”, “body”: “Awesome post!”string format }string body {int id “id”: “scalabl3_Hello_World::2”, “format”: “markdown”, “body”: “Love It!” }
82. 82. Model Structures USER Couchbase instance variables JSON Couchbase JSON USERinstance variables JSON Couchbase Array Data Couchbase Hash Data Set/List Data JSON JSON
83. 83. eCommerce Model USER JSON instance variables shopping_cartsORDER ORDER JSON Couchbase ordersinstance variables instance variables statistics JSON JSON JSON JSON JSON ShoppingCart JSON JSON JSON instance variables JSON CartItems JSON JSON JSON PRODUCT PRODUCT CartItems JSON CartItems JSON instance variables JSON instance variables instance variables instance variables
84. 84. A Few Tips for Single vs Multiple• If an object is pretty simple, generally a Single Document is best• Use Multiple Documents when it’s advantageous to do so - Simpler Code - Simpler Model - Reduce Document Bloating• Generally if you have large or limitless lists, it’s best to use a Multiple Document Scheme• Atomic Counters are great for keeping track of numerical information that changes a lot, like points, counts, stats, etc.• Atomic Counters can be used to create Key-Value Patterns
85. 85. Summary• Couchbase • Client SDKs for Every Major -Is a Document Database Language -Has Easy Scalability • Modeling -Consistent High -Can Be Simple 1:1 Mappings Performance -Can Involve Multiple -Always On 24/7/365 Documents 1:n• Couchbase Operations -Can Have Relationships without Joins -Operations are lightning fast -You don’t have to avoid multiple gets, they’re fast
86. 86. I’m Excited to See What You Build, Thanks For Watching! Q&ACouchbase Docs Contact me on Twitterwww.couchbase.com/docs/index-full.html @scalabl3Couchbase Forums Contact me by Emailwww.couchbase.com/forums jasdeep@couchbase.comIRC Learn More About Design#couchbase Patterns#libcouchbase CouchbaseModels.com Setting up for Ruby on Rails CouchbaseOnRails.com Coming Soon... CouchbaseOnNode.com