Java SDK2.1 tutorial's code not work

Hi, all

Recently I am learning how to use couchbase 4 with java SDK and I learn a lots from the tutorial.
But sometimes it got errors when I use the same code in tutorial, and I didn’t know how to solve it.
For example, in the tutorial Java SDK2.1 Updating documents chapter

the update document code that tutorial provided

bucket
.get(“id”)
.map(new Func1<JsonDocument, JsonDocument>() {
@Override
public JsonDocument call(JsonDocument document) {
modifyDocumentSomehow(document);
return document;
}})
.flatMap(new Func1<JsonDocument, Observable>() {
@Override
public Observable call(JsonDocument document) {
return bucket.replace(document);
}}).subscribe();

I have include couchbase-core-io-1.1.4.jar, couchbase-java-client-2.1.4.jar, rxjava-1.0.4.jar
It have compilation error in “map” and “return” in flatmap.
Here is my code with rewrite version from tutorial: QAm5sE - Online Java Compiler & Debugging Tool - Ideone.com

I don’t get it.

But if I add 2 async() methods to the position before get and the position in the flatmap’s return, It works fine as below

bucket
.async()
.get(“id”)
.map(new Func1<JsonDocument, JsonDocument>() {

})
.flatMap(new Func1<JsonDocument, Observable>() {
@Override
public Observable call(JsonDocument document) {
return bucket.async().replace(document);
}}).subscribe();

Why it can work but fail without async()?

Hi, We assume in the documentation that people use the asynchronous bucket if not stated otherwise. If you do not call async, you get a synchronous bucket. A get from a sync bucket returns a JsonDocument directly, as oppose to an Obseervable. The map, flatmap, etc functions are used with observables.

2 Likes