how-to nosql webinar - couchbase 102 - sdk operations

41
How-to NoSQL Couchbase 102 - SDK Operations Matt Ingenthron – Director, Developer Solutions Don Pinto – Product Manager, Developer Products

Upload: couchbase

Post on 14-Jul-2015

716 views

Category:

Technology


3 download

TRANSCRIPT

How-to NoSQLCouchbase 102 - SDK Operations

Matt Ingenthron – Director, Developer Solutions

Don Pinto – Product Manager, Developer Products

Introduction

Agenda

• Couchbase Capabilities and Use Cases

• Demo!

• Couchbase Operations

• Questions

• Next Steps

Couchbase Server Has

Easy Scalability

Consistent High Performance

FlexibleData Model

Always On 24x7x365

Grow cluster without application changes, without downtime when needed

Always awesome experience for your application users

The sun never sets on the Internet, your application needs the database to always serve data

Keep developers productive and allow fast and easy addition of new features

JSONJSON

JSON

JSONJSON

PERFORMANCE

Web Server

Application Server

Database

In the beginning…

Terminate connections, handle static content requests.

Execute domain specific application logic, frequently in the context of a user session.

Be the persistent store behind the data model defined at the application.

Web Server(s)

Application Server(s)

Database

Building Next Generation

Web/App Server(s)

Database

… And the Next

Web/App Server(s)

Database

… And the Next

Caching

Web/App Server(s)

Database

… And the Next

Caching

WritesReads Reads

Couchbase Cluster

Node Node Node Node

Session StoreUse Case

Session Store

• Extremely fast access to session data using unique session ID

• Easy scalability to handle fast growing number of users and user-generated data

• Always-on functionality for global user base

Application characteristic

Use Case

• Session values or Cookies (stored as key-value pairs)

• Examples include: items in a shopping cart, flights selected, search results, etc.

Data stored in Couchbase?

Globally Distributed User Profile StoreUse Case

http://www.ProfileStore.com

e enim nec felis rhoncus, ac volutpat magna blandit. Nunc facilisis turpis eget dolor mollis, id tincidunt dui mattis. Nunc sodales elementum turpis, vel interdum ante conguequis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aliquam erat volutpat. Nullam suscipit diam nec tortor pharetra, vitae adipiscing dolor pretium. Integer ac porta tortor. Vestibulum imperdiet quam laoreet nisl scelerisque, a tempus tortor tincidunt. Mauris suscipit dui ac urna dignissim, vitae aliquet velit convallis. Phasellus lobortis felis eu magna vulputate dapibus. Ut ornare ut quam a vulputatullam et dui odio. Nulla pharetra, velit ac convallis semper, dolor turpis porta nunc, in egestas mauris leo a nisi. Pellentesque fringilla sagittis magna vitae imperdiet. Mauris ac leo ut tellus aliquet interdum. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nunc cursus odio sit amet elit mollis, et sollicitudin lacus accumsan. Nulla facilisi. Fusce et vehicula sem. Curabitur interdum vestibulum nulla id accumsan. Integer ut tortor in ligula semper vehicula. Vestibulum ut nibh ultrices, venenatis metus at, adipiscingipsum. Donec quis consequat lectus.Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Donec a diam tempus, aliquet ipsum eu, vestibulum sapien. Donec eleifend lectus sit amet luctus facilisis. Morbi porttitor, orci sit amet placerat tempus, nisi justo dictum augue, ac dignissim elit enim eget dolor. Praesent pulvinar ipsum arcu, eu posuere erosluctus nec. Vestibulum odio eros, ultrices non metus sit amet, tristique malesuada augue. Pellentesque lacinia dolor nec diam eleifend mollis. Vestibulum sit amet ultrices diam. Aliquam lacinia accumsan eros id hendrerit. Cras placerat laoreet urna scelerisque rutrum. Duis ornare mi ac augue varius, sit amet accumsan leo lacinia. Vivamus nec egestasneque. Quisque interdum enim molestie urn.

turpis eget dolor mollis, id tincidunt dui mattis. Nuncsodales elementum turpis, vel interdum ante congue quis. Pellentesque habitant morbi tristique senectus et netus et malesuada

Welcome back Laura!You have 3 items in your shopping cart waiting for you.

LOGIN

ID:

PASS:

Globally Distributed User Profile Store

• Extremely fast access to individual profiles

• Always online system as multiple applications access user profiles

• Flexibility to add and update user attributes

• Easy scalability to handle fast growing number of users

• User profile with unique ID

• User setting / preferences

• User’s network

• User application state

Data stored in Couchbase? Application characteristic

Use Case

Laura930

********

Data Aggregation

• Flexibility to store any kind of content

• Flexibility to handle schema changes

• Full-text Search across data set

• High speed data ingestion

• Scales horizontally as more content gets added to the system

• Social media feeds: Twitter, Facebook, LinkedIn

• Blogs, news, press articles

• Data service feeds: Hoovers, Reuters

• Data form other systems

Data stored in Couchbase? Application characteristic

Use Case

in

Ft

NEWS

Blog

Questions?

Demo andCore Operations

Beer Trail Application

Brewery Beers

1 .. N

Brewery Document

Type is “brewery”

Where is it on the map

Name of the brewery

ID of the brewery

Beer Document

Type is “beer”

Name of the beerAlcohol by volume

Brewery ID

ID of the beer

Modeling User Trails

• Can represent rich data in JSON

including arrays

• Can modify schema on the fly,

and Documents of a specific

"type" can vary in schema

Couchbase

{“type": "User","name": “Don Pinto","email": “[email protected]",“trail_history“: [ {“brewery_id": "olde_peninsula_brewpub_and_restaurant",

“beers_tasted": ["olde_peninsula_brewpub_and_restaurant-1_2_3_ale“,"olde_peninsula_brewpub_and_restaurant-4_5_6_ale“]}

]}

Trail history can be modeled

in the user document or if it

grows too large, in a

separate trail document.

Working with application objects

Look up a beer or brewery

Lookup by name

Lookup by brewery id

Lookups are best accomplished using key-value access

Listing all breweries and beers

Emits brewery names

Emits beer names

JAVA CODE USING VIEWS IN COUCHBASE

Modifying a document beer, brewery or user

• Use GSON to serialize Java POJO objects to JSON

- Java classes must implement serializable

Deleting a brewery or beer

• Delete is an async operation

- Returns an OperationFuture that can be blocked through the get method

• If the document does not exist

- The OperationFuture returns false

• If the operation succeeds

- Couchbase Server lazily deletes that document from disk

Couchbase Basic Operations

• get (key)- Retrieve a document

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

• add (key, value)- Store a document, error/exception if exists

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

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

executing this operation

Atomic counters

Atomic Counters are a special structure in Couchbase, they are executed in order and are Positive Integer Values

•set (key, value)- Use set to initialize the counter

• cb.set(“my_counter”, 1)

•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

Making the app more interactive

Adding ratings and comments

link to comments

• Set at the id “f1e62”

"type": "comment","about_id": "beer_Enlightened_Black_Ale","user_id": 525,"text": "tastes like college!","updated": "2010-07-22 20:00:20"

client.set(“f1e62”,{

});

create a new document

{"id": "f1e62"

}

Do it: Save the comment document

But, multiple users can update this at the same timeHow to guarantee atomic updates with concurrent writes?

How to: Add a review and rating

• Use a compare-and-swap (CAS) update

- Don’t want to accidentally overwrite another users review comments and score that is being saved at the same time as ours

Giving it a 5“Nice

flavor”

2 from me“Its bitter”

Time T1

Time T2

User 1 User 2

Get CAS Version(User 1)

Get CAS Version(User 2)

JSONBeer

Document

CAS = 0

JSONBeer

Document

CAS = 0 1

Time T3

JSONBeer

Document

CAS = 1 2

Time T4

CAS succeeds for user 1Fails for user 2

User 2 retriesCAS succeeds for user 2

CAS Explained

Object Graph With Shared Interactive Updates

33

• Challenge: higher level data structures

• Objects shared across multiple users

• Mixed object sets (updating some private and some shared objects)

34

Get With Lock (GETL)

• Locks have a short TTL• Locks released with CAS operations• Useful when working with object graphs

Storing Items with Durability Requirements

• Un-blocked when it has been written to disk in active location

• Un-blocked when it has been written to a replica(s)

• Un-blocked when it has been written to replica(s) disk

• Durability Requirements when mutating data- set/add/replace("mykey", 0, value,

PersistTo.MASTER, ReplicateTo.ONE)

- maximum paranoia: set/add/replace("mykey", 0, value, PersistTo.FOUR, ReplicateTo.THREE)

• Durability Requirements for replication only- set/add/replace("mykey", 0, value,

ReplicateTo.ONE)

Blocking Wait

Managed Cache Persistence Layer

Durability PersistTo

Blocking Wait

Managed Cache Persistence Layer

Durability ReplicateTo

Questions?

Resources• Getting the SDKs and checking out the tutorial for each SDK

- http://www.couchbase.com/communities/

• Java Tutorial App Shown Earlier

- http://docs.couchbase.com/couchbase-sdk-java-1.4/#tutorial

Official SDKs in

PythonRuby

Where to go Next

• Attend the 103 webinar- June 14 – http://info.couchbase.com/HowTo-NoSQL-Webinar-

Couchbase-103-June14.html

• Sign up for training- http://training.couchbase.com

Developing – June 17 – New York City

Administration – Jun 24 – Chicago

Developing – June 24 – London

• Join us at Couchbase Live [NYC]June 3, 2014

Thanks!

Matt Ingenthron –[email protected] – @ingenthr

Don Pinto – [email protected] – @NoSQLDon