couchbase 102: sdk operations for 3.0

Post on 07-Jul-2015

764 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

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

Couchbase 102Todd Greenstein | Engineering, Couchbase

Setup an SDK

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

Connecting to Couchbase

©2014 Couchbase, Inc.

Connect to Couchbase

©2014 Couchbase, Inc. 5

Step 1

Step 2

Step 3

Client Connection Steps

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:

Client Operations

All SDK’s

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.

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.

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.

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

Let’s Build an Application

©2014 Couchbase, Inc.

Node.js

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!

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'

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

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

Node.js – App.js

©2014 Couchbase, Inc. 17

Node.js – routes/route.js

©2014 Couchbase, Inc. 18

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":"dave@couchbase.com"}'

## 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

Build a View Programmatically

©2014 Couchbase, Inc. 20

Enable N1QL

©2014 Couchbase, Inc. 21

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.

top related