Ge#ng	
  Started
	
  
Tugdual	
  Grall
	
  
Technical	
  Evangelist
	
  
Dev	
  Track	
  Agenda	
  
10:30	
  -­‐	
  11:10	
  am	
  

Ge#ng	
  Started	
  :	
  Installa?on	
  and	
  Core	
  opera?o...
Ge#ng	
  Started	
  
Cluster-­‐wide	
  Basic	
  Opera?on	
  
APP	
  SERVER	
  1	
  

APP	
  SERVER	
  2	
  

COUCHBASE	
  Client	
  Library	
  ...
Add	
  Nodes	
  to	
  Cluster	
  
APP	
  SERVER	
  1	
  

APP	
  SERVER	
  2	
  

COUCHBASE	
  Client	
  Library	
  
	
  
...
Fail	
  Over	
  Node	
  
APP	
  SERVER	
  1	
  

APP	
  SERVER	
  2	
  

COUCHBASE	
  Client	
  Library	
  
	
  
CLUSTER	
...
Couchbase	
  SDK	
  
Official	
  SDKs	
  
Ruby	
  

Python	
  

Community	
  SDKs	
  
Go	
  

Clojure	
  

www.couchbase.com...
Client	
  Architecture	
  Overview	
  
• Based on the information given, the Client tries to establish an initial
connecti...
Client	
  Setup:	
  Ge#ng	
  Cluster	
  Configura?on	
  

	
  	
  Couchbase	
  Client	
  
App	
  Server	
  

n	
  
ec-o
nn
...
Bootstrap	
  
1.  GET /pools 	

2.  Look for the "default" pools	

3.  GET /pools/default!
4.  Look for the "buckets" hash...
Client	
  Setup:	
  Ge#ng	
  Cluster	
  Configura?on	
  
Couchbase	
  Client	
  
New	
  Node	
  Added	
  to	
  Cluster	
  
...
SDK	
  &	
  libcouchbase	
  Dependency	
  
• Java, .Net, C, Go are native	

• The SDK does not have dependency on other la...
Hands	
  On	
  
Quick	
  Start	
  :	
  Couchbase	
  Server
	
  
Download from 	

http://www.couchbase.com/download	

	

Install via .rpm, ...
Quick	
  Start	
  :	
  Client	
  
1.  Go to the developer page http://www.couchbase.com/develop	

2.  Select your language...
Demonstra?on	
  

Installing	
  Couchbase	
  Server	
  &	
  Client
Basics:	
  Retrieve
	
  
•  get	
  (key)	
  
­  	
  Retrieve	
  a	
  document	
  

•  gets(key)	
  
­  Retrieve	
  a	
  do...
Basics:	
  Create,	
  Update,	
  Delete
	
  
•  set	
  (key,	
  value)	
  
­  	
  Store	
  a	
  document,	
  overwrites	
 ...
Fundamentals	
  
•  Couchbase is structured as a Key-Value store: every Document has a
Key and a Value.	

•  Key can be up...
Dealing	
  with	
  keys	
  
• You are responsible of the key	

• Keys can be build from:	

• UUID	

• Atomic Counter	

• D...
What	
  about	
  the	
  value	
  ?	
  
•  Couchbase stores the data the way you send them 	

•  For example:	

•  cb.set(“...
Q&A	
  
Next Session : Advanced Operations and Patterns
Ge#ng	
  Started	
  –	
  Part	
  2	
  
Basics:	
  Retrieve
	
  
•  get	
  (key)	
  
­  	
  Retrieve	
  a	
  document	
  

•  gets(key)	
  
­  Retrieve	
  a	
  do...
Basics:	
  Create,	
  Update,	
  Delete
	
  
•  set	
  (key,	
  value)	
  
­  	
  Store	
  a	
  document,	
  overwrites	
 ...
Other	
  Op?ons
	
  
•  Durability	
  Requirements	
  
­  PersistTo.ONE!
­  ReplicateTo.ONE!
­  Simply	
  add	
  this	
  t...
Atomic	
  Integers
	
  
Atomic	
  Counters	
  are	
  a	
  special	
  structure	
  in	
  Couchbase,	
  they	
  are	
  
exec...
Couchbase	
  PaFerns	
  
• Atomic Counter for keys 	

• Find your ID by numbers, loop on the values, …	

• Lookup	

• Look...
Use	
  a	
  Counter	
  
cb = new CouchbaseClient(uris, "default", "");!
Gson json = new Gson()!
!
// create a new User!
Us...
Use	
  a	
  Counter	
  
Lookup	
  
cb = new CouchbaseClient(uris, "default", "");!
Gson json = new Gson()!
!
// create a new User!
User user = new...
Agile	
  Model	
  Development	
  
Simple	
  Example	
  in	
  Ruby	
  
#	
  example.rb	
  
require	
  “./user.rb”	
  
	
  
u1	
  =	
  User.new({	
  	
  
	
  ...
Add	
  Lookup	
  Class	
  Method	
  
	
  

#	
  example.rb	
  
require	
  “./user.rb”	
  
	
  
u1	
  =	
  User.new({	
  	
...
Agile	
  Model	
  Development	
  
	
  

#	
  example.rb	
  
require	
  “./user.rb”	
  
	
  
u1	
  =	
  User.find_by_email(“...
Concurrency	
  &	
  more	
  
Compare	
  and	
  Swap
	
  

Op-mis-c	
  Concurrency	
  in	
  a	
  Distributed	
  System
	
  

#	
  actors.rb	
  
	
  
c	
...
Check	
  Status	
  before	
  Saving
	
  
OperationStatus ops = null;!
int numberOfAttemptsLeft = 5;!
!
do {!
CASValue list...
Replica	
  Read
	
  
•  Read	
  the	
  replica	
  when	
  you	
  cannot	
  reach	
  the	
  node	
  
responsible	
  of	
  t...
Conclusion	
  
• Couchbase Server is installed	

• Couchbase Client SDK is installed	

• Core operations to save and get t...
Q&A	
  
Next Session : Advanced Operation and Sample Applications
Getting Started Developing with Couchbase SF 2013
Upcoming SlideShare
Loading in …5
×

Getting Started Developing with Couchbase SF 2013

2,187 views

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,187
On SlideShare
0
From Embeds
0
Number of Embeds
1,224
Actions
Shares
0
Downloads
49
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Bulletize the text. Make sure the builds work.
  • Bulletize the text. Make sure build work properly.
  • Bulletize the text. Make sure build work properly.
  • Couchbase client Basically, give people an overview of how the SDK ’ s get notified when there is a topology change via async REST. On the SDK side, we make sure the SDK ’ s as much as possible, get to the know the new cluster config – over streaming API.
  • Again, through Streaming API, our Client is made aware of the new Topology Under ideal conditions no pauses – usually realtime.
  • So all the SDKs are responsible of accessing data but also keep the cluster topology up to date.
  • Getting started with Couchbase is intentionally very easy. Couchbase is Open Source under an Apache 2.0 license, but there are binaries built for just about any platform. Most of the time, getting started with Couchbase is just a matter of downloading the package for your OS or the setup. Setting up the client library is equally simple.
  • Download product and install the sdk: - install in the session
  • Access the value in a fast and consistent way!
  • We can have more durability PersistTo and ReplicateTo. Persist to master (1), two, three. Using PersistTo master is the vbucket on which the key is stored. If we use PersistTo, we trade off some performance for more data Durability. ReplicateTo is the fastest, as it ’ s just sending the doc to the other node. We only use these when we really need it.
  • Access the value in a fast and consistent way!
  • We can have more durability PersistTo and ReplicateTo. Persist to master (1), two, three. Using PersistTo master is the vbucket on which the key is stored. If we use PersistTo, we trade off some performance for more data Durability. ReplicateTo is the fastest, as it ’ s just sending the doc to the other node. We only use these when we really need it.
  • You can use counter for example for versionning
  • You can use counter for example for versionning
  • You can use counter for example for versionning
  • We’re looking up by the email address which is the key. If user found, show details, if not, ‘user not registered’
  • We’re adding a field into our model here. With zero downtime. True agile development. Finding the user, adding the fields, saving.
  • If we have two different app servers trying to update the doc, the first one that gets there using the CAS value, will win. The second will fail. Every time any data is changed in a document, the CAS value changes. If we use the same CAS twice, it ’ s not going to work twice.
  • Getting Started Developing with Couchbase SF 2013

    1. 1. Ge#ng  Started   Tugdual  Grall   Technical  Evangelist  
    2. 2. Dev  Track  Agenda   10:30  -­‐  11:10  am   Ge#ng  Started  :  Installa?on  and  Core  opera?ons   11:20  -­‐  12:00pm   Ge#ng  Started  :  Advanced  Opera?ons  and  PaFerns   01:00  -­‐  01:40  pm   N1QL:  An  Early  Peek  at  Couchbase's  document  database  query   language   01:50  -­‐  02:30  pm   Document  Your  World   02:40  -­‐  03:20  pm   Indexing  and  Querying   03:40  -­‐  04:20  pm   Power  Techniques  With  Indexing   04:30  -­‐  05:10  pm   Exploring  Common  Models  and  Integra?ons  
    3. 3. Ge#ng  Started  
    4. 4. Cluster-­‐wide  Basic  Opera?on   APP  SERVER  1   APP  SERVER  2   COUCHBASE  Client  Library     CLUSTER  MAP     COUCHBASE  Client  Library     CLUSTER  MAP     READ/WRITE/UPDATE   SERVER  1       ACTIVE   SERVER  2       ACTIVE   SERVER  3       ACTIVE   Doc  5   Doc   Doc  4   Doc   Doc  1   Doc   Doc  2   Doc   Doc  7   Doc   Doc  2   Doc   Doc  9   Doc   Doc  8   Doc   Doc  6   Doc   REPLICA   REPLICA   REPLICA   •  Docs  distributed  evenly  across   servers     •  Each  server  stores  both  ac?ve  and   replica  docs   Only  one  server  ac-ve  at  a  -me   •  Client  library  provides  app  with   simple  interface  to  database   •  Cluster  map  provides  map     to  which  server  doc  is  on   Doc  4   Doc   Doc  6   Doc   Doc  7   Doc   Doc  1   Doc   Doc  3   Doc   Doc  9   Doc   •  App  reads,  writes,  updates  docs   Doc  8   Doc   Doc  2   Doc   Doc  5   Doc   •  Mul?ple  app  servers  can  access  same   document  at  same  ?me   COUCHBASE  SERVER    CLUSTER   User  Configured  Replica  Count  =  1   App  never  needs  to  know  
    5. 5. Add  Nodes  to  Cluster   APP  SERVER  1   APP  SERVER  2   COUCHBASE  Client  Library     CLUSTER  MAP     COUCHBASE  Client  Library     CLUSTER  MAP     READ/WRITE/UPDATE   SERVER  1       ACTIVE   SERVER  2       ACTIVE   READ/WRITE/UPDATE   SERVER  3       ACTIVE   Doc  5   Doc   Doc  4   Doc   Doc  1   Doc   Doc  7   Doc   Doc  2   Doc   Doc  8   Doc   Doc  6   Doc   REPLICA   REPLICA   REPLICA   Doc  4   Doc   Doc  6   Doc   Doc  7   Doc   Doc  1   Doc   Doc  3   Doc   Doc  9   Doc   Doc  8   Doc   Doc  2   Doc   Doc  5   Doc   COUCHBASE  SERVER    CLUSTER   User  Configured  Replica  Count  =  1   •  Two  servers  added   One-­‐click  opera?on   •  Docs  automa?cally   rebalanced  across   cluster   Doc   Doc  9   SERVER  5       ACTIVE   Doc   Doc  2   SERVER  4       ACTIVE   Even  distribu-on  of  docs   Minimum  doc  movement   REPLICA   REPLICA   •  Cluster  map  updated   •  App  database     calls  now  distributed     over  larger  number  of   servers    
    6. 6. Fail  Over  Node   APP  SERVER  1   APP  SERVER  2   COUCHBASE  Client  Library     CLUSTER  MAP     COUCHBASE  Client  Library     CLUSTER  MAP     SERVER  1       ACTIVE   SERVER  2       ACTIVE   SERVER  3       ACTIVE   SERVER  4       ACTIVE   Doc  5   Doc   Doc  4   Doc   Doc  1   Doc   Doc  9   Doc   Doc  2   Doc   Doc  7   Doc   Doc  2   Doc   Doc  8   SERVER  5       ACTIVE   Doc   Doc  1   Doc  6   Doc   Doc   Doc  3   REPLICA   REPLICA   REPLICA   REPLICA   Doc  4   Doc   Doc  6   Doc   Doc  7   Doc   Doc  5   Doc  1   Doc   Doc  3   Doc   Doc  9   Doc   Doc  2   COUCHBASE  SERVER    CLUSTER   User  Configured  Replica  Count  =  1   Doc   REPLICA   Doc  8   Doc   Doc   •  App  servers  accessing  docs   •  Requests  to  Server  3  fail   •  Cluster  detects  server  failed   Promotes  replicas  of  docs  to   ac-ve   Updates  cluster  map   •  Requests  for  docs  now  go  to   appropriate  server   •  Typically  rebalance     would  follow  
    7. 7. Couchbase  SDK   Official  SDKs   Ruby   Python   Community  SDKs   Go   Clojure   www.couchbase.com/develop  
    8. 8. Client  Architecture  Overview   • Based on the information given, the Client tries to establish an initial connection. • Once that’s done, it connects to a streaming API (HTTP chunked). • Cluster updates are fetched from that connection. • Failover/Add/Remove scenarios all update the clients in near real-time – no application restarts required! • Key/Value access is done directly against the nodes. • For View access one of the nodes is picked out which aggregates the results.
    9. 9. Client  Setup:  Ge#ng  Cluster  Configura?on      Couchbase  Client   App  Server   n   ec-o nn ke  co ma y   ol og  top eive rec
    10. 10. Bootstrap   1.  GET /pools 2.  Look for the "default" pools 3.  GET /pools/default! 4.  Look for the "buckets" hash which contains the bucket list 5.  GET /pools/default/buckets! 6.  Parse the list of buckets and extract the one provided by the application 7.  GET /pools/default/buckets/!
    11. 11. Client  Setup:  Ge#ng  Cluster  Configura?on   Couchbase  Client   New  Node  Added  to  Cluster   and  Coming  Online   Couchbase     Topology   Update  
    12. 12. SDK  &  libcouchbase  Dependency   • Java, .Net, C, Go are native • The SDK does not have dependency on other language • Ruby, PHP, Python, Node have dependency to libcouchbase • They are “wrapper” on the top of the C library • Scala, Clojure, JRuby are using Couchbase Java SDK
    13. 13. Hands  On  
    14. 14. Quick  Start  :  Couchbase  Server   Download from http://www.couchbase.com/download Install via .rpm, .deb, .exe, or .app EASY !
    15. 15. Quick  Start  :  Client   1.  Go to the developer page http://www.couchbase.com/develop 2.  Select your language 3.  Follow the “Getting Started” page •  Java •  C •  .Net •  Python •  Ruby •  Node •  PHP •  …
    16. 16. Demonstra?on   Installing  Couchbase  Server  &  Client
    17. 17. Basics:  Retrieve   •  get  (key)   ­   Retrieve  a  document   •  gets(key)   ­  Retrieve  a  document  and  the  CAS  value  associated  with  the  object   (more  on  this  in  a  bit)    
    18. 18. Basics:  Create,  Update,  Delete   •  set  (key,  value)   ­   Store  a  document,  overwrites  if  exists   •  add  (key,  value)   ­   Store  a  document,  error/excep-on  if  it  already  exists   •  replace  (key,  value)   ­   Store  a  document,  error/excep-on  if  doesn’t  exist   •  delete(key)   ­  Delete  the  document  from  the  system    
    19. 19. Fundamentals   •  Couchbase is structured as a Key-Value store: every Document has a Key and a Value. •  Key can be up to 250 characters long. •  Keys are unique, within a database (bucket), there can only be one document with a associated key. •  Keys are completely in the control of the application developer, there is no internal mechanism for key generation. •  Values can be JSON, strings, numbers, binary blobs, or a special positive atomic counter (unsigned integer). •  Values can be up to 20MB in size.
    20. 20. Dealing  with  keys   • You are responsible of the key • Keys can be build from: • UUID • Atomic Counter • Date/TimeStamp • Contains Separator: • User:001 • Product_XYZ
    21. 21. What  about  the  value  ?   •  Couchbase stores the data the way you send them •  For example: •  cb.set(“mykey”, “This is my Value”);! •  cb.set(“mykey”, 100.2);! •  cb.set(“mykey”, javaObjectSerialized);! •  What about JSON? •  cb.set(“mykey”, "{"msg" : "This is the value"}");! •  cb.set(“mykey”, json.toJson( myJavaObject) );! •  Your application deals with the JSON Document
    22. 22. Q&A   Next Session : Advanced Operations and Patterns
    23. 23. Ge#ng  Started  –  Part  2  
    24. 24. Basics:  Retrieve   •  get  (key)   ­   Retrieve  a  document   •  gets(key)   ­  Retrieve  a  document  and  the  CAS  value  associated  with  the  object   (more  on  this  in  a  bit)    
    25. 25. Basics:  Create,  Update,  Delete   •  set  (key,  value)   ­   Store  a  document,  overwrites  if  exists   •  add  (key,  value)   ­   Store  a  document,  error/excep-on  if  it  already  exists   •  replace  (key,  value)   ­   Store  a  document,  error/excep-on  if  doesn’t  exist   •  delete(key)   ­  Delete  the  document  from  the  system    
    26. 26. Other  Op?ons   •  Durability  Requirements   ­  PersistTo.ONE! ­  ReplicateTo.ONE! ­  Simply  add  this  to  your  method  call  when  needed   •  ops = cb.set(“mykey”, myObject, PersistTo.ONE);! •  Time  to  Live  (TTL)   ­  Used  to  delete  the  value  aTer  the  specified  -me   •  cb.set(“mykey”, 30 x 24 x 60 x 60, myObject);
 // keep the value in Couchbase for 30 days.!
    27. 27. Atomic  Integers   Atomic  Counters  are  a  special  structure  in  Couchbase,  they  are   executed  in  order  and  are  Posi?ve  Integer  Values   • set  (key,  value)   ­   Use  set  to  ini-alize  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  
    28. 28. Couchbase  PaFerns   • Atomic Counter for keys • Find your ID by numbers, loop on the values, … • Lookup • Lookup a document/values using multiple keys • Lists • Lookup pattern, using list of values • Indices and Queries
    29. 29. Use  a  Counter   cb = new CouchbaseClient(uris, "default", "");! Gson json = new Gson()! ! // create a new User! User user = new User("John Doe", "john@demo.com", "7621387216");! ! long userCounter = cb.incr("user_counter", 1, 1);! cb.set( "user:"+ userCounter, json.toJson(user) );! ! // create a new user;! user = new User("Jane Smith Doe", "jane@demo.com", "355662216");! userCounter = cb.incr("user_counter", 1);! cb.set( "user:"+ userCounter, json.toJson(user) );! !
    30. 30. Use  a  Counter  
    31. 31. Lookup   cb = new CouchbaseClient(uris, "default", "");! Gson json = new Gson()! ! // create a new User! User user = new User("John Doe", "john@demo.com", "7621387216");! ! long userCounter = cb.incr("user_counter", 1);! String key = "user:"+ userCounter;! cb.set(key, json.toJson(user) );! // create another key for lookup! cb.set(“email:john@demo.com”, key);! ! // Find by email! String keyToUser = cb.get(“email:john@demo.com”); ! Object user = cb.get(keyToUser);!
    32. 32. Agile  Model  Development  
    33. 33. Simple  Example  in  Ruby   #  example.rb   require  “./user.rb”     u1  =  User.new({            :email  =>  robin@couchbase.com,          :name  =>  “Robin  Johnson”,          :?tle  =>  “Developer  Advocate”,          :twiFer  =>  “@rbin”   })     u1.save   #  user.rb   require  “rubygems”   require  “couchbase”     class  User      aFr_accessor    :name,  :email,  :?tle,  :twiFer        def  ini?alize(aFr  =  {})              aFr.each  do  |name,  value|                      seFer  =  "#{name}=”                      next  unless  respond_to?(seFer)                      send(seFer,  value)                end      end        def  save              client  =  Couchbase.bucket              client.set(@email.downcase,  self.to_json)      end   end  
    34. 34. Add  Lookup  Class  Method     #  example.rb   require  “./user.rb”     u1  =  User.new({            :email  =>  robin@couchbase.com,          :name  =>  “Robin  Johnson”,          :?tle  =>  “Developer  Advocate”,          :twiFer  =>  “@rbin”   })     u1.save     u1  =  User.find_by_email(“robin@couchbase.com”)     if  u1      puts  “User  Found!”      puts  u1.inspect   else      puts  “User  Not  Registered!”   end   #  user.rb   require  “rubygems”   require  “couchbase”     class  User   aFr_accessor    :name,  :email,  :?tle,  :twiFer     def  ini?alize(aFr  =  {})  ...  end     def  save          c  =  Couchbase.bucket            c.set(@email.downcase,  self.to_json)   end     def  self.find_by_email(email)          c  =  Couchbase.bucket          doc  =  c.get(email.downcase)          return  doc  ?  User.new(doc)  :  nil   end     end  
    35. 35. Agile  Model  Development     #  example.rb   require  “./user.rb”     u1  =  User.find_by_email(“robin@couchbase.com”)     if  u1          u1.y_id  =  “682829292”          u1.y_token  =  “f02jdjd020d8373730djd02”          u1.save   else          #  create  user   end   #  user.rb   require  “rubygems”   require  “couchbase”     class  User   aFr_accessor    :name,  :email,  :?tle,  :twiFer   aFr_accessor  :y_id,  :y_token     def  ini?alize(aFr  =  {})  ...  end     def  save  ...  end     def  self.find_by_email(email)        c  =  Couchbase.bucket        doc  =  c.get(email.downcase)              return  doc  ?  User.new(doc)  :  nil   end     end  
    36. 36. Concurrency  &  more  
    37. 37. Compare  and  Swap   Op-mis-c  Concurrency  in  a  Distributed  System   #  actors.rb     c  =  Couchbase.bucket   c.set(“mydoc”,  {  :myvalue  =>  nil  }     doc1,  flags,  cas  =  c.get(“mydoc”,              :extended  =>  true)     #   c.set  (“mydoc”,  {  “myvalue”:  true  },  :cas  =>  cas)     #  will  fail  because  cas  has  changed   c.set  (“mydoc”,  {  “myvalue”:  true  },  :cas  =>  cas)   Actor  1   Success   Actor  2   CAS  mismatch!   Couchbase  Server  
    38. 38. Check  Status  before  Saving   OperationStatus ops = null;! int numberOfAttemptsLeft = 5;! ! do {! CASValue listOfTagWithCas = cb.gets(KEY);! if (listOfTagWithCas == null) {! cb.set(KEY, tag).getStatus();! return;! } else {! ops = cb.append(listOfTagWithCas.getCas(), KEY, ","+ tag).getStatus();! if (ops.isSuccess()) {! return;! }! }! ! numberOfAttemptsLeft--;! } while (numberOfAttemptsLeft > 0);! ! ! if (ops != null && !ops.isSuccess()) {! throw new Exception("Failed to update item '"+ KEY+"' too many times, giving up!");! }!
    39. 39. Replica  Read   •  Read  the  replica  when  you  cannot  reach  the  node   responsible  of  the  data   try {! value = (String)cb.get(key);! System.out.println("Master node read");! ! } catch (Exception e ) {! value = (String)cb.getFromReplica(key);! System.out.println("Doing a Replica Read");! ! }!
    40. 40. Conclusion   • Couchbase Server is installed • Couchbase Client SDK is installed • Core operations to save and get the data • What’s next? • How do you design your data? Document Design • How do you find your data? Indexing and Querying • Do even more… Common Models and Integration
    41. 41. Q&A   Next Session : Advanced Operation and Sample Applications

    ×