VOOZH about

URL: https://www.javacodegeeks.com/2015/09/couchbase-java-sdk-with-rx-java.html

⇱ Couchbase Java SDK with Rx-Java - Java Code Geeks


A neat thing about CouchbaseJava SDK  is that it is built on top of the excellent Rx-Java library, this enables a reactive way to interact with a Couchbase server instance which is very intuitive once you get the hang of it.

Consider a very simple json document that I intend to store in Couchbase:

{"key":"1","value":"one"}

and a Java class to hold this json:

public class KeyVal {
 private String key;
 private String value;

 ...
}

The following is the code to insert an instance of KeyVal to a Couchbase bucket:

JsonObject jsonObject = JsonObject.empty().put("key", keyVal.getKey()).put("value", keyVal.getValue());
JsonDocument doc = JsonDocument.create(keyVal.getKey(), jsonObject);
Observable<JsonDocument> obs = bucket
 .async()
 .insert(doc);

The return type of the insert is an Observable, so if I needed to map the return type back a KeyVal I can use the extensive mapping support provided by Observable class.

Observable<KeyVal> obs = bucket
 .async()
 .insert(doc)
 .map(jsonDoc -> 
 new KeyVal(jsonDoc.id(), jsonDoc.content().getString("value"))
 );

Other API’s follow a similar pattern, for eg. to retrieve the saved document:

bucket
 .async()
 .get(id)
 .map(doc ->
 new KeyVal(doc.id(),
 doc.content().getString("value")));

Resources

Reference: Couchbase Java SDK with Rx-Java from our JCG partner Biju Kunjummen at the all and sundry blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Biju Kunjummen
Biju Kunjummen
September 4th, 2015Last Updated: September 3rd, 2015
0 82 1 minute read
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz