boston developing with couchbase i getting started v3
TRANSCRIPT
1
Developing with Couchbase Part
I:Getting Started
Raghavan “Rags” SrinivasDeveloper Advocate
2
• Architect and Evangelist working with developers
• Speaker at JavaOne, RSA conferences, Sun Tech Days, JUGs and other developer conferences
• Taught undergrad and grad courses
• Technology Evangelist at Sun Microsystems for 10+ years
• Still trying to understand and work more effectively on Java and distributed systems
• Couchbase Developer Advocate working with Java and Ruby developers
• Philosophy: “Better to have an unanswered question than a unquestioned answer”
Speaker Introduction
3
• Setup
• Operations
• Introducing Documents
• Common Questions
• Resources and Summary
• Q/A
Agenda
5
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
6
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
7
Client Setup: Getting Cluster Configuration
Couchbase Server
Node
Couchbase Client
http://myserver:8091/
{…
"bucketCapabilities": [ "touch", "sync", "name": "default", "nodeLocator": "vbucket", "nodes"
….
Cluster Configuration over REST
Couchbase Server
Node
Couchbase Server
Node
Couchbase Server
Node
8
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
10
A Simple “Hello World” Program
<?php
$cb = new Couchbase("127.0.0.1:8091"); // 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"));
?>
11
Writing JSON
<?php
$cb = new Couchbase("127.0.0.1:8091”);
$cb->set("beer_Zelta",
"{\"brewery\":\"Aldaris\",\"name\":\"Zelta\",\"abv\":\"5\",\"updated\":\"2010
-07-22 20:00:20\"}");
var_dump($cb->get("beer_Zelta"));
?>
Use the Java client with your favorite JSON library: Jettison, Google
GSON, etc.
Use other client libraries such as yajl for Ruby
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
13
• 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.
Time To Live (TTL)
14
A Distributed Hash Table
Application
set(key, value) get(key)returns value
DData
Cluster
Database
15
A Distributed Hash Table Document Store
Application
set(key, json) get(key)returns json
DData
Cluster
Database
{
“id": ”brewery_Legacy”,
“type” : “brewery”,
"name" : "Legacy Brewing”,
“address": "525 Canal Street Reading",
"updated": "2010-07-22 20:00:20",
}
16
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
19
E-R Diagram for Beernique
Beer
Id …Name ABV
Brewery
Brew
Name
(1,M)
State
(1, N)
Id…
User
emailName
Drinks
(1, N)
20
A JSON Document
{
“id": "beer_Hoptimus_Prime",
“type”: “beer”,
"abv": 10.0,
"brewery": "Legacy Brewing Co.",
"category": "North American Ale",
"name": "Hoptimus Prime",
"style": "Imperial or Double India Pale Ale",
}
The primary key
A float
The type information
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,
"longitude": 40.325725
}
Afterthought
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
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?
25
Common Questions when Adopting Couchbase
Q: 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, then use CAS operations to store modified documents.
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 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.
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
27
Indexing and Querying
APP SERVER 1
COUCHBASE CLIENT LIBRARY Indexing work is distributed amongst nodes Large data set possible
Parallelize the effort
Each node has index for data stored on it
Queries combine the results from required nodes
CLUSTER MAP
Doc 4
Doc 2
Doc 5
SERVER 1
Doc 6
Doc 4
SERVER 2
Doc 7
Doc 1
SERVER 3
Doc 3
APP SERVER 2
COUCHBASE CLIENT LIBRARY
CLUSTER MAP
Doc 9
Doc 7
Doc 8 Doc 6
Doc 3
DOC
DOC
DOC
DOC
DOC
DOC
DOC
DOC
DOC
DOC
DOC
DOC
DOC
DOC
DOC
Doc 9
Doc 5
DOC
DOC
DOC
Doc 1
Doc 8 Doc 2
Replica Docs Replica Docs Replica Docs
Active Docs Active Docs Active Docs
COUCHBASE CLIENT LIBRARY COUCHBASE CLIENT LIBRARY
CLUSTER MAP CLUSTER MAP
APP SERVER 1 APP SERVER 2
QueryResponse
29
• Couchbase Server Downloads
– http://www.couchbase.com/downloads-all
– http://www.couchbase.com/couchbase-server/overview
• Developing with Client libraries
– http://www.couchbase.com/develop/
• Couchbase forums
– http://www.couchbase.com/forums/
Resources, Summary and Call For Action