Getting Started with the Java SDK
Michael Nitschinger, SDK Engineer@daschl
Overview
©2015 Couchbase Inc. 3
Couchbase SDKs What does it mean to be a Couchbase SDK?
Cluster
Bucket
CRUD View Query
N1QL 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()
©2014 Couchbase, Inc. — Proprietary and Confidential
©2015 Couchbase Inc. 4
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
PHP – Version 2.0 C – Version 2.5 Go – Version 1.0 Ruby – Version 2.0
DP
JDBC and ODBC
©2014 Couchbase, Inc. — Proprietary and Confidential
©2015 Couchbase Inc. 5
Documents are integral to the SDKs. There are many implementations, depending on the
content type.
A Document contains:
The Document
5
Property DescriptionID The bucket-unique identifierContent The value that is storedExpiry An expiration timeCAS The Compare-And-Swap identifier
©2015 Couchbase Inc. 6
Facts Split into two projects
– core-io– java-client
Current Versions (java-client)– 2.2.0 (GA)
Documentation available through http://developer.couchbase.com/documentation/server/4.0/sdks/java-2.2/java-intro.html
6
©2014 Couchbase, Inc. — Proprietary and Confidential
©2015 Couchbase Inc. 7
Couchbase Core IO
7
Common infrastructure & feature set for all language bindings
Message oriented Asynchronous only Low overhead and performance focused Supports Java 6+ (including 8!)
Disruptor RingBuffer for implicit batching and backpressure Netty for high performance IO
©2015 Couchbase Inc. 8
Detour: Java Client Architecture
8
©2015 Couchbase Inc. 9
Detour: Smart Batching
Source: http://mechanical-sympathy.blogspot.co.at/2011/10/smart-batching.html 9
©2015 Couchbase Inc. 10
Detour: Smart Batching
Source: http://mechanical-sympathy.blogspot.co.at/2011/10/smart-batching.html 10
©2014 Couchbase, Inc. — Proprietary and Confidential
©2015 Couchbase Inc. 11
The Big Picture
11
©2014 Couchbase, Inc. — Proprietary and Confidential
©2015 Couchbase Inc. 12
Couchbase Java Client
12
Java Binding on top of core-io
Clusters/Buckets Documents Sync & Async API Bucket and Cluster Management Various Utilities and Helpers
©2014 Couchbase, Inc. — Proprietary and Confidential
©2015 Couchbase Inc. 13
Connecting
13
©2014 Couchbase, Inc. — Proprietary and Confidential
©2015 Couchbase Inc. 14
From Sync to Async
14
©2015 Couchbase Inc. 15
2.x API
RxJava 101A Gentle Introduction
©2015 Couchbase Inc. 17
Java implementation for Reactive Extensions https://github.com/ReactiveX
A library to compose asynchronous and event-driven programs through observable sequences.
RxJava: Introduction
single multiplesync T Iterable<T>async
Future<T>
Observable<T>
17
©2015 Couchbase Inc. 18
Observables are the duals of Iterables They describe both Latency and Error side effects.
RxJava: Introduction
event Iterable<T> (pull) Observable<T> (push)data retrieval
T next() onNext(T)
error discovery
throws Exception onError(Exception)
completion returns onCompleted()
18
©2015 Couchbase Inc. 19
Consuming Observables
19
The Observer subscribes and receives events.
A cold Observablestarts when subscribed.
onNext can be called0..N times
©2015 Couchbase Inc. 20
RxJava: Creating Observables
just
20
©2015 Couchbase Inc. 21
RxJava: Creating Observables
21
©2015 Couchbase Inc. 22
RxJava: Creating Observables
22
©2015 Couchbase Inc. 23
RxJava: Creating Observables
23
©2015 Couchbase Inc. 24
RxJava: Creating Observables
24
©2015 Couchbase Inc. 25
RxJava: Creating Observables
25
©2015 Couchbase Inc. 26
RxJava: Transforming Observables
26
©2015 Couchbase Inc. 27
RxJava: Transforming Observables
27
©2015 Couchbase Inc. 28
RxJava: Transforming Observables
28
©2015 Couchbase Inc. 29
RxJava: Transforming Observables
29
©2015 Couchbase Inc. 30
RxJava: Transforming Observables
30
©2015 Couchbase Inc. 31
RxJava: Transforming Observables
31
©2015 Couchbase Inc. 32
RxJava: Transforming Observables
32
©2015 Couchbase Inc. 33
RxJava: Transforming Observables
33
©2015 Couchbase Inc. 34
RxJava: Filtering Observables
34
©2015 Couchbase Inc. 35
RxJava: Filtering Observables
35
©2015 Couchbase Inc. 36
RxJava: Filtering Observables
36
©2015 Couchbase Inc. 37
RxJava: Filtering Observables
37
Spring Data Couchbase
©2015 Couchbase Inc. 39
Spring IO
©2015 Couchbase Inc. 40
Spring Data Couchbase
• Integration with Spring Data Templates Repositories Exception Mapping also @Cacheable
• Maps POJO Entities to JSON (and back)• 2.0 M1 Released!
©2015 Couchbase Inc. 41
Spring Data Landscape
JDBC/JPA
©2015 Couchbase Inc. 42
Repositories – Do this
©2015 Couchbase Inc. 43
Repositories – Get this
43
©2015 Couchbase Inc. 44
Repositories – Backed by Views
findByFirstname()
findAll(), count()
44
©2015 Couchbase Inc. 45
JavaConfig
45
©2015 Couchbase Inc. 46
Putting it together
46
©2015 Couchbase Inc. 47
There is more! Entity mapping (Alias keys, TTLs, @Version for optimistic
concurrency,...)
JavaConfig and XML support CouchbaseTemplate for lower level access @View to customize Views and DesignDocuments @Cacheable suport for transparent caching
Spring Boot and Spring XD integration upcoming
47
Questions?