Transcript
Page 1: Couchbase 102 - SDK Operations
Page 2: Couchbase 102 - SDK Operations

Technical  Evangelist

twi0er:  @scalabl3email:  [email protected]

Jasdeep  Jaitla

Couchbase  102:  SDK  OperaFons

Page 3: Couchbase 102 - SDK Operations
Page 4: Couchbase 102 - SDK Operations

SETUP  SDK

Page 5: Couchbase 102 - SDK Operations

Supported SDK'swww.couchbase.com/communi/es

• Each  supported  SDK  page  has  instrucFons  for  setup  

• PHP,  Ruby,  NodeJS  and  Python  clients  are  wrappers  around  libcouchbase  C  library,  so  libcouchbase  must  be  installed  first  

• For  other  community  clients,  click  on  "All  Clients"  on  leS  nav,  scroll  down  the  page  and  you  can  see  clients  for  Go,  Erlang,  Clojure,  TCL,  other  nodejs  and  Perl.

Page 6: Couchbase 102 - SDK Operations

Installing Libcouchbase

• Mac  Tips  before  Libcouchbase  &  SDK  Install  

• Make  sure  you  have  XCode  &  Command  Line  Tools  Installed  

• Install  Homebrew  if  you  don't  have  it  already  

• Do  a  $  brew  update,  $  brew  upgrade  and  $  brew  doctor  to  be  sure  you're  up  to  date

www.couchbase.com/communi/es/c/geBng-­‐started

Page 7: Couchbase 102 - SDK Operations

Installing Libcouchbase

• Mac  Via  Homebrew  

• $  brew  install  libcouchbase  

• PC-­‐Windows    

• Download  appropriate  Zip  from  website  

• Redhat/CentOS  

• wget  the  yum  repositories  

• $  sudo  yum  install  -­‐y  libcouchbase2-­‐libevent  libcouchbase-­‐devel  

• Ubuntu  

• wget  ubuntu  repositories  

• $  sudo  apt-­‐get  install  libcouchbase2-­‐libevent  libcouchbase-­‐dev

www.couchbase.com/communi/es/c/geBng-­‐started

Page 8: Couchbase 102 - SDK Operations

Client Connections

Couchbase Server8091

11210

8092

11211Application Server

Page 9: Couchbase 102 - SDK Operations

Client Connections

Couchbase Server8091

11210

8092

11211Application Server

HTTP

Cluster  Configura/on  and  Cluster  Par//on  Map

Page 10: Couchbase 102 - SDK Operations

Client Connections

Couchbase Server8091

11210

8092

11211Application Server

HTTP

Cluster  Configura/on  and  Cluster  Par//on  Map

MAP

Page 11: Couchbase 102 - SDK Operations

Client Connections

Couchbase Server8091

11210

8092

11211Application Server

HTTP

MAP

Page 12: Couchbase 102 - SDK Operations

Client Connections

Couchbase Server8091

11210

8092

11211Application Server

HTTP

MAP

TCP Binary

Create  Socket  Connec/on  to  Each  Node  in  Cluster

Page 13: Couchbase 102 - SDK Operations

Client Connections

Couchbase Server8091

11210

8092

11211Application Server

HTTP

MAP

TCP Binary

Page 14: Couchbase 102 - SDK Operations

Client Connections

Couchbase Server8091

11210

8092

11211

HTTP

Application Server

HTTP

MAP

View  Querying

TCP Binary

Page 15: Couchbase 102 - SDK Operations

Client Connections

Couchbase Server8091

11210

8092

11211

HTTP

Application Server

HTTP

MAP

TCP Binary

Page 16: Couchbase 102 - SDK Operations

Make a Connectionrequire 'rubygems'!require 'couchbase'! !cb = Couchbase.connect(!

:bucket => "default",!:hostname => "localhost")!!

data = { jsonkey: "value", created_at: Time.now }!!cb.add("mydata", data)!puts cb.get("mydata")

#!/usr/bin/env python!from couchbase import Couchbase!from pprint import pprint!from datetime import datetime!!cb = Couchbase.connect(bucket='default')!!data = { "jsonkey": "value", "created_at": datetime.now() }!!cb.add('mydata', data)!result = cb.get('mydata')!pprint(result.value, indent=4)!

RUBY

PYTHON

Page 17: Couchbase 102 - SDK Operations

Make a Connectionimport com.couchbase.client.CouchbaseClient;!import java.net.URI;!import java.util.*;!import com.google.gson.Gson;!import com.google.gson.GsonBuilder;!!public class HelloWorld {! !public static void main(String[] args) throws Exception {!

 !List<URI> hosts = Arrays.asList(!new URI("http://127.0.0.1:8091/pools")!

);!!

CouchbaseClient cb = new CouchbaseClient(hosts, "default", "");!Gson json = new Gson();!!Hashtable data = new Hashtable();!data.put("jsonkey", "value");!data.put("created_at", new Date());!!cb.add("mydata", json.toJson(data))!System.out.println(cb.get("mydata"));!

!cb.shutdown();!

}!}!

JAVA

Page 18: Couchbase 102 - SDK Operations

Make a Connection

var Couchbase = require('couchbase');!var cb = new Couchbase.Connection({bucket: "default"}, function(err) { });!!var data = { jsonkey: "value", created_at: new Date().toString() };!cb.add("mydata", data);!!console.log(cb.get("mydata"));!

NODEJS

<?php!// adjust these parameters to match your installation!$cb = new Couchbase("127.0.0.1:8091", "", "", "default");!!$data = array("jsonkey" => "value", ! "created_at" => date("Y-m-d H:i:s"));!!$cb->add("mydata",json_encode($data));!var_dump($cb->get("mydata"));!!?>!

PHP

Page 19: Couchbase 102 - SDK Operations
Page 20: Couchbase 102 - SDK Operations

OPERATIONS

Page 21: Couchbase 102 - SDK Operations

Couchbase Organization

• Couchbase operates like a Key-Value Document Store - Simple Datatypes: strings, numbers, datetime (string),

boolean, and binary data (string) can be stored- Complex Datatypes: dictionaries/hashes, arrays/lists, can

be stored in JSON format (simple lists can be string based with delimiter)

- JSON is a special class of string with a specific format for encoding simple and complex data structures

• Schema is unenforced and implicit, schema changes are programmatic, done online, and can vary from Document to Document

Page 22: Couchbase 102 - SDK Operations

• get  (key)  –  Retrieve  a  document  

• set  (key,  value)  –  Store  a  document,  overwrites  if  exists  

• add  (key,  value)  –  Store  a  document,  error/excepFon  if  exists  

• replace  (key,  value)  –  Store  a  document,  error/excepFon  if  doesn’t  exist  

• cas  (key,  value,  cas)  –  Compare  and  swap,  mutate  document  only  if  it  hasn’t  changed  while  execuFng  this  operaFon

Store  &  Retrieve  OperaFons

Page 23: Couchbase 102 - SDK Operations

Atomic Counter OperationsThese  operaFons  are  always  executed  in  order  atomically.  !

• incr  (key)  –  Increase  an  atomic  counter  value,  default  by  1  • cb.incr(“my_counter”)  #  now  it’s  2  

• decr  (key)  –  Decrease  an  atomic  counter  value,  default  by  1  • cb.decr(“my_counter”)  #  now  it’s  1

Page 24: Couchbase 102 - SDK Operations

Non-JSON OperationsYou  can  use  these  creaFvely!  !

• prepend  (key,  value)  –  prepend  exisFng  string  in  couchbase  with  value  • cb.prepend(“mykey”,  "jumped  over  the  lazy  dog")    • cb.prepend("mykey",  "the  brown  fox  ")    

• append  (key,  value)  –  append  exisFng  string  in  couchbase  with  value  • cb.append(“mykey2”,  "oranges")    • cb.append("mykey2",  "  apples  bananas")

Page 25: Couchbase 102 - SDK Operations

Retrieval Operations

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

Page 26: Couchbase 102 - SDK Operations

Retrieval Operations

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

get

SELECT  *  WHERE  KEY  =  “mykey”

Page 27: Couchbase 102 - SDK Operations

Storage Operations - set

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

Page 28: Couchbase 102 - SDK Operations

Storage Operations - set

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

set

INSERT/UPDATE  WHERE  KEY  =  “mykey”

Page 29: Couchbase 102 - SDK Operations

Storage Operations - add

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

Page 30: Couchbase 102 - SDK Operations

Storage Operations - add

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

add

INSERT  KEY  =  VALUE

Page 31: Couchbase 102 - SDK Operations

Storage Operations - add

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

Page 32: Couchbase 102 - SDK Operations

Storage Operations - add

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

add

ALREADY  EXISTS!

Page 33: Couchbase 102 - SDK Operations

Storage Operations - replace

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

Page 34: Couchbase 102 - SDK Operations

Storage Operations - replace

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

replace

UPDATE  SET  KEY  =  VALUE  WHERE  KEY  =  “mykey”

Page 35: Couchbase 102 - SDK Operations

Storage Operations - replace

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

Page 36: Couchbase 102 - SDK Operations

Storage Operations - replace

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

replace

DOESN’T  EXIST!

Page 37: Couchbase 102 - SDK Operations

Consistency

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

Page 38: Couchbase 102 - SDK Operations

Consistency

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

get

Page 39: Couchbase 102 - SDK Operations
Page 40: Couchbase 102 - SDK Operations

CONCURRENCY

Page 41: Couchbase 102 - SDK Operations

Optimistic Concurrency with CAS

• Every storage operation (including touch) creates a new "CAS" value, which is just a long int

• The CAS value simply represents the current state of the Document, it's like a version number

• You can use this CAS for "Optimistic Concurrency"- value, flags, cas = get("mykey", :extended => true)- This will only succeed if the CAS matches

• replace("mykey", newvalue, :cas => cas)- If another process had changed the "mykey" document, a

new CAS will have been generated, and that replace operation will fail

Page 42: Couchbase 102 - SDK Operations

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Optimistic Concurrency with CAS

Application Server 2

Application Server 1

Page 43: Couchbase 102 - SDK Operations

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Optimistic Concurrency with CAS

Application Server 2

Application Server 1

CAS: 111111111

CAS: 111111111

Page 44: Couchbase 102 - SDK Operations

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Optimistic Concurrency with CAS

Application Server 2

Application Server 1

CAS: 111111111

CAS: 111111111

Page 45: Couchbase 102 - SDK Operations

CAS: 222222222Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Optimistic Concurrency with CAS

Application Server 2

Application Server 1

CAS: 111111111

Page 46: Couchbase 102 - SDK Operations

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Optimistic Concurrency with CAS

Application Server 2

Application Server 1

CAS: 222222222

CAS: 111111111

Page 47: Couchbase 102 - SDK Operations

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Optimistic Concurrency with CAS

Application Server 2

Application Server 1

CAS: 222222222

CAS: 111111111

set/add/replace with CAS

CAS  MISMATCH

Page 48: Couchbase 102 - SDK Operations

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Optimistic Concurrency with CAS

Application Server 2

Application Server 1

CAS: 222222222

CAS: 111111111

set/add/replace with CAS

CAS  MISMATCH

Page 49: Couchbase 102 - SDK Operations

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Optimistic Concurrency with CAS

Application Server 2

Application Server 1

CAS: 222222222

CAS: 111111111

Page 50: Couchbase 102 - SDK Operations

CAS: 222222222

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Optimistic Concurrency with CAS

Application Server 2

Application Server 1

CAS: 222222222

set/add/replace with CAS

RETRY  MUTATION  WITH  UPDATED  CAS

NEW  CAS  GENERATED

Page 51: Couchbase 102 - SDK Operations

CAS: 222222222

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Optimistic Concurrency with CAS

Application Server 2

Application Server 1

CAS: 222222222

set/add/replace with CAS

RETRY  MUTATION  WITH  UPDATED  CAS

NEW  CAS  GENERATED

Page 52: Couchbase 102 - SDK Operations

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Optimistic Concurrency with CAS

Application Server 2

Application Server 1

CAS: 222222222

CAS: 222222222

Page 53: Couchbase 102 - SDK Operations

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Optimistic Concurrency with CAS

Application Server 2

Application Server 1

CAS: 333333333

CAS: 333333333

Page 54: Couchbase 102 - SDK Operations

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Pessimistic Concurrency with Lock

Application Server 2

Application Server 1

Page 55: Couchbase 102 - SDK Operations

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Pessimistic Concurrency with Lock

Application Server 2

Application Server 1

Page 56: Couchbase 102 - SDK Operations

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Pessimistic Concurrency with Lock

Application Server 2

Application Server 1

Page 57: Couchbase 102 - SDK Operations

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Pessimistic Concurrency with Lock

Application Server 2

Application Server 1

Page 58: Couchbase 102 - SDK Operations

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Pessimistic Concurrency with Lock

Application Server 2

Application Server 1

set/add/replace with CAS

UNLOCKS  AFTER  COMPLETE

NEW  CAS  GENERATED

Page 59: Couchbase 102 - SDK Operations
Page 60: Couchbase 102 - SDK Operations

EXPIRATIONS

Page 61: Couchbase 102 - SDK Operations

Storage Operations with Expirations

• CMS Framework Cache can be configured to use Couchbase- In most frameworks this is simple, as they typically already

have memcached support!

• Create/Update the Value and Expiration - [Ruby] set/add/replace("mykey", value, :ttl => 30)- [Java] set/add/replace("mykey", 30, value)!

• Update the expiration only- [Ruby] touch("mykey", :ttl => 30)- [Java] touch("mykey", 30)!

Page 62: Couchbase 102 - SDK Operations

Expiration

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

Page 63: Couchbase 102 - SDK Operations

Expiration

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

set/add/replace with TTLget

EXPIRED

DELETED  ON  COMPACTION

Page 64: Couchbase 102 - SDK Operations

Expiration

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

Page 65: Couchbase 102 - SDK Operations

Expiration

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

set/add/replace with TTL

UPDATES  TTL

touch

Page 66: Couchbase 102 - SDK Operations
Page 67: Couchbase 102 - SDK Operations

DURABILITY

Page 68: Couchbase 102 - SDK Operations

Using Storage with Observe

• Callback when it has been written to disk on active partition• Callback when it has been written to a replica(s)• Callback when it has been written to replica(s) disk• Observe Persisted to Disk and Replicated

• [Ruby] set/add/replace("mykey", value, :observe => {:persisted => 1, :replicated => 1})

• [Java] set/add/replace("mykey", 0, value, PersistTo.MASTER, ReplicateTo.ONE)

• Observe Replicated Only• [Ruby] set/add/replace("mykey", value,

:observe => {:replicated => 1})• [Java] set/add/replace("mykey", 0, value, ReplicateTo.ONE)

Page 69: Couchbase 102 - SDK Operations

Durability Persist-To

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

Page 70: Couchbase 102 - SDK Operations

Durability Persist-To

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

set/add/replace

Page 71: Couchbase 102 - SDK Operations

Durability Persist-To

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

set/add/replace

Callback

Page 72: Couchbase 102 - SDK Operations

Durability Replicate-To

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

Page 73: Couchbase 102 - SDK Operations

Durability Replicate-To

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

set/add/replace

Page 74: Couchbase 102 - SDK Operations

Durability Replicate-To

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

set/add/replace

Callback

Page 75: Couchbase 102 - SDK Operations

Durability Replicate-To

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

set/add/replace

Callback

Page 76: Couchbase 102 - SDK Operations

Durability Replicate-To

Couchbase Server

EP EngineRAM Cache

Disk Write Queue

Replication Queue

Application Server

Replica Couchbase Cluster Machine

set/add/replace

Callback

Page 77: Couchbase 102 - SDK Operations
Page 78: Couchbase 102 - SDK Operations

DEMO

Page 79: Couchbase 102 - SDK Operations
Page 80: Couchbase 102 - SDK Operations

Q  &  A

Page 81: Couchbase 102 - SDK Operations

Resources

Main  Resource  Portal  www.couchbase.com/communiFes  !Code  Samples  Going  through  OperaUons  www.github.com/couchbaselabs/DeveloperDay  !Couchbase  Q  &  A  www.couchbase.com/communiFes/q-­‐and-­‐a  

My  Email:  [email protected]  My  TwiZer:  @scalabl3

Next Webinar: Couchbase 103 - Modeling


Top Related