Hello,
I am trying to migrate from couchbase SDK 2.7.2 to SDK 3.2.7 . I am trying to find the way to migrate to JsonDocument in SDK 3.7.2
Existing code is as below
// Query
List<JsonDocument> foundDocs = Observable.from(idList)
.flatMap(new Func1<String, Observable<JsonDocument>>() {
@Override
public Observable<JsonDocument> call(String id) {
return bucket.async().get(id);
}
})
.toList()
.toBlocking()
.single();
There is no support for Observable and JsonDocument in SDK3.2.7. What is the way to get the JsonDocument in SDK3.2.7. ?
daschl
June 30, 2022, 7:14am
2
@rishabh_katyal I recommend that you check out our SDK 3 documentation which covers all of this in detail.
Here is the equivalent in SDK 3:
List<GetResult> foundDocs = Flux
.fromIterable(Arrays.asList("id1", "id2"))
.flatMap(id -> collection.reactive().get(id))
.collectList()
.block();
Note that if a document is not found you’ll get an exception in SDK 3, but if you just want to ignore it you can do it like this:
List<GetResult> foundDocs = Flux
.fromIterable(Arrays.asList("id1", "id2"))
.flatMap(id -> collection.reactive().get(id).onErrorResume(t -> {
if (t instanceof DocumentNotFoundException) {
return Mono.empty();
} else {
return Mono.error(t);
}
}))
.collectList()
.block();
@daschl Thanks for the reply.
bucket.async().get(id);
AsyncBucket.get()
would just return empty Observable
when the document does not exist, however, it would throw DocumentNotFoundException
in SDK 3.
So, the below part is to resume in case if there is an Error ?
collection.reactive().get(id).onErrorResume(t → {
if (t instanceof DocumentNotFoundException) {
return Mono.empty();
} else {
return Mono.error(t);
}
Also,
since we dont have JsonDocuments in SDK3, so, how can i use the GetResult to get the document id ?
My current code is as below:
// Result Set
return foundDocs.stream()
.map(doc -> resultSet(doc))
.collect(Collectors.toList());
}
private ItemCouchbaseEntity resultSet(JsonDocument doc) {
ItemCouchbaseEntity entity = new ItemCouchbaseEntity();
// id
entity.setId(doc.id());
return entity;
}
You would do it inside the flatMap like this:
List<ItemCouchbaseEntity> foundDocs = Flux
.fromIterable(Arrays.asList("id1", "id2"))
.flatMap(id -> collection.reactive()
.get(id)
.map(result -> {
// here you convert from the GetResult to your entity and you have the id still in scope from
// the outer flatMap
ItemCouchbaseEntity entity = new ItemCouchbaseEntity();
entity.setId(id);
return entity
})
.onErrorResume(t -> {
if (t instanceof DocumentNotFoundException) {
return Mono.empty();
} else {
return Mono.error(t);
}
})
)
.collectList()
.block();