1

I am using Couchbase 3.2 SDK, to query couchbase server. Like below

QueryResult result = cluster.query(
                    "SELECT *, Meta().id FROM  bucketName USE KEYS ?",
                    queryOptions().parameters(JsonArray.from("pk")).readonly(true));

The response is like

[{"id":"pk","bucketName":"<binary (21 b)>"}]

My Document is a byte[], How can I get byte[] from the query response. I have tried

  1. parsing to custom object by doing
    result.rowsAs(CustomClass.class)
    
  2. Also tried
    for (JsonObject row : result.rowsAsObject()) {
                 resposne.put((String) row.get("id"), ((String)row.get("ratingsAndReviewCollection")).getBytes("UTF-8"));
             }
    

but both of them does not return the same doc that i had put.

This thread talks about this but does not give clear solution to this.

Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
best wishes
  • 5,789
  • 1
  • 34
  • 59

2 Answers2

2
GetResult s = cluster.bucket("bucketName").defaultCollection().get("pk");
byte[] doc = s.contentAs(byte[].class);
Michael Reiche
  • 375
  • 1
  • 7
  • I meant queryresult which comes by executing N1QL query. hence the above answer does not answer the question – best wishes Aug 10 '21 at 02:18
  • I had to do this because when i was using async java client and doing ` List results = Flux.fromIterable(docsToFetch).flatMap(reactiveCollection::get).collectList().block();` i was not able to associate which GetResult associates to which id. I am not sure what i am supposed to do with the GetResult if i can not associate it with the original id. – best wishes Aug 10 '21 at 02:20
0

It is not possible with N1QL, hence the solution in the referenced thread and the solution I provided to the specific question asked.

To pair arguments with results, get creative with reactor. The following is with spring-data-couchbase, where findById() returns Airport. Using the SDK replace findById() with get() which returns a GetResult.

Flux<Pair<String, Mono<Airport>>> pairFlux = Flux.fromIterable(list).map((airport) -> Pair.of(airport.getId(), airportRepository.findById(airport.getId())));
List<Pair<String, Mono<Airport>>> airportPairs = pairFlux.collectList().block();
for (Pair<String, Mono<Airport>> airportPair : airportPairs) {
    System.out.println("id: " + airportPair.getFirst() + " airport: " + airportPair.getSecond().block());
}
Michael Reiche
  • 375
  • 1
  • 7