nosql for java developers
TRANSCRIPT
![Page 1: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/1.jpg)
Webinar NoSQL For Java Developers
Simon Baslé -‐ SDK Engineer
1
![Page 2: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/2.jpg)
Agenda
▪Motivation ▪ The new API, what does it look like? ▪ Example ▪ RxJava and the asynchronous API ▪ Error handling and batching ▪ Live Demo (N1QL DSL) ▪ Plans for the future
2
![Page 3: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/3.jpg)
Let's Dive In !
3
![Page 4: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/4.jpg)
Motivationfor the 2.0 SDK generation
4
![Page 5: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/5.jpg)
Benefits of a SDK?
• Work with the database using familiar language and
patterns associated with it
• It does the heavy lifting for you (cluster topology
awareness, op routing, dealing with protocol(s), ...)
• Mutualize work on performance and offer good
abstractions5
![Page 6: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/6.jpg)
Previous generation (1.4.x) based on Spymemcached
• a lot of legacy, Spymemcached is maybe too
memcached-‐focused
• async, but with java.util.concurrent.Future (limited)
• asynchronous interdependent dataflows are hard to build
6
![Page 7: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/7.jpg)
Time to freshen up the API!
• Find a better, more expressive way of doing async
• Offer coherent well-‐thought abstractions
• Plan for the future, evolvability
• Don't forget about performance!
7
![Page 8: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/8.jpg)
2.0 is a complete rewrite with asynchronicity at its core...
RxJava Netty
8
![Page 9: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/9.jpg)
2.0 is a complete rewrite ...but a synchronous API built on top of it is offered too
9
![Page 10: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/10.jpg)
Architectural Overview
10
![Page 11: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/11.jpg)
Architectural Overview
Core▪ Core abstracts low-‐level
▪ Fully async & message-‐oriented
▪ Low overhead, high performance
▪ Java 6+11
![Page 12: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/12.jpg)
Architectural Overview
Core
Java
▪ Java Client is a higher level abstraction
▪ Exposes RxJava Observables in the Asynchronous API
▪ Adds a Synchronous API on top of it
▪ Java 6+ (especially great for clients in Java 8!) 12
![Page 13: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/13.jpg)
Architectural Overview
Core
Java Hadoop
Spring
▪ All that can be leveraged to build connectors
▪ Spring Data, Hadoop in the works
13
![Page 14: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/14.jpg)
Architectural Overview
Core
Java Scala JRuby, ... Hadoop
Spring Play!2
▪ Other SDKs / connectors could be built
▪ even by you, the community
14
![Page 15: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/15.jpg)
The New API -‐ What does it look like?(for now the Synchronous API)
15
![Page 16: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/16.jpg)
Cluster and BucketEntry points Class naming refers to Couchbase concepts
16
![Page 17: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/17.jpg)
Cluster and BucketEntry points Class naming refers to Couchbase concepts
▪ To bootstrap, connect and also manage nodes, use Cluster – just needs one or more IPs or hostnames to connect
▪ To operate on data, use Bucket
▪ A Bucket is thread safe, should be reused instead of re-‐created
17
![Page 18: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/18.jpg)
The Document<T>where the data goes
18
![Page 19: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/19.jpg)
The Document<T>where the data goes
▪ Represents the data in Couchbase
▪ Both content() and metadata : id(), cas(), expiry()
▪ CAS: Compare-‐and-‐Swap. Sequence number that allows to prevent mutation clashes
19
![Page 20: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/20.jpg)
The Document<T>where the data goes
▪ JsonDocument ▪ BinaryDocument ▪ SerializableDocument ▪ ...
▪ The JsonDocument's content is a JsonObject ▪ simple Map-‐like API to deal with JSON ▪ there's also a RawJsonDocument to deal with JSON as String
20
![Page 21: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/21.jpg)
Key-‐Value Operationsthe gist of the API
21
![Page 22: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/22.jpg)
Key-‐Value Operationsthe gist of the API
▪ Get (but also getFromReplica, getAndLock, touch)
▪ Upsert (but also insert, replace)
▪ Remove
▪ Append, Prepend
▪ Unlock...22
![Page 23: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/23.jpg)
Key-‐Value Operationsthe gist of the API
▪ Each operation returns a Document, by default JsonDocument, with the updated CAS
▪ Overrides to choose a target Document type
▪ Overrides to customize the timeout for each operation
▪ Mutating operations have overrides to choose replication factor and persistence constraints to wait for before returning 23
![Page 24: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/24.jpg)
Management APIs"ops stuff"
24
![Page 25: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/25.jpg)
Management APIs"ops stuff"
▪ cluster.createManager("admin", "pass") ▪ info ▪ hasBucket ▪ getBucket ▪ insert/remove/updateBucket
▪ bucket.bucketManager() ▪ info ▪ flush ▪ get/insert/upsert/removeDesignDocument ▪ publishDesignDocument 25
![Page 26: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/26.jpg)
Example
26
![Page 27: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/27.jpg)
connect to cluster
prepare JSON
insert a document vs. upsert a document
clean up
27
![Page 28: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/28.jpg)
Cluster cluster = CouchbaseCluster.create("127.0.0.1"); Bucket testBucket = cluster.openBucket("testBucket");
//create some JSON content for a new DocumentJsonObject content = JsonObject.create() .put("type", "test") .put("value", "this is a test"); JsonDocument testDoc = JsonDocument.create("test", content);
//insert the fresh DocumenttestBucket.insert(testDoc);
28
![Page 29: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/29.jpg)
//try again with same key? try { testBucket.insert(testDoc); } catch (DocumentAlreadyExistsException e) { //expected //upsert works whenever document exists or not testBucket.upsert(testDoc); }
//cleans up ALL shared resources // (also calls close() on each Bucket)cluster.disconnect();
29
![Page 30: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/30.jpg)
Q&A Intermission 1any questions at this point?
30
![Page 31: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/31.jpg)
RxJava & the Asynchronous APIModern Async Dataflows
31
![Page 32: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/32.jpg)
RxJava 101aka "Async Goodness"
32
![Page 33: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/33.jpg)
RxJava 101aka "Async Goodness"
▪ Observable<T> and Observer<T>
▪ Observable = a Stream of Data
▪ Use Rx operators to manipulate the stream
33
![Page 34: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/34.jpg)
RxJava 101compare with Iterator
Single Multiple
Sync ("pull") T Iterable<T>
Async ("push") Future<T> Observable<T>
34
![Page 35: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/35.jpg)
RxJava 101Observer<T> interface
Event Iterator<T> Observer<T>
data retrieval T next() onNext(T)
discover error throw Exception onError(Throwable)
complete returns onCompleted()35
![Page 36: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/36.jpg)
Observableexposes operators
▪ Transformation
▪ Filtering
▪ Combination
▪ Managing Errors
36
![Page 37: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/37.jpg)
Transform!"map is the first step"
37
![Page 38: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/38.jpg)
Transform!"map is the first step"
▪ Observable<T> source
▪ Observable<R> transformed = source.map(mapFunction)
▪ mapFunction transforms each T into an R ▪ eg. T = String and R = Integer, mapFunction returns string.length()
38
![Page 39: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/39.jpg)
Transform!"map is the first step"
39
![Page 40: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/40.jpg)
Filterwhen you only need part of the data
40
![Page 41: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/41.jpg)
Filterwhen you only need part of the data
▪ first()
▪ take(int n)
▪ skip(int n)
▪ distinct()
▪ ...
41
![Page 42: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/42.jpg)
FilterThe Heavy Artillery
▪ Observable<T> filter( filterPredicate )
▪ Only emit the Ts that match the filterPredicate
42
![Page 43: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/43.jpg)
FilterThe Heavy Artillery
43
![Page 44: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/44.jpg)
Combineseveral streams
44
![Page 45: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/45.jpg)
Combineseveral streams
▪ merge(otherObservableOfT)
▪ concat(otherObservableOfT)
▪ mergeDelayErrors, zipWith, join, ...
45
![Page 46: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/46.jpg)
Combinemerge
46
![Page 47: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/47.jpg)
Combineconcat
47
![Page 48: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/48.jpg)
Back to Transformations"improve your Rx-‐Fu with flatMap"
48
![Page 49: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/49.jpg)
Back to Transformations"improve your Rx-‐Fu with flatMap"
▪ Like a map... ▪ ...for when your transformation must also be asynchronous
49
![Page 50: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/50.jpg)
Back to Transformations"improve your Rx-‐Fu with flatMap"
▪ Like a map... ▪ ...for when your transformation must also be asynchronous
▪ transforms each item to an Observable<R> (not just an R)
▪ flattens them all in the output stream to give a flat stream of Rs
50
![Page 51: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/51.jpg)
Back to Transformations"improve your Rx-‐Fu with flatMap"
51
![Page 52: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/52.jpg)
The Very Last Stepsubscription
52
![Page 53: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/53.jpg)
The Very Last Stepsubscription
▪ You have your final Observable representing your async data flow
▪ Now is time to start emission and describe what to do with data in the end
▪ Always subscribe !
53
![Page 54: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/54.jpg)
The Very Last Stepsubscription
▪ either a full implementation of Observer
▪ or use Observable<R>.subscribe "shortcuts" : just pass in an Action for the part of the API that are relevant (eg. just onNext)
54
![Page 55: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/55.jpg)
©2014 Couchbase Inc. 55
I Know Rx-‐Fu!
![Page 56: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/56.jpg)
Q&A Intermission 2any questions at this point?
56
![Page 57: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/57.jpg)
Putting That Into Actionhow does it weave into the SDK?
57
![Page 58: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/58.jpg)
Putting That Into Action
▪ to access the Async API, use async() on both Cluster and Bucket
▪ now every operation returns an Observable<something>
▪ apply the power of Rx to work with your data as a stream
how does it weave into the SDK?
58
![Page 59: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/59.jpg)
Async Example"the 2014 club"
59
![Page 60: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/60.jpg)
Async Example
I have a list of user IDs
get their detailed profile
and, for those who registered last year (2014),
give them a badge
(make sure to persist it)
then finally display the 2014 Club
60
![Page 61: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/61.jpg)
Async Example"the 2014 club"
Observable.from list of IDs
flatMap to get every profile
filter on registration year
mutate to add the badge via a map
flatMap to upsert the profiles
map to the stream of updated IDs
subscribe in order to print out 61
![Page 62: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/62.jpg)
Async Example"the 2014 club"
Observable<String> oneYearClub =Observable.from(Arrays.asList("1", "18")) .map(userId -> "user::" + userId) .flatMap(docId -> bucket.async().get(docId)) .filter(userDoc -> userDoc.content().getInt("yearRegistered") == Calendar.getInstance().get(Calendar.YEAR) - 1) .map(userDoc -> { userDoc.content().getArray("badges").add("oneYearClub"); return userDoc; }) .flatMap(userDoc -> bucket.async().upsert(userDoc)) .map(userDoc -> userDoc.id().replace("user::", "")); oneYearClub.subscribe(id -> System.out.println("Welcome user #" + id + " to the One Year Club!"));
62
![Page 63: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/63.jpg)
Async Example"the 2014 club"
Observable<String> oneYearClub =Observable.from(Arrays.asList("1", "18")) .map(userId -> "user::" + userId) .flatMap(docId -> bucket.async().get(docId)) .filter(userDoc -> userDoc.content().getInt("yearRegistered")== Calendar.getInstance().get(Calendar.YEAR) - 1) .map(userDoc -> { userDoc.content().getArray("badges").add("oneYearClub"); return userDoc; }) .flatMap(userDoc -> bucket.async().upsert(userDoc)) .map(userDoc -> userDoc.id().replace("user::", ""));oneYearClub.subscribe(id -> System.out.println("Welcome user #" + id + " to the One Year Club!"));
Observable.from(Arrays.asList("1", "18")) .map(userId -> "user::" + userId)
63
![Page 64: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/64.jpg)
Async Example"the 2014 club"
Observable<String> oneYearClub =Observable.from(Arrays.asList("1", "18")) .map(userId -> "user::" + userId) .flatMap(docId -> bucket.async().get(docId)) .filter(userDoc -> userDoc.content().getInt("yearRegistered")== Calendar.getInstance().get(Calendar.YEAR) - 1) .map(userDoc -> { userDoc.content().getArray("badges").add("oneYearClub"); return userDoc; }) .flatMap(userDoc -> bucket.async().upsert(userDoc)) .map(userDoc -> userDoc.id().replace("user::", ""));oneYearClub.subscribe(id -> System.out.println("Welcome user #" + id + " to the One Year Club!"));
.flatMap(docId -> bucket.async().get(docId))
64
![Page 65: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/65.jpg)
Async Example"the 2014 club"
Observable<String> oneYearClub =Observable.from(Arrays.asList("1", "18")) .map(userId -> "user::" + userId) .flatMap(docId -> bucket.async().get(docId)) .filter(userDoc -> userDoc.content().getInt("yearRegistered") == Calendar.getInstance().get(Calendar.YEAR) - 1) .map(userDoc -> { userDoc.content().getArray("badges").add("oneYearClub"); return userDoc; }) .flatMap(userDoc -> bucket.async().upsert(userDoc)) .map(userDoc -> userDoc.id().replace("user::", ""));oneYearClub.subscribe(id -> System.out.println("Welcome user #" + id + " to the One Year Club!"));
.filter(userDoc -> userDoc.content().getInt("yearRegistered") == Calendar.getInstance().get(Calendar.YEAR) - 1)
65
![Page 66: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/66.jpg)
Async Example"the 2014 club"
Observable<String> oneYearClub =Observable.from(Arrays.asList("1", "18")) .map(userId -> "user::" + userId) .flatMap(docId -> bucket.async().get(docId)) .filter(userDoc -> userDoc.content().getInt("yearRegistered") == Calendar.getInstance().get(Calendar.YEAR) - 1) .map(userDoc -> { userDoc.content().getArray("badges").add("oneYearClub"); return userDoc; }) .flatMap(userDoc -> bucket.async().upsert(userDoc)) .map(userDoc -> userDoc.id().replace("user::", ""));oneYearClub.subscribe(id -> System.out.println("Welcome user #" + id + " to the One Year Club!"));
.map(userDoc -> { userDoc.content().getArray("badges") .add("oneYearClub"); return userDoc; })
66
![Page 67: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/67.jpg)
Async Example"the 2014 club"
Observable<String> oneYearClub =Observable.from(Arrays.asList("1", "18")) .map(userId -> "user::" + userId) .flatMap(docId -> bucket.async().get(docId)) .filter(userDoc -> userDoc.content().getInt("yearRegistered")== Calendar.getInstance().get(Calendar.YEAR) - 1) .map(userDoc -> { userDoc.content().getArray("badges").add("oneYearClub"); return userDoc; }) .flatMap(userDoc -> bucket.async().upsert(userDoc)) .map(userDoc -> userDoc.id().replace("user::", ""));oneYearClub.subscribe(id -> System.out.println("Welcome user #" + id + " to the One Year Club!"));
.flatMap(userDoc -> bucket.async().upsert(userDoc))
67
![Page 68: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/68.jpg)
Async Example"the 2014 club"
Observable<String> oneYearClub =Observable.from(Arrays.asList("1", "18")) .map(userId -> "user::" + userId) .flatMap(docId -> bucket.async().get(docId)) .filter(userDoc -> userDoc.content().getInt("yearRegistered")== Calendar.getInstance().get(Calendar.YEAR) - 1) .map(userDoc -> { userDoc.content().getArray("badges").add("oneYearClub"); return userDoc; }) .flatMap(userDoc -> bucket.async().upsert(userDoc)) .map(userDoc -> userDoc.id().replace("user::", ""));oneYearClub.subscribe(id -> System.out.println("Welcome user #" + id + " to the One Year Club!"));
.map(userDoc -> userDoc.id().replace("user::", ""));
68
![Page 69: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/69.jpg)
Async Example"the 2014 club"
Observable<String> oneYearClub =Observable.from(Arrays.asList("1", "18")) .map(userId -> "user::" + userId) .flatMap(docId -> bucket.async().get(docId)) .filter(userDoc -> userDoc.content().getInt("yearRegistered")== Calendar.getInstance().get(Calendar.YEAR) - 1) .map(userDoc -> { userDoc.content().getArray("badges").add("oneYearClub"); return userDoc; }) .flatMap(userDoc -> bucket.async().upsert(userDoc)) .map(userDoc -> userDoc.id().replace("user::", ""));oneYearClub.subscribe(id -> System.out.println("Welcome user #" + id + " to the One Year Club!"));
oneYearClub.subscribe(id -> System.out.println( "Welcome user #" + id + " to the One Year Club!"));
Observable<String> oneYearClub = ...;
69
![Page 70: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/70.jpg)
What If there are Errors?
70
![Page 71: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/71.jpg)
Error-‐handling & Batching
71
![Page 72: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/72.jpg)
Error Handlingthe basics
▪ Basic Error Handling is using Observer.onError(Throwable t)
▪ One can also use the default value pattern ▪ Observable<T>.onErrorReturn(T default)
72
![Page 73: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/73.jpg)
Error Handlingmore advanced
▪ Defer to a backup stream on error : ▪ Observable<T>.onErrorResumeNext(Observable<T> backup)
73
![Page 74: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/74.jpg)
Error Handlingmore advanced
▪ Defer to a backup stream on error : ▪ Observable<T>.onErrorResumeNext(Observable<T> backup)
74
![Page 75: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/75.jpg)
Error Handlingmore advanced
▪ Retry on error : ▪ Observable<T>.retry(retryPredicate)
▪ retryPredicate takes the number of retries and the last error to determine if another retry should happen
▪ if that's the case, there's a re-‐subscription
75
![Page 76: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/76.jpg)
Error Handlingmore advanced
▪ Retry on error : ▪ Observable<T>.retry(retryPredicate)
76
![Page 77: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/77.jpg)
Error Handlingbuild on this for production-‐ready strategies
▪ Exponential backoff ▪ using retry overrides to retry with a delay that grows
▪ Throttling ▪ when rate of data creation > rate of data consumption ▪ divide timeline into windows, only emit first/last item of each window
▪ Concept of backpressure ▪ when rate of data creation > rate of data consumption ▪ a way to let the Observer signal source at what pace data production is manageable
77
![Page 78: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/78.jpg)
Batching"where is bulk get gone?"
78
![Page 79: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/79.jpg)
Batching"where is bulk get gone?"
▪ Now relying on RxJava primitives for bulk
▪ Underlying RingBuffer + Netty is pretty capable for rapid-‐burst requests
▪ In a word, use Observable.from(collection of keys) and flatMap to a get
79
![Page 80: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/80.jpg)
Batching"where is bulk get gone?"
Observable<JsonDocument> allDocs = Observable.from(allKeys) .flatMap(new Func1<String, JsonDocument>() { public Observable<JsonDocument> call(String id) { return bucket.async().get(id); } });
80
![Page 81: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/81.jpg)
Batching"where is bulk get gone?"
Observable<JsonDocument> allDocs = Observable.from(allKeys) .flatMap(new Func1<String, JsonDocument>() { public Observable<JsonDocument> call(String id) { return bucket.async().get(id); } });
The best thing is, it is now possible on every operations, not just get!81
![Page 82: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/82.jpg)
Querying JSON with N1QLdeveloper preview
82
![Page 83: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/83.jpg)
Let's do a Live Demo!
83
![Page 84: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/84.jpg)
* Totally made with Special Effects
*
Let's do a Live Demo!
84
![Page 85: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/85.jpg)
Plans for the futurea glimpse at things to come
85
![Page 86: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/86.jpg)
2.1"Overall, More Goodness"
Geo, N1QL, ... + lots of bug fixes
86
![Page 87: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/87.jpg)
Finalizing N1QL Supportgive your feedback on developer preview
87
![Page 88: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/88.jpg)
Spring Data Connector, 2.0porting the adapter to the new client
88
![Page 89: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/89.jpg)
Thank You!the End
89
![Page 90: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/90.jpg)
Final Q&A?
90
![Page 91: NoSQL for Java Developers](https://reader033.vdocuments.site/reader033/viewer/2022042516/55a7aa0b1a28ab6e638b4693/html5/thumbnails/91.jpg)
via Wikipedia via Wikimedia via Flickr via Wikimedia , still from the motion picture
CC BY-‐SA David Shankbone CC BY-‐SA Donovan Govan
CC BY-‐ND NCinDC CC BY Evan Lovely
all rights reserved Warner Bros
Diver Kitchen Funnel
Then&Now Computer-‐Using Cat
"The Matrix",
End Credits
91