Your SlideShare is downloading. ×
0
Technical	
  Evangelist	
  
twi0er:	
  @tgrall	
  email:	
  tug@couchbase.com	
  
Tugdual	
  “Tug” Grall	
  
	
  
Ge;ng	
 ...
Technical	
  Evangelist	
  
twi0er:	
  @scalabl3	
  email:	
  jasdeep@couchbase.com	
  
Jasdeep	
  Jaitla	
  
	
  
Ge;ng	
...
RDBMS	
  vs	
  Couchbase	
  
RDBMS	
  vs	
  Couchbase	
  
• It's	
  Different	
  but	
  Familiar!	
  
• Think	
  in	
  New	
  Ways!	
  
• Simpler	
  in	...
RDBMS	
  OrganizaYon	
  
• RDBMS	
  organizes	
  data	
  as	
  tables	
  
-­‐ Tables	
  represent	
  data	
  in	
  rows,	
...
Couchbase	
  OrganizaYon	
  
• Couchbase	
  operates	
  like	
  a	
  Key-­‐Value	
  Document	
  Store	
  	
  
-­‐ Simple	
...
Complex	
  Datatypes	
  
• Can	
  represent	
  both	
  simple	
  and	
  
complex	
  data	
  types	
  in	
  JSON	
  data	
 ...
Architecture	
  
Install	
  Couchbase	
  
• Install	
  Couchbase	
  by	
  following	
  InstrucYons	
  
• Ajer	
  installaYon,	
  open	
  yo...
Disk	
  Queue	
  
Disk	
  
ReplicaYon	
  
Queue	
  
Couchbase	
  Server	
  Node	
  
To	
  other	
  node	
  
Managed	
  Cac...
Disk	
  Queue	
  
Disk	
  
ReplicaYon	
  
Queue	
  
Couchbase	
  Server	
  Node	
  
To	
  other	
  node	
  
Managed	
  Cac...
Disk	
  Queue	
  
Disk	
  
ReplicaYon	
  
Queue	
  
Couchbase	
  Server	
  Node	
  
To	
  other	
  node	
  
Managed	
  Cac...
SDK's	
  
Setup	
  SDK	
  
• Each	
  supported	
  SDK	
  page	
  has	
  instrucYons	
  for	
  setup	
  
• PHP,	
  Ruby,	
  NodeJS	
 ...
Make	
  a	
  ConnecYon	
  
require 'rubygems'!
require 'couchbase'!
 !
cb = Couchbase.connect(!
:bucket => "default",!
:ho...
Make	
  a	
  ConnecYon	
  
import com.couchbase.client.CouchbaseClient;!
import java.net.URI;!
import java.util.*;!
import...
Make	
  a	
  ConnecYon	
  
var Couchbase = require('couchbase');!
var cb = new Couchbase.Connection({bucket: "default"}, f...
Fundamentals	
  
• Couchbase	
  is	
  structured	
  as	
  a	
  Key-­‐Value	
  store,	
  therefore	
  All	
  Data	
  has	
 ...
• get	
  (key)	
  
– 	
  Retrieve	
  a	
  document	
  
• set	
  (key,	
  value)	
  
– 	
  Store	
  a	
  document,	
  overw...
Atomic	
  Counter	
  OperaYons	
  
These	
  operaYons	
  are	
  always	
  executed	
  in	
  order	
  atomically.	
  
	
  
...
Objects	
  Serialized	
  to	
  JSON	
  and	
  Back	
  	
  
User	
  Object	
  
string	
   uid	
  
string	
   firstname	
  
s...
Basic	
  Key	
  Pa0erns	
  
Basic	
  Keying	
  
• Use	
  a	
  Unique	
  value	
  for	
  key	
  (email,	
  username,	
  sku,	
  isbn,	
  etc.)	
  
-­‐ ...
Counter-­‐ID	
  
ApplicaYon	
  
incr("counter-­‐key")	
  
add("key"	
  +	
  counter_val,	
  data)	
  
ApplicaYon	
  
count...
Counter-­‐ID	
  
• Similar	
  to	
  IDENTITY	
  column	
  in	
  RDBMS	
  
• CreaYng	
  New	
  Document	
  is	
  a	
  pair	...
Lookup	
  Pa0ern	
  
ApplicaYon	
  
add("john@demo.com",	
  	
  
"u::550e8400-­‐e29b-­‐41d4-­‐a716")	
  
add("u::550e8400-...
Lookup	
  Pa0ern	
  
• Create	
  simple	
  document	
  that	
  has	
  referenYal	
  data	
  (Key)	
  to	
  primary	
  
doc...
Combine	
  Counter-­‐ID	
  and	
  Lookup	
  
ApplicaYon	
  
add("john@demo.com",	
  id)	
  
add("u::"	
  +	
  id,	
  data)...
Combine	
  Counter-­‐ID	
  and	
  Lookup	
  
Pro's	
  
• Binary	
  OperaYons,	
  overall	
  faster	
  than	
  large	
  vol...
Views	
  
Map()	
  FuncYon	
  =>	
  Index	
  
function(doc,	
  meta)	
  {	
  
emit(doc.username,	
  doc.email)	
  
}	
  
Every Docum...
Single	
  Element	
  Keys	
  (Text	
  Key)	
  
function(doc,	
  meta)	
  {	
  
emit(doc.email,	
  doc.points)	
  
}	
   te...
Compound	
  Keys	
  (Array)	
  
function(doc,	
  meta)	
  {	
  
emit(dateToArray(doc.timestamp),	
  1)	
  
}	
   array key...
Most	
  Common	
  Query’s	
  Are	
  Ranges	
  
doc.email	
   meta.id	
  
abba@couchbase.com	
   u::1	
  
beta@couchbase.co...
Index-­‐Key	
  Matching	
  
doc.email	
   meta.id	
  
abba@couchbase.com	
   u::1	
  
beta@couchbase.com	
   u::7	
  
jasd...
Index-­‐Key	
  Set	
  Matches	
  
doc.email	
   meta.id	
  
abba@couchbase.com	
   u::1	
  
beta@couchbase.com	
   u::7	
 ...
Key-­‐Value	
  Pa0erns	
  vs	
  Views	
  
• Key	
  Pa0erns	
  only	
  use	
  Binary	
  OperaYons	
  
• Documents	
  are	
 ...
Key-­‐Value	
  Pa0erns	
  vs	
  Views	
  
• As	
  Documents	
  are	
  created/mutated,	
  they	
  are	
  persisted	
  	
  ...
Quick	
  Demo	
  
Q&A	
  
Webinar - Getting Started with Couchbase App Development
Webinar - Getting Started with Couchbase App Development
Upcoming SlideShare
Loading in...5
×

Webinar - Getting Started with Couchbase App Development

2,097

Published on

Learn the primary differences between RDBMS and NoSQL from a developer perspective, how to setup the development environment and how to model your documents.

In this webinar you will see:

JSON document and document database basics
How to setup your development environment
How Couchbase Operations work
How to model your documents, including examples

Published in: Technology
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total Views
2,097
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
32
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Webinar - Getting Started with Couchbase App Development"

  1. 1. Technical  Evangelist   twi0er:  @tgrall  email:  tug@couchbase.com   Tugdual  “Tug” Grall     Ge;ng  Started  with   Couchbase  (Europe)  
  2. 2. Technical  Evangelist   twi0er:  @scalabl3  email:  jasdeep@couchbase.com   Jasdeep  Jaitla     Ge;ng  Started  with   Couchbase  (US)  
  3. 3. RDBMS  vs  Couchbase  
  4. 4. RDBMS  vs  Couchbase   • It's  Different  but  Familiar!   • Think  in  New  Ways!   • Simpler  in  many  cases!   • New  Pa0erns!   • Faster/Lighter/Agile!   • "Unlearn  what  you  have  Learned!"   Remember:  Data  is  Data;  databases  are  just  opYmizing  how  to  store  and  retrieve   it.    Objects  are  Classes  with  Data  and  Methods.  
  5. 5. RDBMS  OrganizaYon   • RDBMS  organizes  data  as  tables   -­‐ Tables  represent  data  in  rows,  n  columns  of  m  rows   -­‐ Table  rows  have  a  specific  schema,  each  column  as  a  staYc  type   -­‐ Simple  Datatypes:  strings,  numbers,  dateYmes,  booleans,  can  be   represented  by  columns  in  a  single  table   -­‐ Complex  Datatypes:  dicYonaries/hashes,  arrays/lists  can  not  be   represented  in  a  single  table  [Impedence  Mismatch]   • All  rows  have  idenYcal  schema,  schema  changes  require  taking   database  offline   • Reading/WriYng/TransacYons  require  mutex  and  locking  
  6. 6. Couchbase  OrganizaYon   • Couchbase  operates  like  a  Key-­‐Value  Document  Store     -­‐ Simple  Datatypes:  strings,  numbers,  dateYme,  boolean,  and  binary   data  can  be  stored;  they  are  stored  as  Base64  encoded  strings   -­‐ Complex  Datatypes:  dicYonaries/hashes,  arrays/lists,  can  be  stored  in   JSON  format  (simple  lists  can  be  string  based  with  delimiter)   -­‐ JSON  is  a  special  class  of  string  with  a  specific  format  for  encoding   simple  and  complex  data  structures   • Schema  is  unenforced  and  implicit,  schema  changes  are   programmaYc,  done  online,  and  can  vary  from  Document  to   Document  
  7. 7. Complex  Datatypes   • Can  represent  both  simple  and   complex  data  types  in  JSON  data   structures   • Can  modify  schema  on  the  fly,  and   Documents  of  a  specific  "type"  can   vary  in  schema   • "Type"  is  arbitrary,  it's  a   programming  strategy,  there  are  no   actual  "types",  but  it's  typical  to   embed  the  class  name  as  a   "doctype"  json  key   Couchbase   {! "doctype": "User","name": "John Doe",! "email": "john@demo.com",! "age": 38,! "gender_male": true,! "created_at": "2013-09-20 23:59:59",! "items_viewed": [! "12345", "23456", 34567"! ],! "preferences": {! "email_notifications": true,! "sms_notifications": false! },! "authored": [! { "title": "Couchbase Models",! "price": 49.95 }]}!
  8. 8. Architecture  
  9. 9. Install  Couchbase   • Install  Couchbase  by  following  InstrucYons   • Ajer  installaYon,  open  your  browser  to  h0p://localhost:8091/   • Finish  setup  step  prompts,  now  you  have  a  running  Couchbase  server   with  a  default  bucket   www.couchbase.com/download  
  10. 10. Disk  Queue   Disk   ReplicaYon   Queue   Couchbase  Server  Node   To  other  node   Managed  Cache   App  Server   Doc  1  Doc  1   Doc  1   Write  OperaYon  
  11. 11. Disk  Queue   Disk   ReplicaYon   Queue   Couchbase  Server  Node   To  other  node   Managed  Cache   App  Server   Doc  1’   Doc  1   Doc  1’  Doc  1   Doc  1’   .   Update  OperaYon  
  12. 12. Disk  Queue   Disk   ReplicaYon   Queue   Couchbase  Server  Node   To  other  node   Managed  Cache   GET   Doc  1   2   App  Server   Doc  1   Doc  1  Doc  1   Read  OperaYon  
  13. 13. SDK's  
  14. 14. Setup  SDK   • Each  supported  SDK  page  has  instrucYons  for  setup   • PHP,  Ruby,  NodeJS  and  Python  clients  are  wrappers  around   libcouchbase  C  library,  so  libcouchbase  must  be  installed  first   • For  other  community  clients,  click  on  All  Clients  on  lej  nav,  scroll   down  the  page  and  you  can  see  clients  for  Go,  Erlang,  Clojure,  TCL   and  Perl.   www.couchbase.com/communiYes  
  15. 15. Make  a  ConnecYon   require 'rubygems'! require 'couchbase'!  ! cb = Couchbase.connect(! :bucket => "default",! :hostname => "localhost")! ! data = { jsonkey: "value", created_at: Time.now }! ! cb("mydata", data)! puts cb.get("mydata")! #!/usr/bin/env python! from couchbase import Couchbase! from pprint import pprint! from datetime import datetime! ! cb = Couchbase.connect(bucket='default')! ! data = { "jsonkey": "value", "created_at": datetime.now() }! ! cb.add('mydata', data)! result = cb.get('mydata')! pprint(result.value, indent=4)!   RUBY   PYTHON  
  16. 16. Make  a  ConnecYon   import com.couchbase.client.CouchbaseClient;! import java.net.URI;! import java.util.*;! import com.google.gson.Gson;import com.google.gson.GsonBuilder;! public class HelloWorld {!  ! public static void main(String[] args) throws Exception {!  ! List<URI> hosts = Arrays.asList(! new URI("http://127.0.0.1:8091/pools")! );! ! CouchbaseClient cb = new CouchbaseClient(hosts, "default", "");! Gson json = new Gson();! ! Hashtable data = new Hashtable();! data.put("jsonkey", "value");! data.put("created_at", new Date());! ! cb.add("mydata", json.toJson(data))! System.out.println(cb.get("mydata"));! ! cb.shutdown();! }! }!   JAVA  
  17. 17. Make  a  ConnecYon   var Couchbase = require('couchbase');! var cb = new Couchbase.Connection({bucket: "default"}, function(err) { });! var data = { jsonkey: "value", created_at: new Date().toString() };! cb.add("mydata", data);! ! console.log(cb.get("mydata"));! ! NODEJS   <?php! // adjust these parameters to match your installation! $cb = new Couchbase("127.0.0.1:8091", "", "", "default");! ! $data = array("jsonkey" => "value", ! "created_at" => date("Y-m-d H:i:s"));! ! $cb->add("mydata",json_encode($data));! var_dump($cb->get("mydata"));! ! ?>! ! PHP  
  18. 18. Fundamentals   • Couchbase  is  structured  as  a  Key-­‐Value  store,  therefore  All  Data  has  a   Key  and  a  Value   • Keys  can  be  any  string  up  to  250  characters  long   • Keys  are  unique,  within  a  database  (bucket),  there  can  only  be  one   instance  of  a  key   • Keys  are  completely  in  the  control  of  the  applicaYon  developer,  there   is  no  internal  mechanism  for  key  generaYon   • Values  can  be  JSON,  strings,  numbers,  binary  blobs,  or  a  special   posiYve  atomic  counter  (unsigned  integer)   • Values  can  be  up  to  20MB  in  size  
  19. 19. • get  (key)   –   Retrieve  a  document   • set  (key,  value)   –   Store  a  document,  overwrites  if  exists   • add  (key,  value)   –   Store  a  document,  error/excepYon  if  exists   • replace  (key,  value)   –   Store  a  document,  error/excepYon  if  doesn t  exist   • cas  (key,  value,  cas)   –   Compare  and  swap,  mutate  document  only  if  it  hasn t  changed   while  execuYng  this  operaYon   Store  &  Retrieve  OperaYons  
  20. 20. Atomic  Counter  OperaYons   These  operaYons  are  always  executed  in  order  atomically.     • 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  
  21. 21. Objects  Serialized  to  JSON  and  Back     User  Object   string   uid   string   firstname   string   lastname   int   age   array   favorite_colors   string   email   u::jasdeep@couchbase.com   {   “uid”:  123456,   “firstname”:  “john”,   “lastname”:  “doe”,   “age”:  22,   “favorite_colors”:  [“blue”,  “black”],   “email”:  “john@demo.com”   }   User  Object   string   uid   string   firstname   string   lastname   int   age   array   favorite_colors   string   email   u::jasdeep@couchbase.com   {   “uid”:  123456,   “firstname”:  “John”,   “lastname”:  “Doe”,   “age”:  22,   “favorite_colors”:  [“blue”,  “black”],   “email”:  “john@demo.com”   }   add()   get()  
  22. 22. Basic  Key  Pa0erns  
  23. 23. Basic  Keying   • Use  a  Unique  value  for  key  (email,  username,  sku,  isbn,  etc.)   -­‐ Users   • u::john@demo.com   • u::jdoetw   -­‐ Products   • p::978-­‐0321573513      [isbn]   • Predictable  Keys  can  follow  Key-­‐Value  pa0erns  (Users  typically  can  be   done  this  way  and  are  the  most  numerous  items)   • Unpredictable  Keys  (GUID,  UUID,  etc.)  require  Views  (Map-­‐Reduce   Indexes)  to  find  their  documents  
  24. 24. Counter-­‐ID   ApplicaYon   incr("counter-­‐key")   add("key"  +  counter_val,  data)   ApplicaYon   count  =  get("counter-­‐key")   mulY-­‐get(keys[])   Data  CreaYon   Iterate  Through  CollecYon  
  25. 25. Counter-­‐ID   • Similar  to  IDENTITY  column  in  RDBMS   • CreaYng  New  Document  is  a  pair  of  operaYons,  INCR  and  ADD   -­‐ IniYalize  one  Key  as  an  Atomic  Counter  (I  do  at  App  Start)   -­‐ Increment  Counter  and  save  new  value   ✴  id  =  client.incr("blog::couchbase_dev::comment_count")   -­‐ Use  the  id  as  component  of  key  for  new  document   ✴  client.add(""blog::couchbase_dev::c"::"  +  id,  self.to_json)  
  26. 26. Lookup  Pa0ern   ApplicaYon   add("john@demo.com",     "u::550e8400-­‐e29b-­‐41d4-­‐a716")   add("u::550e8400-­‐e29b-­‐41d4-­‐a716",  data)   add("jdoetw",     "u::550e8400-­‐e29b-­‐41d4-­‐a716")   ApplicaYon   key  =  get("john@demo.com")   get(key)   Data  CreaYon   Data  Retrieval  
  27. 27. Lookup  Pa0ern   • Create  simple  document  that  has  referenYal  data  (Key)  to  primary   document   -­‐ Primary  Document  u::a2bf2-­‐23317-­‐2302   -­‐ Lookup  Document:      u::john@demo.com  {  u::a2bf2-­‐23317-­‐2302  }   • Lookup  Documents  aren't  JSON,  they  should  just  be  the  Key  as  a   string  so  you  skip  JSON  parsing   • Requires  Two  GET  operaYons,  first  GET  Lookup,  then  GET  primary   Document   -­‐ key  =  client.get("u::john@demo.com")   -­‐ doc  =  client.get(key)  
  28. 28. Combine  Counter-­‐ID  and  Lookup   ApplicaYon   add("john@demo.com",  id)   add("u::"  +  id,  data)   add("jdoetw",  id)   ApplicaYon   key  =  get("john@demo.com")   get(key)   Data  CreaYon   Data  Retrieval   id  =  incr("user::count")  
  29. 29. Combine  Counter-­‐ID  and  Lookup   Pro's   • Binary  OperaYons,  overall  faster  than  large  volume  of  View  Queries   • EssenYally  creates  several  ways  to  find  a  single  document   • Is  always  consistent,  just  like  all  other  Binary  operaYons       Con's   • Increases  Number  of  Documents,  therefore  Metadata  usage  in  RAM   -­‐ But  this  is  generally  a  non-­‐issue  for  most  people  
  30. 30. Views  
  31. 31. Map()  FuncYon  =>  Index   function(doc,  meta)  {   emit(doc.username,  doc.email)   }   Every Document passes through View Map() functions Map
  32. 32. Single  Element  Keys  (Text  Key)   function(doc,  meta)  {   emit(doc.email,  doc.points)   }   text key Map doc.email   doc.points   abba@couchbase.com   1000   jasdeep@couchbase.com   1200   zorro@couchbase.com   900  
  33. 33. Compound  Keys  (Array)   function(doc,  meta)  {   emit(dateToArray(doc.timestamp),  1)   }   array key Array Based Index Keys get sorted as Strings, but can be grouped by array elements Map dateToArray(doc.Ymestamp)   value   [2012,10,9,18,45]   1   [2012,9,26,11,15]   1   [2012,8,13,2,12]   1  
  34. 34. Most  Common  Query’s  Are  Ranges   doc.email   meta.id   abba@couchbase.com   u::1   beta@couchbase.com   u::7   jasdeep@couchbase.com   u::2   math@couchbase.com   u::5   ma0@couchbase.com   u::6   yeY@couchbase.com   u::4   zorro@couchbase.com   u::3   ?startkey=”b1”  &  endkey=”zZ”   Pulls  the  Index-­‐Keys   between  UTF-­‐8  Range   specified  by  the  startkey   and  endkey.   ?startkey=”bz”  &  endkey=”zn”   Pulls  the  Index-­‐Keys   between  UTF-­‐8  Range   specified  by  the  startkey   and  endkey.  
  35. 35. Index-­‐Key  Matching   doc.email   meta.id   abba@couchbase.com   u::1   beta@couchbase.com   u::7   jasdeep@couchbase.com   u::2   math@couchbase.com   u::5   ma0@couchbase.com   u::6   yeY@couchbase.com   u::4   zorro@couchbase.com   u::3   ?key=”math@couchbase.com”     Match  a  Single  Index-­‐Key  
  36. 36. Index-­‐Key  Set  Matches   doc.email   meta.id   abba@couchbase.com   u::1   beta@couchbase.com   u::7   jasdeep@couchbase.com   u::2   math@couchbase.com   u::5   ma0@couchbase.com   u::6   yeY@couchbase.com   u::4   zorro@couchbase.com   u::3   ?keys=[“math@couchbase.com”,   “yeY@couchbase.com”]   Query  MulYple  in  the   Set  (Array  NotaYon)  
  37. 37. Key-­‐Value  Pa0erns  vs  Views   • Key  Pa0erns  only  use  Binary  OperaYons   • Documents  are  consistent,  CAS  operaYons  can  be  used  for  race   condiYons  on  parYcular  documents   • Do  not  have  a  Yme  gap,  therefore  can  be  faster   • Even  as  data  size  gets  larger,  doesn't  increase  latency   Key-­‐Value  Pa0erns  
  38. 38. Key-­‐Value  Pa0erns  vs  Views   • As  Documents  are  created/mutated,  they  are  persisted     • Views  (Indexes)  do  batch  indexing  for  persisted  Documents,   incrementally  from  last  batch  process   • The  process  of  persistence  +  indexing  ==  Yme  gap  between  CRUD   OperaYons  and  showing  in  View  query  ==>  Eventually  Consistent   Views  
  39. 39. Quick  Demo  
  40. 40. Q&A  
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×