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.

Developing with couchbase i getting started v3


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Developing with couchbase i getting started v3

  1. 1. Developing with Couchbase Part I: Getting Started John Zablocki Developer Advocate 1
  2. 2. About Me{ " _id" : "John Zablocki”, "Title" : "Developer Advocate", "Company" : "Couchbase", "Books" : [ { "Title" : "Orchard CMS", "Publisher" : "OReilly" } ], "Twitter" : "@CodeVoyeur", "Email" : "", "Meetup" : "Beantown ALT.NET“, "Code" : [ "", ], "Blogs" : [ "", "" ]} 2
  3. 3. Agenda• Setup• Operations• Introducing Documents• Common Questions• Resources and Summary• Q/A 3
  4. 4. SETUPSetup Operations Introducing Common Resources and Documents Questions Summary 4
  5. 5. Development Environment: Development DB• Downloads at • Linux: Ubuntu and Red Hat/CentOS – Packages for most common distributions. • dpkg -i , rpm -i, etc. • Mac – Download a .zip, open, drag to Applications, • Windows – Download a setup.exe, double click • Provision via wizard in Web Console – Or provision via REST interface: operations folks to automate provisioning (i.e Chef, puppet, Rightscale rightscript) 5
  6. 6. Development Environment: Obtaining a Client• High performance, official client libraries for Java, .NET, PHP, Ruby, C, Python*• Head to for SDKs where you will find – Client libraries – Screencasts on Getting Started – Getting started guides – Tutorial with a sample application – A complete API reference 6
  7. 7. Client Setup: Getting Cluster Configuration Couchbase Client Cluster Configuration over REST {
… "bucketCapabilities": [
 "name": "default", 
 "nodeLocator": "vbucket", 
http://myserver:8091/ …. Couchbase Couchbase Couchbase Couchbase Server Node Server Node Server Node Server Node 7
  8. 8. Client at Runtime: Adding a node Couchbase Client New node Cluster coming online Topology UpdateCouchbase Couchbase Couchbase Couchbase Couchbase Server Server Server Server Server Node Node Node Node Node 8
  9. 9. OPERATIONSSetup Operations Introducing Common Resources and Documents Questions Summary 9
  10. 10. A Simple “Hello World” Program<?php$cb = new Couchbase(""); // uses the default bucket// or specify a specific bucket like so://$cb = new Couchbase(”host:8091", "bucket", "pass", "bucket");$cb->set("spoon", "Hello World");var_dump($cb->get("spoon"));?> 10
  11. 11. Writing JSON<?php$cb = new Couchbase("”);$cb->set("beer_Zelta", "{"brewery":"Aldaris","name":"Zelta","abv":"5","updated":"2010-07-22 20:00:20"}");var_dump($cb->get("beer_Zelta"));?> Use your platform’s favorite JSON library: Jettison, Google GSON, etc. Use other client libraries such as JSON.NETfor .NET 11
  12. 12. Operations Available to a Client of Couchbase Server• Store Operations – Add: Store the document if it does not yet exist – Set: Store the document, overwriting existing if necessary• Retrieve Operations – Get: Fetch the document – Get and touch: Fetch the document and update the TTL – Getl/unlock: Get Lock and Unlock (more later) – Multiget: Fetch multiple documents at the same time• Update operations – Append/prepend: Add data in front of or on the end of a document – Delete: Remove the document from the store – Compare and Swap (CAS): Replace the current document, if CAS matches – Replace: Replace the document if it exists, otherwise do not – Touch: Update the TTL for a document Note: Not all Client Libraries are created equal 12
  13. 13. Time To Live (TTL)• TTL – Property to set expiration on the document – the actual value sent may either be – Unix time (number of seconds since January 1, 1970, as a 32- bit value – OR number of seconds starting from current time. number of seconds may not exceed 60*60*24*30 (number of seconds in 30 days) – if the number sent by a client is larger than – that, the server will consider it to be real Unix time value rather than an offset from current time. 13
  14. 14. A Distributed Hash Table Application set(key, value) get(key) returns value Database DData Cluster 14
  15. 15. A Distributed Hash Table Document Store Application set(key, json) get(key) returns json Database{ DData“id": ”brewery_Legacy”,“type” : “brewery”,"name" : "Legacy Brewing”,“address": "525 Canal Street Reading","updated": "2010-07-22 20:00:20",} Cluster 15
  16. 16. Distributed System Design: Concurrency Controls Actor 1 Actor 2 • Compare and Swap Operations – Often referred to as “CAS” – Optimistic concurrency control Success CAS mismatch – Available with many mutation operations, depending on client. • Get with Lock Couchbase Server – Often referred to as “GETL” – Pessimistic concurrency control – Locks have a short TTL A – Locks released with CAS operations – Useful when working with object B C D graphs F E 16
  17. 17. INTRODUCING DOCUMENTSSetup Operations Introducing Common Resources and Documents Questions Summary 17
  18. 18. Sample App -- Beernique 18
  19. 19. E-R Diagram for Beernique (1, N) User Drinks Name email (1, N) Beer Brew (1,M)Id Name ABV … Brewery … Id Name State 19
  20. 20. A JSON Document The primary key{ “id": "beer_Hoptimus_Prime", “type”: “beer”, "abv": 10.0, The type information "brewery": "Legacy Brewing Co.", A float "category": "North American Ale", "name": "Hoptimus Prime", "style": "Imperial or Double India Pale Ale",} 20
  21. 21. Other Documents and Document Relationships{ “id": "beer_Hoptimus_Prime", “type” : “beer”, "abv": 10.0, "brewery": ”brewery_Legacy_Brewing_Co", "category": "North American Ale", "name": "Hoptimus Prime", "style": “Double India Pale Ale”} { “id": ”brewery_Legacy_Brewing_Co”, “type” : “brewery”, "name" : "Legacy Brewing Co.", "address": "525 Canal Street Reading, Pennsylvania, 19601 United States", "updated": "2010-07-22 20:00:20", "latitude": -75.928469, Spatial "longitude": 40.325725 } 21
  22. 22. Simplicity of Document Oriented Datastore• Schema is optional – Technically, each document has an implicit schema – Extend the schema at any time! • Need a new field? Add it. Define a default for similar objects which may not have this field yet.• Data is self-contained – Documents more naturally support the world around you, the data structures around you• Model data for your App/Code instead for the Database 22
  23. 23. Adding a Document: Observations and Considerations• Observations – Conversion to document was very simple, many JSON options – Flexible schema: Did not need to add the latitude and longitude to every record – Flexible schema: Can add the brewery detail later• Considerations – Use a “type” field for high level filtering on object types – Why use a particular key/_id : ”beer_My_Brew” – Should I have a TTL? 23
  24. 24. COMMON QUESTIONSSetup Operations Introducing Common Resources and Documents Questions Summary 24
  25. 25. Common Questions when Adopting CouchbaseQ: What if I need to fetch referenced documents?A: Simply get them one after another or use another View.Q: How can I update just a small portion of a document?A: The best approach is to keep the document model live in your application, thenuse CAS operations to store modified documents.Q: I currently use serialized objects with memcached or Membase, can I do this stillwith Couchbase Server?A: Absolutely! Everything previously supported and used is still there. JSON offersadvantages with heterogeneous platform support and preparing for Couchbase2.0 views.Q: How do I use flags?A: Frequently used to identify data type or other attributes, such as compression.Behavior varies from client to client. 25
  26. 26. Couchbase Server 2.0: Views • Views can cover a few different use cases – Simple secondary indexes (the most common) – Aggregation functions • Example: count the number of North American Ales – Organizing related data 26
  27. 27. Indexing and Querying APP SERVER 1 APP SERVER 2 APP SERVER 1 APP SERVER 2 COUCHBASE CLIENT LIBRARY COUCHBASE CLIENT LIBRARY COUCHBASE CLIENT LIBRARY COUCHBASE CLIENT LIBRARY  Indexing work is distributed CLUSTER MAP MAP CLUSTER CLUSTER MAPMAP CLUSTER amongst nodes  Large data set possible  Parallelize the effort  Each node has index for data Query Response stored on it  Queries combine the results from required nodes SERVER 1 SERVER 2 SERVER 3 Active Docs Active Docs Active Docs Doc 5 DOC Doc 4 DOC Doc 1 DOC Doc 2 DOC Doc 7 DOC Doc 3 DOC Doc 9 DOC Doc 8 DOC Doc 6 DOC Replica Docs Replica Docs Replica Docs 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 27
  28. 28. RESOURCES AND SUMMARYSetup Operations Introducing Common Resources and Documents Questions Summary 28
  29. 29. Resources, Summary and Call For Action• Couchbase Server Downloads – –• Developing with Client libraries –• Couchbase forums – 29
  30. 30. THANKS - Q&AJohn Zablocki, Couchbase Inc.(, @codevoyeur) 30