How to Build Your First Couchbase Application – Couchbase Live New York 2015
Transcript of How to Build Your First Couchbase Application – Couchbase Live New York 2015
How to build your first Couchbase Application
Martin Esmann | Developer Advocate, CouchbaseMichael Nitschinger | SDK Engineer, Couchbase
©2015 Couchbase Inc. 2
Separation of Concerns
Design around clear system boundaries
FrontendBackendCouchbase JSONSDK
©2015 Couchbase Inc. 3
Sample Application
For 4.0, we’ve built a sample application
Uses the travel-sample bucket
Search flights and book your seats!
Pick your Language!
– Java: https://github.com/couchbaselabs/try-cb-java
– .NET: https://github.com/couchbaselabs/try-cb-dotnet
– NodeJS: https://github.com/couchbaselabs/try-cb-nodejs
– Go: https://github.com/couchbaselabs/try-cb-golang
©2015 Couchbase Inc. 4
Separation of Concerns
Design around clear system boundaries
FrontendBackendCouchbase JSONSDK
Data Modeling
©2015 Couchbase Inc. 6
Embrace the Couchbase Way
Modeling against Couchbase can be the same, but will likely be different than against an RDBMS (JSON is more flexible)
Think about your data access!
– Embedding vs. Referencing
– Key lookup patterns
– N1QL supports your application model
Check out: Couchbase Webinars & Docs on Data Modeling
– http://www.couchbase.com/nosql-resources/webinar/
– http://docs.couchbase.com/developer/dev-guide-3.0/modeling-docs.html
©2015 Couchbase Inc. 7
Answer oriented databases
©2014 Couchbase, Inc. 7
http://martinfowler.com/bliki/AggregateOrientedDatabase.html
©2015 Couchbase Inc. 8
Airline bookings
©2014 Couchbase, Inc. 8
©2015 Couchbase Inc. 9
Travel-Sample Data Model
Document Types:
– Airline
– Airport
– Route
– Landmark
– User
Identified by a „type“ attribute
Try: select distinct(type) from `travel-sample`;
©2015 Couchbase Inc. 10
Airline
{
"callsign": "SASQUATCH",
"country": "United States",
"iata": "K5",
"icao": "SQH",
"id": 10765,
"name": "SeaPort Airlines",
"type": "airline”
}
©2015 Couchbase Inc. 11
Route
{
"airline": "AF",
"airlineid": "airline_137",
"destinationairport": "CDG",
"sourceairport": "TXL",
"distance": 850.4987892267409,
"equipment": "321 319 320 318",
"id": 10012,
"stops": 0,
"type": "route"
"schedule": [
{ "day": 0, "flight": "AF943", "utc": "01:05:00" },
{ "day": 0, "flight": "AF814", "utc": "16:52:00" },
...
}
©2015 Couchbase Inc. 12
Separation of Concerns
Design around clear system boundaries
FrontendBackendCouchbase JSONSDK
Document Access
©2015 Couchbase Inc. 14
Languages and Interfaces for Couchbase
Official SDKs
– Java – Version 2.2
– .NET – Version 2.1
– Node.js – Version 2.1
– Python – Version 2.0
For each of these we have
– Full Document support
– Interoperability
– Common yet idiomatic Programming Model
Others: Erlang, Perl, TCL, Clojure, Scala
©2014 Couchbase, Inc. — Proprietary and Confidential
PHP – Version 2.0
C – Version 2.5
Go – Version 1.0
Ruby – Version 2.0 DP
JDBC and ODBC
©2015 Couchbase Inc. 15
Couchbase SDKs
What does it mean to be a Couchbase SDK?
Cluster
Bucket
CRUDView
QueryN1QL Query
FunctionalManage connections to the bucket within the cluster for different services.Provide a core layer where IO can be managed and optimized.Provide a way to manage buckets.
APIinsertDesignDocument()flush()listDesignDocuments()
FunctionalHold on to cluster information such as topology.
APIReference Cluster ManagementopenBucket()info()disconnect()
FunctionalGive the application developer a concurrent API for basic (k-v) or document management
APIget()insert()upsert()remove()
FunctionalAllow for querying, execution of other directives such as defining indexes and checking on index state.
APIabucket.NewN1QLQuery(
“SELECT * FROM default LIMIT 5” ).Consistency(gocouchbase.RequestPlus);
FunctionalAllow for view querying, building of queries and reasonable error handling from the cluster.
APIabucket.NewViewQuery().Limit().Stale()
©2015 Couchbase Inc. 16
Pick your Tool from Toolbelt
©2014 Couchbase, Inc. 16
Application layer computation Off-load computation
Predictable queries Key-value: pre-computed answers Views
Ad-hoc queries N1QL and key-value with manual indexes N1QL and Views
©2015 Couchbase Inc. 17
N1QL or Views?
©2014 Couchbase, Inc. 17
N1QL Views
Ad-hoc querying Predictable queries
Nested JSON in and unnested JSON out Number crunching
Large growth clusters Multi-dimensional/geospatialqueries
©2015 Couchbase Inc. 18
Offloading computations to N1QL
N1QL allows you to choose which elements are returned at the top level.
This is incredibly useful as it reduces the need to write complex parsing logic:
– Within the application
– In Client side front end frameworks (Angular/Backbone etc)
SELECT a.name, s.flight, s.utc, r.sourceairport, r.destinationairport, r.equipment
FROM `travel-sample` r UNNEST r.schedule s JOIN `travel-sample` a ON KEYS r.airlineid
WHERE r.sourceairport='LHR' AND r.destinationairport='LAX' AND s.day=2 ORDER BY a.name
©2014 Couchbase, Inc. 18
©2015 Couchbase Inc. 19
Separation of Concerns
Design around clear system boundaries
FrontendBackendCouchbase JSONSDK
Serving Your Frontend
©2015 Couchbase Inc. 21
Exposing an API
Expose a „sane“ API to allow decoupling
Make use of JSON as the primary data storage format Remember: Develop with Agility
Tools like Ember & AngularJS already provide abstractions – use them!
Look at concepts like REST and HATEOAS for „discoverable“ and self-documenting APIs
©2015 Couchbase Inc. 22
Travel-Sample API (/api/)
(RequestParams omitted for clarity)
/user
– GET & POST /login
– GET & POST /flights
GET /airport/findAll
GET /flightPath/findAll
Sample Application Walkthrough
©2015 Couchbase Inc. 24
Application Walkthrough
©2015 Couchbase Inc. 25
Application Walkthrough
©2015 Couchbase Inc. 26
Application Walkthrough
©2015 Couchbase Inc. 27
Application Walkthrough
©2015 Couchbase Inc. 28
Application Walkthrough
©2015 Couchbase Inc. 29
Application Walkthrough
Demo!
©2015 Couchbase Inc. 31
What we learned
N1QL is a flexible query language
The SDKs have first class support for N1QL
We used raw N1QL queries to look-up airports by faa, icao and city
We encoded the N1QL query paramters by adding parameters to the QueryRequest.
We used Language Integrated Query (LINQ) to build a type-safe querythat converted Airportnames to an FAA value.
The other SDKs have similar "LINQ" options
This demo was in .NET but you can pick your favorite…
©2015 Couchbase Inc. 32
Try out the samples
developer.couchbase.comTo learn more about Couchbase, SDKs, N1QL and more…
Q&A
Thank you!