简体   繁体   English

批量读取Couchbase文档

[英]Bulk read Couchbase documents

I want to asynchronously read a number of documents from a Couchbase bucket. 我想异步地从Couchbase存储桶中读取许多文档。 This is my code: 这是我的代码:

JsonDocument student = bucketStudent.get(studentID);

The problem is for a large data file with a lot of studentIDs, it would take a long time to get all documents for these studentIDs because the get() method is called for each studentID. 问题是对于具有大量StudentID的大型数据文件,由于这些学生ID会调用get()方法,因此get()这些StudentID的所有文档将花费很长时间。 Is it possible to have a list of studentIDs as input and return an output of a list of students instead of getting a single document for each studentID? 是否可以有一个学生ID列表作为输入并返回学生列表的输出,而不是为每个学生ID获得单个文档?

AFAIK couchbase SDK does not have a native function for a bulk get operation. AFAIK Sofabase SDK没有用于批量get操作的本机功能。

The node.js SDK has a getMulti method, but it's basically an iteration over an array and then get() is fired for each element. node.js SDK有一个getMulti方法,但基本上是一个数组迭代,然后为每个元素触发get()

I've found in my applications that the key-value approach is still faster than the SELECT * on a primary index but the N1QL query is remarkably close (on couchbase 5.x). 在我的应用程序中,我发现key-value方法仍然比主索引上的SELECT *更快,但是N1QL查询非常接近(在ouchbase 5.x上)。

Just a quick tip: if you have A LOT of ids to fetch and you decide to go with the N1QL queries, try to split that list in smaller chunks. 快速提示:如果要提取很多ID,并且决定使用N1QL查询,请尝试将该列表拆分成较小的块。 It's speeds up the queries and you can actually manage your errors better and avoid getting some nasty timeouts. 它加快了查询速度,您实际上可以更好地管理错误,避免出现一些讨厌的超时。

If you are running a query node, you can use N1QL for this. 如果您正在运行查询节点,则可以为此使用N1QL。 Your query would look like this: 您的查询如下所示:

SELECT * FROM myBucket USE KEYS ["key1", "key2", "key3"]

In practice you would probably pass in the array of strings as a parameter, like this: 实际上,您可能会将字符串数组作为参数传递,如下所示:

SELECT * FROM myBucket USE KEYS ?

You will need a primary index for your bucket, or queries like this won't work. 您需要为存储桶添加主索引,否则类似的查询将无法正常工作。

Retrieving multiple documents using the document IDs is not supported by default in the Couchbase Java SDK. Couchbase Java SDK默认情况下不支持使用文档ID检索多个文档。 To achieve that you'll need to use a N1QL query as below 为此,您需要使用N1QL查询,如下所示

SELECT S.* FROM Student S USE KEYS ["StudentID1", "StudentID2", "StudentID3"]

which would return an array of Documents with the given IDs. 它将返回具有给定ID的Documents数组。 Construct the query with com.couchbase.client.java.query.N1qlQuery , and use either of below to execute 使用com.couchbase.client.java.query.N1qlQuery构造查询,并使用以下任一方法执行

If you're using Spring's CouchbaseTemplate , you can use the below 如果您使用Spring的CouchbaseTemplate ,则可以使用以下内容

List<T> findByN1QL(com.couchbase.client.java.query.N1qlQuery n1ql,
                          Class<T> entityClass)

If you're using Couchbase's Java SDK , you can use the below 如果您使用Couchbase的Java SDK ,则可以使用以下内容

N1qlQueryResult query(N1qlQuery query)

Note that you'll need an index on your bucket to run N1QL queries. 请注意,您需要在存储桶上建立index以运行N1QL查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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