couchbase 102: sdk operations for 3.0

22
Couchbase 102 Todd Greenstein | Engineering, Couchbase

Upload: couchbase

Post on 07-Jul-2015

764 views

Category:

Software


0 download

DESCRIPTION

In Couchbase 102 for 3.0, the incredible power and flexibility of the Couchbase SDK's are examined and the fundamentals of distributed applications and use cases built with Couchbase are explained.

TRANSCRIPT

Page 1: Couchbase 102: SDK Operations for 3.0

Couchbase 102Todd Greenstein | Engineering, Couchbase

Page 2: Couchbase 102: SDK Operations for 3.0

Setup an SDK

Page 3: Couchbase 102: SDK Operations for 3.0

Supported SDK’s

©2014 Couchbase, Inc. 3

Installation Location: http://docs.couchbase.com/developer

• All SDK’s are open source.

• All SDK’s utilize advanced package management

• All SDK’s contain extensive documentation and

excellent tutorials

Page 4: Couchbase 102: SDK Operations for 3.0

Connecting to Couchbase

©2014 Couchbase, Inc.

Page 5: Couchbase 102: SDK Operations for 3.0

Connect to Couchbase

©2014 Couchbase, Inc. 5

Step 1

Step 2

Step 3

Client Connection Steps

Page 6: Couchbase 102: SDK Operations for 3.0

Service List

©2014 Couchbase, Inc. 6

• Dynamic Distributed Services

• Dynamic Configuration Updates – No

additional work from the developer

• Fault Tolerant/Durable Connectivity

Client Connectivity Characteristics:

Page 7: Couchbase 102: SDK Operations for 3.0

Client Operations

All SDK’s

Page 8: Couchbase 102: SDK Operations for 3.0

Unified API - DML Methods, Add/Remove/Update

©2014 Couchbase, Inc. 8

Adding and Removing Information (JSON Documents, K/V

Binary)• insert-Insert a document or binary key/value. Fails if the item exists.

• upsert-Stores a document or binary key/value to the bucket, or updates if a document exists.

• replace-Replaces a document or binary key/value in a bucket. Fails if the item doesn’t exist.

• remove-Deletes an item from the bucket. Fails if the item doesn’t exist

• append/prepend-Appends or prepends in place the value of a binary k/v item. Does NOT

work with documents

• touch-Updates the ttl of a documet.

• getAndTouch-Retrieves a document or binary key/value and updates the expiry of the item

at the same time.

• counter-Increments or decrements a key's numeric value.

Page 9: Couchbase 102: SDK Operations for 3.0

Unified API - DML Methods, Retrieval

©2014 Couchbase, Inc. 9

Retrieving Information (JSON Documents, K/V Binary)• get-Retrieves a document or binary key/value.

• getAndLock-Lock the document or binary key/value on the server and retrieve it. When an

document is locked, its CAS changes and subsequent operations on the document (without

providing the current CAS) will fail until the lock is no longer held.

• getReplica-Get a document binary key/value from a replica server in your cluster.

• unlock-Unlock a previously locked document or binary key/value on within a bucket.

Page 10: Couchbase 102: SDK Operations for 3.0

Unified API – DML, CAS Example

©2014 Couchbase, Inc.

1. Two Clients retrieve the same document "XYZ"

2. Client A retrieves it first.

3. Client B then retrieves XYZ. Both clients will have the same CAS value for document XYZ

4. Client B tries to perform an update to document XYZ. The update succeeds as the CAS value was unchanged from when Client B initially retrieved the document. Once the update succeeds, the CAS value for XYZ changes.

5. Client A then tries to perform an update on XYZ immediately after Client B. The update will fail as Client A's CAS value is out of date. When Client B updated XYZ, the CAS value changed.

Page 11: Couchbase 102: SDK Operations for 3.0

Views

©2014 Couchbase, Inc. 11

• All SDK’s support programmatically defining views

• Views are materialized secondary indexes

• Stored on Disk

• Each Node Responsible for items from its vBuckets

• Updated from memory, change detection happens through DCP

Protocol (this is new for 3.0!). VERY small consistency window.

• Built using Map Reduce, in Javascript:

function (doc, meta) {

emit(meta.id, null);

}

KEY VAL

Page 12: Couchbase 102: SDK Operations for 3.0

Let’s Build an Application

©2014 Couchbase, Inc.

Node.js

Page 13: Couchbase 102: SDK Operations for 3.0

Environment

©2014 Couchbase, Inc. 13

Demo Application:

• 4 Node Demo Environment

• Use Vagrant to provision: https://github.com/couchbaselabs/vagrants

• Setup a cluster, using all 3 of the 4 virtual machines.

• Setup a “default” bucket to use for our application

• Do it with a simple build script

• Node.js

• Install node.js on the 4th virtual machine in our environment.

• Install Couchbase Client (defined in this case in the “package.json”) file

• Build a C/R/U/D API.

• Test it!

Page 14: Couchbase 102: SDK Operations for 3.0

Build a 3 Node VM Cluster From a Script

©2014 Couchbase, Inc. 14

## Initialize Node

/opt/couchbase/bin/couchbase-cli node-init -c 192.168.67.101:8091 --node-init-data-

path=/opt/couchbase/var/lib/couchbase/data -u Administrator -p password

## Initialize Cluster

/opt/couchbase/bin/couchbase-cli cluster-init -c 192.168.67.101:8091 -u Administrator -p password --cluster-init-

port=8091 --cluster-init-ramsize=400

## Add Nodes

curl -u Administrator:password\

192.168.67.101:8091/controller/addNode \

-d "hostname=192.168.67.102&user=Administrator&password=password”

curl -u Administrator:password\

192.168.67.101:8091/controller/addNode \

-d "hostname=192.168.67.103&user=Administrator&password=password"

## Build a Bucket

curl -v -u Administrator:password -X POST 'http://192.168.67.101:8091/pools/default/buckets' -d name=default -d

ramQuotaMB=100 -d authType=none -d replicaNumber=1 -d proxyPort=11216

## Rebalance

curl -v -u Administrator:password -X POST 'http://192.168.67.101:8091/controller/rebalance' -d

'ejectedNodes=&knownNodes=ns_1%40192.168.67.101%2Cns_1%40192.168.67.102%2Cns_1%40192.168.67.103'

Page 15: Couchbase 102: SDK Operations for 3.0

Setup Node.js on a VM

©2014 Couchbase, Inc. 15

## Fix DNS

echo "nameserver 8.8.4.4" >> /etc/resolv.conf

echo "nameserver 8.8.8.8" >> /etc/resolv.conf

echo "nameserver 127.0.0.1" >> /etc/resolv.conf

## Install Node.js and CB Client Prereq

yum install gcc-c++ -y

curl -sL https://rpm.nodesource.com/setup | bash -

yum install -y nodejs

Page 16: Couchbase 102: SDK Operations for 3.0

Node.js - Package.json

©2014 Couchbase, Inc. 16

• package.json contains a list of all

dependencies needed by the

application

• After defining, simply run:

npm install

Page 17: Couchbase 102: SDK Operations for 3.0

Node.js – App.js

©2014 Couchbase, Inc. 17

Page 18: Couchbase 102: SDK Operations for 3.0

Node.js – routes/route.js

©2014 Couchbase, Inc. 18

Page 19: Couchbase 102: SDK Operations for 3.0

Test the API

©2014 Couchbase, Inc. 19

## Create

curl -X POST 192.168.67.104:3000/api/create/dave

-H "Content-Type: application/json" -d

'{"username":"dave","email":"[email protected]"}'

## Read Your Own Write

curl 192.168.67.104:3000/api/read/dave

## Delete

curl -X DELETE 192.168.67.104:3000/api/delete/dave

Page 20: Couchbase 102: SDK Operations for 3.0

Build a View Programmatically

©2014 Couchbase, Inc. 20

Page 21: Couchbase 102: SDK Operations for 3.0

Enable N1QL

©2014 Couchbase, Inc. 21

Page 22: Couchbase 102: SDK Operations for 3.0

Further Information

©2014 Couchbase, Inc. 22

Couchbase Node.js Client API Reference: http://docs.couchbase.com/sdk-

api/couchbase-node-client-2.0.0/

N1QL Documentation:

• http://docs.couchbase.com/developer/n1ql-dp3/n1ql-intro.html

Next Session:

• Couchbase 103 Modeling on 11/12/2014 - Learn the fundamentals of creating

data models with Couchbase including modeling, JSON strategies, common key

patterns. Explore modeling differences between NOSQL and RDBMS systems.