Spring Data CosmosTemplate使用系统函数创建查询

huangapple go评论65阅读模式
英文:

Spring Data CosmosTemplate create query with System Functions

问题

我们正在尝试使用 spring-data-cosmosdb 项目中的 cosmosTemplate 实现一个查询。查询具有以下语法:

"select * from movie where ARRAY_CONTAINS(movie.countries, @country)"。

CosmosTemplate 接受 DocumentQuery 对象,该对象基于 Criteria 对象构建。Criteria 对象仅支持一小部分基本谓词,如 inoris equal 等,但没有 array_contains 谓词。

目前,我们通过使用 cosmos 客户端(来自 sdk)执行查询,而不是使用 cosmosTemplate。这带来了两个问题:

  1. 我们不得不混合使用 cosmosTemplate 和 cosmos 客户端的代码。
  2. 由于我们有复杂的参数化查询,涉及系统函数,我们不得不拼接 SQL 查询字符串并收集 SQL 参数。

如何使用 cosmosTemplate 处理这样的查询?这样的处理是否可行?

附注:我们使用的是 com.microsoft.azure:azure-cosmosdb-spring-boot-starter:2.2.5 库。

英文:

We are trying to implement a query with cosmosTemplate from spring-data-cosmosdb project.<br>
The query has the following syntax:<br>
&quot;select * from movie where ARRAY_CONTAINS(movie.countries, @country)&quot;. <br>
CosmosTemplate accepts DocumentQuery object, that is build upon Criteria object. Criteria object supports a small subset of basic predicates like in, or, is equal and etc., but doesn't have array_contains predicate. <br>
At the moment the query is performed by using cosmos client(from sdk), instead of cosmosTemplate.
This brings us two issues:

  1. We have to mix the code by using cosmosTemplate and cosmos client together.
  2. Since we have complex parameterized queries that use system functions, we have to concatenate sql query string and gather sql parameters.

How queries like this should be handled with cosmosTemplate and is that even possible?

P.S we are using com.microsoft.azure:azure-cosmosdb-spring-boot-starter:2.2.5 library.

答案1

得分: 2

在当前的GA版本中,您将需要像您提到的那样同时使用Client和模板。

最新的Beta版本包括对QueryAnnotation的支持,可在存储库中使用带注释的查询。以下是一个示例:

@Query(value = "select * from c where c.firstName = @firstName and c.lastName = @lastName")
List<User> getUsersByTitleAndValue(@Param("firstName") int firstName, @Param("lastName") String lastName);
英文:

In the current GA release you will have to use the Client and template together like you mentioned.

The latest beta release includes support for QueryAnnotation Using annotated queries in repositories. Following is an example:

@Query(value = &quot;select * from c where c.firstName = @firstName and c.lastName = @lastName&quot;)
List&lt;User&gt; getUsersByTitleAndValue(@Param(&quot;firstName&quot;) int firstName, @Param(&quot;lastName&quot;) String lastName);

答案2

得分: 2

Ravi的回答是正确的。要直接从Spring Data连接器创建自定义查询,目前有两个选项。

首先,您可以按照此文档中“自定义查询”部分的说明:

https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/how-to-guides-spring-data-cosmosdb

其中引导您直接使用Java SDK CosmosClient。当前的Spring Data连接器的GA版本没有@Query注释,该注释可以启用自定义查询注释,这就是为什么您需要直接使用Java SDK的原因。

其次,升级到最新的beta版本,该版本启用了@Query注释:

https://mvnrepository.com/artifact/com.azure/azure-spring-data-cosmos

关于此的示例代码将在接下来的几天内发布,GA版本计划于9/30发布,所以不需要等待太长时间。

英文:

Ravi's answer is correct. To create custom queries directly from Spring Data connector, there are current two options.

First, you can follow the instructions in the "Custom Query" section of this document

https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/how-to-guides-spring-data-cosmosdb

which guide you through using the Java SDK CosmosClient directly. The current GA release of Spring Data connector does not have @Query annotation which would enable custom query annotation, that is why you need to use the Java SDK directly.

Second, upgrade to the latest beta which enables @Query annotation

https://mvnrepository.com/artifact/com.azure/azure-spring-data-cosmos

Sample code for this will be released in the next few days, and the GA release is scheduled for 9/30 so it is not a long wait.

答案3

得分: 0

经过在 azure sdk 中的研究,我发现目前 cosmosTemplate 并不支持所有系统函数。<br> 可以在以下位置找到支持的系统函数列表:

https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/query/CriteriaType.java

如果您想编写查询使用不在上述链接列表中的系统函数,可以使用 @Query 注解或直接使用 cosmos 客户端,而不是使用 cosmosTemplate。

英文:

After a research in azure sdk, I've found that at the moment not all system functions are supported by cosmosTemplate.<br>
List of supported system functions can be found there:

https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/query/CriteriaType.java

If you wan't to write query that uses system function, that isn't in the list in the link above, you can use either @Query annotation or directly use cosmos client instead of cosmosTemplate.

huangapple
  • 本文由 发表于 2020年9月21日 04:33:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63983384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定