Download - 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
● 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
Document Databases
DocumentDatabases
Document DesignBasics
CreatingViews
Demonstrationof ViewEditing
Summary
● 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
● 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
Document Design Basics
DocumentDatabases
Document DesignBasics
CreatingViews
Demonstrationof ViewEditing
Summary
{ "_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 >_<
{"_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!
Creating Views
DocumentDatabases
Document DesignBasics
CreatingViews
Demonstrationof ViewEditing
Summary
Couchbase Server 2.0:Querying and Aggregation
with Views
Document Editing Demo
Couchbase Server 2.0:Querying and Aggregation
with Views
● 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
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
_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)
View Editing
DocumentDatabases
Document DesignBasics
CreatingViews
Demonstrationof ViewEditing
Summary
Views Editing Demo
{"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
Wait...where's the data?
Meet ?include_docs=true
{"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
{"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", {}]
{"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
Creating Views
DocumentDatabases
Document DesignBasics
CreatingViews
Demonstrationof ViewEditing
Summary
Couchbase Server 2.0:Querying and Aggregation
with Views
● Schema-less Document Databases
● Document Design● you deal in docs daily
● View Index Building● schema on the "way out"
Summary
Questions?