portland developing with couchbase i: getting started
TRANSCRIPT
![Page 1: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/1.jpg)
1
Developing with Couchbase Part
I:Getting Started
John ZablockiDeveloper Advocate
@codevoyeur
![Page 2: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/2.jpg)
2
GETTING STARTED:DEVELOPMENT ENVIRONMENT
![Page 3: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/3.jpg)
3
Development Environment: Development DB
• Downloads at couchbase.com/download
• Provision via wizard in Web Console– Or provision via REST interface: operations folks to automate provisioning
(i.e Chef, puppet, Rightscale rightscript)
• 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
![Page 4: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/4.jpg)
4
Development Environment: Obtaining a Client
• High performance, official client libraries for Java, .NET, PHP, Ruby, C, Python*
• Head to couchbase.com/develop 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
![Page 5: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/5.jpg)
5
Client Setup: Getting Cluster Configuration
Couchbase Server Node
Couchbase Client
http://myserver:8091/pools
{…
"bucketCapabilities": [ "touch", "sync", "name": "default", "nodeLocator": "vbucket", "nodes"
….
Cluster Configuration over REST
Couchbase Server Node
Couchbase Server Node
Couchbase Server Node
![Page 6: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/6.jpg)
6
Client at Runtime: Adding a node
Couchbase Client
Couchbase Server Node
Couchbase Server Node
Couchbase Server Node
Couchbase Server Node
Couchbase Server Node
Cluster
Topology
Update
New node coming online
![Page 7: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/7.jpg)
7
Client Set up at a Code Level
// Set up at least two URIs in case one server fails
URI oneserver = new URI("http://10.1.6.171:8091/pools/");
URI twoserver = new URI("http://10.1.6.172:8091/pools/");
List<URI> servers = new ArrayList<URI>();
servers.add(oneserver);
servers.add(twoserver);
// Now create a client talking to the default bucket
CouchbaseClient 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.
![Page 8: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/8.jpg)
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
![Page 9: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/9.jpg)
9
Common Questions with Metadata
Document Metadata:• TTL• CAS value• Flags
Q: 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 as compression. Behavior varies from client to client.
![Page 10: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/10.jpg)
10
Distributed System Design: Concurrency Controls
• Compare and Swap Operations– Often referred to as “CAS”
– Optimistic concurrency control
– Available with many mutation operations, depending on client.
• Get with Lock– Often referred to as “GETL”
– Pessimistic concurrency control
– Locks have a short TTL
– Locks released with CAS operations
– Useful when working with object graphs
Actor 1 Actor 2
Couchbase Server
CAS mismatchSuccess
A
B
F
C D
E
![Page 11: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/11.jpg)
11
INTRODUCING DOCUMENTS
![Page 12: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/12.jpg)
12
A JSON Document
{
"_id": "beer_Hoptimus_Prime",
"abv": 10.0,
"brewery": "Legacy Brewing Co.",
"category": "North American Ale",
"name": "Hoptimus Prime",
"style": "Imperial or Double India Pale Ale",
"updated": [2010, 7, 22, 20, 0, 20],
"available": true
}
The primary key
A float
Date time as arrayA Boolean
![Page 13: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/13.jpg)
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}
![Page 14: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/14.jpg)
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));
![Page 15: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/15.jpg)
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
![Page 16: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/16.jpg)
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?
![Page 17: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/17.jpg)
17
Common Questions when Adopting Couchbase
Q: What if I need to fetch referenced documents?
A: Simply get them one after another. It’s very fast owing to our cache and storage 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, then use CAS operations to store modified documents. The Ruby sample application has a good example.
Q: I currently use serialized objects with memcached or Membase, can I do this still with Couchbase Server?
A: Absolutely! Everything previously supported and used is still there. JSON offers advantages with heterogenenous platform support and preparing for Couchbase 2.0 views.
![Page 18: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/18.jpg)
18
COUCHBASE SERVER 2.0 COMPATIBLE LIBRARY FEATURES
![Page 19: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/19.jpg)
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
![Page 20: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/20.jpg)
21
Q&A
![Page 21: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/21.jpg)
22
OTHER EXAMPLE DOCUMENTS
![Page 22: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/22.jpg)
23
Storing a Message Document
Document:
{
"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);
![Page 23: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/23.jpg)
24
User Profile Document
{
"user_id": 512,
"name": "Beer Likington",
"email": "[email protected]",
"sign_up_timestamp": 1224612317,
"last_login_timestamp": 1245613101
}
{
"user_id": 768,
"name": "Simon Neal",
"email": "[email protected]",
"sign_up_timestamp": 1225554317,
"last_login_timestamp": 1234166701,
"country": "Scotland",
"pro_account" true,
"friends": [512, 666, 742, 1111]
}
![Page 24: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/24.jpg)
25
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
}
![Page 25: Portland Developing with Couchbase I: Getting Started](https://reader031.vdocuments.us/reader031/viewer/2022032419/55a2c9591a28ab1a6c8b462e/html5/thumbnails/25.jpg)
26
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]
}