简体   繁体   中英

How to get bulk documents/multi gets based on keys in spring data couchbase

I have been trying to get bulk documents based on a list of id's. But for some reason I don't see a method in CRUD Repo which can give me that data. I was able to locate a method name "FindAll(List)", but this seems to work only on the view named "all", and I don't want to introduce a view for a simple lookup(which primarily should be a functionality of couchbase).

Can someone please let me know what all options I have to achieve my end goal if I dont want to end up using Views or Nickel queries.

Also, why is it not supported by spring data couchbase. Is this something that is not expected?

The Repository needs to be able to findAll() documents that it is tasked with saving. Problem is, in Couchbase you can save all sorts of documents in the same bucket, so the repository needs a way of isolating only the documents that match its Entity type.

It is done with the requirement of the View for CRUD operations, and for generated N1QL queries by appending a criteria on the _class field to the WHERE clause.

When you provide a List of keys, the Couchbase repository simply reuses the view you had to configure so that findAll() works, which has the additional benefit of ensuring that keys not corresponding to a correct Entity (that is, not indexed by the view) will be ignored.

That said I think it is on the roadmap to remove the view requirement... (But that's up to the Couchbase team. Maybe raise an issue to get a more definitive answer to that).

Spring Data Kay and its support for Reactive Programming will most likely also change the landscape.

According to documentation , with RxJava you can use effective batching.

Code example

bucket.async()
        .query(N1qlQuery.simple("SELECT meta().id as id FROM bucket"))
        .doOnNext(res -> res.info().map(N1qlMetrics::elapsedTime).
         forEach(t -> System.out.println("time elapsed"+t)))
        .flatMap(AsyncN1qlQueryResult::rows)
        .flatMap(row -> 
        bucket.async().
        get(row.value().getString("id")))
        .map(JsonDocument::content).
        toList()
        .toBlocking()
        .single();

RxJava is asynchronous and will save additional round-trips and should end up the better performer!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM