View
3.738
Download
3
Category
Tags:
Preview:
Citation preview
1
Developing with Couchbase Part I:
Getting StartedMatt Ingenthron
Director, Developer Solutions
2
GETTING STARTED:DEVELOPMENT ENVIRONMENT
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
4
Development Environment: Obtaining a Client
• High performance, official client libraries for Java, .NET, PHP, Ruby, C
• 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
5
Client Setup: Getting Cluster Configuration
Couchbase Server Node
Couchbase Client
http://myserver:8091/pools
{… "bucketCapabilities": [ "touch", "sync", "couchapi" ], "bucketCapabilitiesVer": "sync-1.0", "bucketType": "membase", "name": "default", "nodeLocator": "vbucket", "nodes": [….
Cluster Configuration over REST
Couchbase Server Node
Couchbase Server Node
Couchbase Server Node
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 TopologyUpdate
New node coming online
7
Client Set up at a Code Level
// Set up at least two URIs in case one server failsURI 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 bucketCouchbaseClient cbc = new CouchbaseClient(servers, "default", "");
System.err.println(cbc.get("MattIngenthron") + " is off developing with Couchbase!");
Use the Java client with your favorite JSON library: Jettison, Google GSON, etc.
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
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.
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
11
INTRODUCING DOCUMENTS
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 array
A Boolean
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": "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}
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));
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
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 my
kitchen– Flexible schema: Can add the brewery detail later
• Considerations– Why use a particular key/_id : "beer_My_Brew”– Should I have a TTL?
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.
18
COUCHBASE SERVER 2.0 COMPATIBLE LIBRARY FEATURES
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
21
Q&A
22
OTHER EXAMPLE DOCUMENTS
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);
24
User Profile Document
{ "user_id": 512, "name": "Beer Likington", "email": "beer.like@gmail.com", "sign_up_timestamp": 1224612317, "last_login_timestamp": 1245613101}
{ "user_id": 768, "name": "Simon Neal", "email": "sneal@gmail.com", "sign_up_timestamp": 1225554317, "last_login_timestamp": 1234166701, "country": "Scotland", "pro_account" true, "friends": [512, 666, 742, 1111]}
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}
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]}
Recommended