couchbase tlv dev track 03- document your world

31

Upload: couchbase

Post on 12-Apr-2017

929 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Couchbase TLV Dev track 03- document your world
Page 2: Couchbase TLV Dev track 03- document your world

Developing with Couchbase: Document Your World

Michael NitschingerEngineer, Developer Solutions

Page 3: Couchbase TLV Dev track 03- document your world

What to Expect:

• JSON Basics & Documents

• Mind-set Changes

• Building an application around JSON

• Document Structuring & Modeling

• Views and Indexes within Couchbase

Page 4: Couchbase TLV Dev track 03- document your world

JSON Basics – what is JSON?

Java Script Object Notation

• Created by Douglas Crockford

• Text Based Format

• Subset of JavaScript

• Human Readable

Page 5: Couchbase TLV Dev track 03- document your world

JSON Basics – Why JSON?

• Compact

• Understandable

• Data structures map nicely

• Widely adopted

Page 6: Couchbase TLV Dev track 03- document your world

Supported JSON Types:

String:

Numbers: – (Int. & Floating Point)

"A String"

Boolean:

{“value” : false}

Object:

{ ”name" : “Michael N.” “twitter" : “@daschl", ”age" : 25, "title" : ”Engineer"}

22 & 55.2

Page 7: Couchbase TLV Dev track 03- document your world

Supported JSON Types - Lists:

Array:

["one", "two", "three"]foos : [ { ”bar1":"value1", ”bar2":"value2" }, { ”bar3":"value3", ”bar4":"value4" }]

List of Objects:

Complex, Nested Objects:

{ tweet, tweet… }

Page 8: Couchbase TLV Dev track 03- document your world

JSON Documents within Couchbase

• Documents

• have a unique ID

• can store JSON (also binary) as the value

• have meta information attached

• If it’s JSON, the document can be used in Views (secondary

indexing)

Page 9: Couchbase TLV Dev track 03- document your world

meta{“id”: “[email protected]”,“rev”: “1-0002bce0000000000”,“flags”: 0,“expiration”: 0,“type”: “json”}

document{“uid”: 1234,“firstname”: “Michael”,“lastname”: “Nitschinger”,“age”: 25,“favorite_colors”: [“blue”, “green”],“email”: “[email protected]”}

Meta Information Including Key (ID)

All Keys Unique and Kept in RAM at all times.

Document Value

Most Recent In Ram And Persisted To Disk

JSON Document Structure

Page 10: Couchbase TLV Dev track 03- document your world

User Objectstring uid

string firstname

string lastname

int age

array favorite_colors

string email

u::[email protected]{“uid”: 1234,“firstname”: “Michael”,“lastname”: “Nitschinger”,“age”: 17,“favorite_colors”: [“blue”, “green”],“email”: “[email protected]”}

User Objectstring uid

string firstname

string lastname

int age

array favorite_colors

string email

set()

get()

Objects Serialized to JSON and Back

u::[email protected]{“uid”: 1234,“firstname”: “Michael”,“lastname”: “Nitschinger”,“age”: 25,“favorite_colors”: [“blue”, “green”],“email”: “[email protected]”}

Page 11: Couchbase TLV Dev track 03- document your world

The Mind-Set Change

Page 12: Couchbase TLV Dev track 03- document your world

• Information spread across (multiple) Tables,

• Rigid and inflexible schema

• Records need to look the same

• Complex JOINS, WHERE Clauses and ORDER BY Clauses

The Move from Relational Modeling

Our ‘Recipe’ table uses “JOINS” to aggregate info from other Tables.

Page 13: Couchbase TLV Dev track 03- document your world

The Move to NoSQL

• Gets and Sets are quick, do not fear them!

• Schema is dictated by the application, not the database.

• No ALTER_TABLE needed!• Each Document can look different.

Page 14: Couchbase TLV Dev track 03- document your world

Modeling an Application…The JSON way

Page 15: Couchbase TLV Dev track 03- document your world

Application in which people can

• vote on other User’s Videos

• see a Global Ranking of the Best and Worst Videos!

Rate My Vine…

Top Rated Vines

Cooking w/ Hugh Fearnley-Whittingstall

I love doing Housework

What happened to Amanda Bynes

Random Access Memories

I don’t even know

Twerking gone wrong

Too cold to Dance

How To Scare Your Friends

176

143

120

112

107

98

74

37

Page 16: Couchbase TLV Dev track 03- document your world

• Open Source Sample App for Couchbase

• Built on Ruby, Rails & Couchbase­ Couchbase-Model Ruby Gem for Active-Record style (easy) data

modeling­ Puma as web server for concurrent connections

Technology Used:

Page 17: Couchbase TLV Dev track 03- document your world

•Users must Auth with Twitter before Submitting Vines•We simply register their Name, Twitter Username & Avatar upon T-auth

User.rb

Page 18: Couchbase TLV Dev track 03- document your world

•Standard JSON structure with simple String fields

•This JSON is editable within the Couchbase Console

How that looks as JSON in Couchbase:

Key created by a hash of Twitter UID

Explicit ‘type’ of Document

Page 19: Couchbase TLV Dev track 03- document your world

•Vines need a Name, A Video URL, a User and a Score

Vine.rb

Page 20: Couchbase TLV Dev track 03- document your world

• Product Manager has informed us that we need to add a new field for Facebook Sharing into our Vine Videos

• In a relational world, we would have to do schema migrations

• In the Couchbase world, it’s much easier!

The Joys of a Flexible Schema!

Page 21: Couchbase TLV Dev track 03- document your world

•User_ID included so we know who each Vine belongs to

•Score is inside each Vine document. This brings it’s own challenges, but Couchbase solves them!

Again, the JSON within Couchbase

Random Hash generated Key

User_ID reference

Page 22: Couchbase TLV Dev track 03- document your world

•We have chosen to have the Score inside each Vine doc.

•We need to be able to deal with concurrent score updates.

Optimistic Concurrency

{ “score" : 174}

UPDATE UPDATE

Page 23: Couchbase TLV Dev track 03- document your world

• To handle the Concurrent updates, we can utilise Couchbase’s built in CAS concurrency controls.

• We simply write a new Update method in our application controller to use the CAS value on update.

CAS – Compare and Swap

Page 24: Couchbase TLV Dev track 03- document your world

•Just as in SQL, our JSON Documents also have various types of ‘Relationship’.

•For example, a User can own many Videos as a 1 to many relationship.

Document Relationships

video:1{ type: “vine”, title: “My Epic Video”, owner: “ingenthr”}

user:ingenthr{ type: “user”, name: “Matt Ingenthron”, id: “ingenthr”}

video:2{ type: “vine”, title: “I NEED A HORSE!”, owner: “ingenthr”}

Page 25: Couchbase TLV Dev track 03- document your world

• Product Manager has informed us we need to add a Comment mechanism to our Vine Videos.

• We need to decide the best way to approach this in JSON document design.

Single vs. Multiple Documents

{

}

Single Multiple

vs.

Document

Comment

Comment

Comment

Page 26: Couchbase TLV Dev track 03- document your world

• Comments are nested within their respective Vine documents.

• Great when we know we have a finite amount of Results.

Single vs. Multiple - Single

{ "type": "vine", "user_id": "145237874", "title": "I NEED A HORSE", "vine_url": "https://vine.co/v/b2jjzY0Wqg5", "video_url": "https://mtc.cdn.vine.co……, "score": 247, "comments": [ {"format": "markdown", "body": "I LOVE this video!"}, {"format": "markdown", "body": "BEST video I have ever seen!"}, ]}

7b18b847292338bc29

Page 27: Couchbase TLV Dev track 03- document your world

•Comments are split from the parent document.

•Comments use referential ID’s, incremented by 1

Single vs. Multiple - Multiple

{ "type": "vine", "user_id": "145237874", "title": "I NEED A HORSE", "score": 247,}

7b18b847292338bc29 { "format": "markdown", "body": "I LOVE this video!”}

7b18b847292338bc29::1

{ "format": "markdown", "body": “BEST video ever!”}

7b18b847292338bc29::2

Page 28: Couchbase TLV Dev track 03- document your world

•Couchbase has no built in mechanism for Versioning.

•There are many ways to approach document Versioning.­Copy the versions of the document into new documents,­Copy the versions of the document into a list of nested documents,­Store the list of mutated / modified attributes:

• In nested Element,

• In separate Documents.

•In this case, we’re going to look at the simplest way…

Versioning our Documents:

Page 29: Couchbase TLV Dev track 03- document your world

•Get the current version of the document,

•Increment the version number,

•Create the version with the new key "mykey::v1”,

•Save the document in it’s current version.

Versioning our Documents:

Current Version:

Version 1:

Version 2:

mykey

mykey::v1

mykey::v2

Page 30: Couchbase TLV Dev track 03- document your world

Questions?

Page 31: Couchbase TLV Dev track 03- document your world