Developing with couchbase i getting started v3

30
1 Developing with Couchbase Part I: Getting Started John Zablocki Developer Advocate [email protected]

Transcript of Developing with couchbase i getting started v3

Page 1: Developing with couchbase i getting started v3

1

Developing with Couchbase Part I:

Getting StartedJohn ZablockiDeveloper Advocate

[email protected]

Page 2: Developing with couchbase i getting started v3

2

{ " _id" : "John Zablocki”, "Title" : "Developer Advocate", "Company" : "Couchbase", "Books" : [ { "Title" : "Orchard CMS", "Publisher" : "O'Reilly" } ], "Twitter" : "@CodeVoyeur", "Email" : "[email protected]", "Meetup" : "Beantown ALT.NET“, "Code" : ["https://github.com/jzablocki", http://bitbucket.com/johnzablockis], "Blogs" : ["http://dllhell.net", "http://blog.couchbase.com"]}

About Me

Page 3: Developing with couchbase i getting started v3

3

• Setup• Operations• Introducing Documents• Common Questions• Resources and Summary• Q/A

Agenda

Page 4: Developing with couchbase i getting started v3

4

SETUP

Setup Operations IntroducingDocuments

CommonQuestions

Resources andSummary

Page 5: Developing with couchbase i getting started v3

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

Page 6: Developing with couchbase i getting started v3

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

Page 7: Developing with couchbase i getting started v3

7

Client Setup: Getting Cluster Configuration

Couchbase Server Node

Couchbase Client

http://myserver:8091/

{…    "bucketCapabilities": [        "touch",         "sync",         "couchapi"    ],     "bucketCapabilitiesVer": "sync-1.0",     "bucketType": ”couchbase",    "name": "default",     "nodeLocator": "vbucket",     "nodes": [….

Cluster Configuration over REST

Couchbase Server Node

Couchbase Server Node

Couchbase Server Node

Page 8: Developing with couchbase i getting started v3

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 TopologyUpdate

New node coming online

Page 9: Developing with couchbase i getting started v3

9

OPERATIONS

Setup Operations IntroducingDocuments

CommonQuestions

Resources andSummary

Page 10: Developing with couchbase i getting started v3

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"));

?>

Page 11: Developing with couchbase i getting started v3

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 your platform’s favorite JSON library: Jettison, Google GSON, etc.Use other client libraries such as JSON.NETfor .NET

Page 12: Developing with couchbase i getting started v3

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 documentNote: Not all Client Libraries are created equal

Page 13: Developing with couchbase i getting started v3

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)

Page 14: Developing with couchbase i getting started v3

14

A Distributed Hash Table

Application

set(key, value) get(key)returns value

DData

Cluster

Database

Page 15: Developing with couchbase i getting started v3

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",}

Page 16: Developing with couchbase i getting started v3

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

Page 17: Developing with couchbase i getting started v3

17

INTRODUCING DOCUMENTS

Setup Operations IntroducingDocuments

CommonQuestions

Resources andSummary

Page 18: Developing with couchbase i getting started v3

18

Sample App -- Beernique

Page 19: Developing with couchbase i getting started v3

19

E-R Diagram for Beernique

Beer

Id …Name ABV

Brewery

Brew

Name

(1,M)

State

(1, N)

Id…

User

emailName

Drinks

(1, N)

Page 20: Developing with couchbase i getting started v3

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

Page 21: Developing with couchbase i getting started v3

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}

Spatial

Page 22: Developing with couchbase i getting started v3

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

Page 23: Developing with couchbase i getting started v3

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?

Page 24: Developing with couchbase i getting started v3

24

COMMON QUESTIONS

Setup Operations IntroducingDocuments

CommonQuestions

Resources andSummary

Page 25: Developing with couchbase i getting started v3

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 Couchbase 2.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.

Page 26: Developing with couchbase i getting started v3

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

Page 27: Developing with couchbase i getting started v3

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

Page 28: Developing with couchbase i getting started v3

28

RESOURCES AND SUMMARY

Setup Operations IntroducingDocuments

CommonQuestions

Resources andSummary

Page 29: Developing with couchbase i getting started v3

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

Page 30: Developing with couchbase i getting started v3

30

THANKS - Q&A

30

John Zablocki, Couchbase Inc.([email protected], @codevoyeur)