Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Couchbase Java SDK with Rx-Java

DZone's Guide to

Couchbase Java SDK with Rx-Java

Couchbase Java SDK is built on top of the Rx-Java library, enabling a reactive way to interact with a Couchbase server.

· Database Zone
Free Resource

Whether you work in SQL Server Management Studio or Visual Studio, Redgate tools integrate with your existing infrastructure, enabling you to align DevOps for your applications with DevOps for your SQL Server databases. Discover true Database DevOps, brought to you in partnership with Redgate.

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")));

If you are interested in exploring this sample further, here is my github repo with a working example - https://github.com/bijukunjummen/sample-karyon2-couch

References:

It’s easier than you think to extend DevOps practices to SQL Server with Redgate tools. Discover how to introduce true Database DevOps, brought to you in partnership with Redgate

Topics:
database ,nosql ,couchbase ,java sdk ,rx java

Published at DZone with permission of Biju Kunjummen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}