developing ii advanced document design

26
Advanced Document Design Benjamin Young [email protected] m @bigbluehat

Upload: couchbase

Post on 09-May-2015

513 views

Category:

Business


3 download

TRANSCRIPT

Page 1: Developing ii   advanced document design

Advanced Document Design

Benjamin [email protected]

m@bigbluehat

Page 2: Developing ii   advanced document design

● "User Advocate"● User Experience Engineers● + Tech Evangelist

● Entrepreneur for 7+ years● built BlueInk, a CMS

● Couch.io/CouchOne in 2010● did User Advocate stuff

● Co-Organizer of REST Fest (www.restfest.org)● Couchbase in 2011+

● build UIs, make sample apps, give talks

Page 3: Developing ii   advanced document design

● Document Databases● Schema-less Data Modeling● Inherent Constraints

● Document Design Basics● living in the world of Documents

● Getting at your Documents (with Demos!)● Document Editing Demo● Creating Views and Indexes● Querying those Indexes● Reducing the output to values

● Questions

Agenda

Page 4: Developing ii   advanced document design

Document Databases

DocumentDatabases

Document DesignBasics

CreatingViews

Demonstrationof ViewEditing

Summary

Page 5: Developing ii   advanced document design

● ad hoc data store● No ALTER TABLE● Add new "fields" to any object any time● improves development speed

● Structure matters at query time, not write time

● JSON data is interoperable● 2.0 Features

● dynamic queries with views● “Web-native” format● broad language support: json.org

Schema-less Data Modeling

Page 6: Developing ii   advanced document design

● Document ID is the only (DB-side) way to enforce uniqueness

● JSON doc key name restrictions● top-level keys may not begin with either “_” or “$”

● Small “gotchas”● watch out for numbers as strings (Reduce will care)● data format consistency

● unix time stamps● some other JS parse-able, IETF codified format

Inherent Constraints

Page 7: Developing ii   advanced document design

Document Design Basics

DocumentDatabases

Document DesignBasics

CreatingViews

Demonstrationof ViewEditing

Summary

Page 8: Developing ii   advanced document design

{   "_id": "beer_1554_Enlightened_Black_Ale",   "brewery": "New Belgium Brewing",   "name": "1554 Enlightened Black Ale",   "abv": "5.5",   "description": "Born of a flood...",   "category": "Belgian and French Ale",   "style": "Other Belgian-Style Ales",   "updated": "2010-07-22 20:00:20"}

Beer Document

type name in ID

used to reference the

brewery doc in MapReduce

“vintage” date from an SQL dump >_<

Page 9: Developing ii   advanced document design

{"_id": "brewery_New_Belgium_Brewing",   "name": "New Belgium Brewing",   "address": ["500 Linden Street"],   "city": "Fort Collins",   "state": "Colorado",   "code": "80524",   "website": "http://www.newbelgium.com/",   "description": "We'll set the scene...”,   "geo": {       "loc": ["-105.07", "40.5929"],       "accuracy": "RANGE_INTERPOLATED"   },   "updated": "2010-07-22 20:00:20"}

Brewery Documenttype name in

ID

JSON array

GeoCouch FTW!

Page 10: Developing ii   advanced document design

Creating Views

DocumentDatabases

Document DesignBasics

CreatingViews

Demonstrationof ViewEditing

Summary

Couchbase Server 2.0:Querying and Aggregation

with Views

Page 11: Developing ii   advanced document design

Document Editing Demo

Page 12: Developing ii   advanced document design

Couchbase Server 2.0:Querying and Aggregation

with Views

Page 13: Developing ii   advanced document design

● Building indexes, not querying● Eventually Consistent indexed view● “Secondary Index”

● Output keys are used to reference ranges of the index

● Document IDs can also be used for referencing● “Querying” is done via the Query String

● the SDKs wrap this up for you

● Values can be anything

● Reduce is an additional layer

MapReduce Basics

Page 14: Developing ii   advanced document design

function(doc) { if (doc.geo) { emit([humanize(doc._id)], 1); } else if (doc.brewery && typeof doc.brewery === 'string') { emit([doc.brewery, humanize(doc._id)], 1); }

// handy function for building IDs from names function humanize(name) { return name.replace(/^(brewery_|beer_)/, '') .replace(/_/g, ' '); }}

Map function to build the Index of Breweries and their Beers

_design/beers/_view/brewery_beers

Page 15: Developing ii   advanced document design

_count

Built-in Reduce function for the Brewery and their Beers

_design/beers/_view/brewery_beers?reduce=true

• Built-in reducers are the fastest option for Index reduction

• _count• _sum• _stats (min, max, count, avg)

Page 16: Developing ii   advanced document design

View Editing

DocumentDatabases

Document DesignBasics

CreatingViews

Demonstrationof ViewEditing

Summary

Page 17: Developing ii   advanced document design

Views Editing Demo

Page 18: Developing ii   advanced document design

{"rows": [ {"id": "brewery_Abhi_Brewery", "key": ["Abhi Brewery"], "value": 1 }, {"id": "beer_Abhi_beer", "key": ["Abhi Brewery","Abhi beer"], "value": 1 }]}

Results from the brewery_beers index

_design/beers/_view/brewery_beers

Page 19: Developing ii   advanced document design

Wait...where's the data?

Meet ?include_docs=true

Page 20: Developing ii   advanced document design

{"rows": [ {"id": "brewery_Abhi_Brewery", "key": ["Abhi Brewery"], "value": 1, "doc": { "_id": "brewery_Abhi_Brewery", "name": "Abhi Brewery", .... } }, {"id": "beer_Abhi_beer", "key": ["Abhi Brewery","Abhi beer"], "value": 1, "doc": { "_id": "beer_Abhi_beer", "name": "Abhi beer", ....} }]}

Results from the brewery_beers index

_design/beers/_view/brewery_beers?include_docs=true

Page 21: Developing ii   advanced document design

{"rows": [ {"id": "brewery_New_Belgium_Brewing", "key": ["New Belgium Brewing"], "value": 1, "doc": { "_id": "brewery_New_Belgium_Brewing", "name": "New Belgium Brewing", .... } }, {"id": "beer_Fat_Tire_Amber_Ale", "key": ["New Belgium Brewing", "Fat Tire Amber Ale"], "value": 1, "doc": { "_id": "beer_Fat_Tire_Amber_Ale", "name": "Fat Tire Amber Ale", ....} } ]}

Results from the brewery_beers index

_design/beers/_view/brewery_beers?startkey=["New Belgium Brewing"]&endkey=["New Belgium Brewing", {}]

Page 22: Developing ii   advanced document design

{"rows": [ {"id": "brewery_New_Belgium_Brewing", "key": ["New Belgium Brewing"], "value": 9, {"id": "brewery_Other_Brewery", "key": ["Other Brewery"], "value": 3]}

Number of Beers per Brewery using Reduce

_design/beers/_view/brewery_beers?group_level=1

• Value's are the full count of docs beginning in this range

• subtract one (in this case) to find the # of beer docs

Page 23: Developing ii   advanced document design

Creating Views

DocumentDatabases

Document DesignBasics

CreatingViews

Demonstrationof ViewEditing

Summary

Couchbase Server 2.0:Querying and Aggregation

with Views

Page 24: Developing ii   advanced document design

● Schema-less Document Databases

● Document Design● you deal in docs daily

● View Index Building● schema on the "way out"

Summary

Page 25: Developing ii   advanced document design

Questions?

Page 26: Developing ii   advanced document design

Thanks!Benjamin Young

[email protected]

@bigbluehat