How to get row.document() with java SDK 3.x

How can I get document content with couchbase java-client 3.x? Something like bucket.defaultCollection().get(row.id().get(), getOptions().transcoder(RawJsonTranscoder.INSTANCE).timeout(Duration.ofSeconds(10))).contentAs(String.class);
in the foreach can work but isn’t the right way.

i.e.
ViewResult viewResult = bucket.viewQuery(“design-doc-name”, “view-name”);
for (ViewRow row : viewResult.rows()) {
System.out.println("Found row: " + row);
…row.document()…
}

Hi Porta,

If you know the result row is a String, row.valueAs(String.class) should work.

If you don’t know the result type, you could do row.valueAs(JsonNode.class) and use the Jackson Tree Model API to inspect the results.

Thanks,
David

EDIT: changed contentAs -> valueAs

Hi David, not sure why but in my case doesn;t work.
With couchbaseClient in SDK 1.x I had a list of ViewRowWithDocs that have following fields(id, key, value, doc).
I SDK 3.x I had a list of ViewChunckRow that have only (id, key, value).
Id, key and value are the same but doc is something else and I don’t know ho to get it, except getting each document.

Hi Porta,

I made a mistake in my previous post; in SDK 3 the method is ViewRow.valueAs (not contentAs). Does that make a difference?

Id, key and value are the same but doc is something else and I don’t know ho to get it, except getting each document.

Can you clarify please, what do you need from the “doc” that’s not in the ID, key, and value?

Thanks,
David

In the SDK3 for ViewRow.valueAs(JsonObject.class), I’ve got Deserialization of content into target class com.couchbase.client.java.json.JsonObject failed; encoded = {"from":98254059}. But its content is not I’m expected.
In SDK 1, I get this too for value field, but "doc " looks totally different. Now I can get that value using
bucket.defaultCollection().get(row.id().get(), getOptions().transcoder(RawJsonTranscoder.INSTANCE).timeout(Duration.ofSeconds(10))).contentAs(String.class);
which is not the right way to do it, doing a read for each document id.

Document content is something like
{“id”:“f147174a-46eb-4aa8-90ed-e2a6dce264c3”,“amount”:20.0,“src”:“MAIN”,"“created”:“210115171323595+0000”,“changed”:“210115171323595+0000”,“transactions”:[{“step”:1,“orderId”:98254059},{“step”:2,“orderId”:98254059}]}

valueAs() return View result (what javascript “function (doc, meta) {emit(…)})” returns) and I need the whole document. the document content was included in the response in previous SDK versions. Now, how can I can get it and not calling get foreach row?

2 Likes

Hi Porta,

Thanks for clarifying the issue. I asked around and learned that the “include docs” feature was not carried forward from SDK 2 to SDK 3; as a result, it’s necessary to fetch the document content in a separate request. I realize this probably isn’t the answer you were hoping for. As I understand it, the motivation for this change was to simplify the View API, and to give users more flexibility in how they want to load the documents.

In SDK 3, the most efficient way to get the document bodies would be to use the reactive API to issue a batch request.

Thanks,
David

1 Like

Hi ,
could give an example for this to fetch document from View in couchbase java sdk3+.
I could not find any such example?

I could not find code for how to migarte ViewRow.document() method from java sdk1.2 to sdk3+.

if you could give me coding example than would be great.

I am getting ViewResult from asyncBucket.

MY code:

CompletableFuture result=bucket.viewQuery(getDesignDoc(), getView(),viewOptions);

result.get().rows().stream().collect(Collector.ToList())

;//here i want to collect document from the View.

if you code give me code example than would be great.

see How to get viewRow.document() with java SDK 3.x