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.

Portland Developing with Couchbase I: Getting Started


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Portland Developing with Couchbase I: Getting Started

  1. 1. Developing with Couchbase Part I: Getting Started John Zablocki Developer Advocate @codevoyeur 1
  3. 3. 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) 3
  4. 4. 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 *Current version 0.6 as of 2012-03-13 4
  5. 5. Client Setup: Getting Cluster Configuration Couchbase Client Cluster Configuration over REST {
… "bucketCapabilities": [
 "name": "default", 
 "nodeLocator": "vbucket", 
 …. http://myserver:8091/pools Couchbase Couchbase Couchbase Couchbase Server Server Server Server Node Node Node Node 5
  6. 6. 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 6
  7. 7. Client Set up at a Code Level// Set up at least two URIs in case one server failsURI oneserver = new URI("");URI twoserver = new URI("");List<URI> servers = new ArrayList<URI>();servers.add(oneserver);servers.add(twoserver);// Now create a client talking to the default bucketCouchbaseClient cbc = new CouchbaseClient(servers, "default", "");System.err.println(cbc.get(“JohnZablocki") + " is off developing with Couchbase!"); Use the Java client with your favorite JSON library: Jettison, Google GSON, etc. 7
  8. 8. 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 – 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 8
  9. 9. Common Questions with MetadataDocument Metadata:• TTL• CAS value• FlagsQ: What happens to a document persisted after it’s TTL?A: A regular background job will remove expired documents.Q: How do I use flags?A: Frequently used to identify data type or other attributes, such ascompression. Behavior varies from client to client. 9
  10. 10. 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 10
  12. 12. A JSON Document The primary key{ "_id": "beer_Hoptimus_Prime", "abv": 10.0, "brewery": "Legacy Brewing Co.", A float "category": "North American Ale", "name": "Hoptimus Prime", "style": "Imperial or Double India Pale Ale", "updated": [2010, 7, 22, 20, 0, 20], "available": true} Date time as array A Boolean 12
  13. 13. Other Documents and Document Relationships{ "_id": "beer_Hoptimus_Prime", "abv": 10.0, "brewery": "Legacy Brewing Co.", "category": "North American Ale", "name": "Hoptimus Prime", "style": “Double India Pale Ale", "updated": [2010, 7, 22, 20, 0, 20], "available": true } { "_id": "brewery_Legacy_Brewing_Co", "name" : "Legacy Brewing Co.", "address": "525 Canal Street Reading, Pennsylvania, 19601 United States", "updated": "2010-07-22 20:00:20", "latitude": -75.928469, "longitude": 40.325725} 13
  14. 14. Adding (a document) to the Bucket of Beers • Simply create the document, then add <?php include "Couchbase.php"; $cb = new Couchbase; $cb->addCouchbaseServer("http://localhost:8091"); // defaults to the “default” bucket // a very simple brew $mybrew = new stdObj; $mybrew->brewery = "The Kitchen"; $cb->set("beer_My_Brew", json_encode($mybrew)); 14
  15. 15. 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 15
  16. 16. 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 of Matt’s kitchen – Flexible schema: Can add the brewery detail later• Considerations – Why use a particular key/_id : "beer_My_Brew” – Should I have a TTL? 16
  17. 17. Common Questions when Adopting CouchbaseQ: What if I need to fetch referenced documents?A: Simply get them one after another. It’s very fast owing to our cache andstorage model.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. The Ruby sample applicationhas a good example.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 heterogenenous platform support and preparing for Couchbase2.0 views. 17
  19. 19. Couchbase Server 2.0: Views • Views can spread a few different uses – Simple secondary indexes (the most common) – Aggregation functions • Example: count the number of North American Ales – Organizing related data 19
  20. 20. Q&A 21
  22. 22. Storing a Message DocumentDocument:{ "from": "user_512", "to": "user_768", "text": "Hey, that Beer you recommended is pretty fab, thx!" "sent_timestamp": 1326476560}<?php// store it$cb = new Couchbase;$cb->addCouchbaseServer("localhost");$cb->set("message_1024", $message); 23
  23. 23. User Profile Document{ "user_id": 512, "name": "Beer Likington", "email": "", "sign_up_timestamp": 1224612317, "last_login_timestamp": 1245613101}{ "user_id": 768, "name": "Simon Neal", "email": "", "sign_up_timestamp": 1225554317, "last_login_timestamp": 1234166701, "country": "Scotland", "pro_account" true, "friends": [512, 666, 742, 1111]} 24
  24. 24. Game Score Document{ "game_id": 12415115, "scores": { "user_a": { "user_id": 512, "score_a": 1, "score_b": 57, "score_c": 24 }, "user_b": { "user_id": 768, "score_a": 3, "score_b": 67, "score_c": 15 } }, "score_timestamp": 1348560842} 25
  25. 25. Photo Metadata Document{ "photo_id": "ccbcdeadbeefacee", "size": { "w": 500, "h": 320, "unit", "px" }, "exposure: "1/1082", "aperture": "f/2.4", "flash": false, "camera": { "name": "iPhone 4S", "manufacturer": "Apple", } "user_id": 512, "timestamp": [2011, 12, 13, 16, 31, 07]} 26