简体   繁体   English

使用 IN 运算符的数据存储查询

[英]Datastore query with IN operator

The new flexible environment datastore interface does not seem to support IN operation when running a query.运行查询时,新的柔性环境数据存储接口似乎不支持IN操作。 I hope that I'm wrong, and if so, how can one use an IN operator in the new Java interface of Datastore?我希望我错了,如果是这样,如何在 Datastore 的新 Java 接口中使用IN运算符?

A query like - WHERE color IN('RED', 'BLACK') , it is not supported by the Datastore (server side). WHERE color IN('RED', 'BLACK')这样的查询,数据存储区(服务器端)不支持。 Same is the case with OR operator (eg WHERE color='RED' OR color='BLACK' ). OR运算符也是如此(例如WHERE color='RED' OR color='BLACK' )。 Some client APIs have added this functionality by splitting the query into multiple and then merging the results from each query. 一些客户端API通过将查询分为多个然后合并每个查询的结果来添加此功能。 The new google-cloud-java API does not support this yet. 新的google-cloud-java API尚不支持此功能。 For now, you would have to run multiple queries for each value in the IN clause and merge the results. 现在,您将必须对IN子句中的每个值运行多个查询,然后合并结果。

Here's an example from the documentation : 这是文档中的示例:

If you want to set more than one filter on a query, you must use CompositeFilter , which requires at least two filters. 如果要对一个查询设置多个过滤器,则必须使用CompositeFilter ,它至少需要两个过滤器。

Filter tooShortFilter = new FilterPredicate("height", FilterOperator.LESS_THAN, minHeight);
Filter tooTallFilter = new FilterPredicate("height", FilterOperator.GREATER_THAN, maxHeight);
Filter heightOutOfRangeFilter = CompositeFilterOperator.or(tooShortFilter, tooTallFilter);
Query q = new Query("Person").setFilter(heightOutOfRangeFilter);

You can also use .and() . 您也可以使用.and() The code here is for Java 7. For Java 8 you can find a corresponding code in the documentation referenced above. 此处的代码适用于Java7。对于Java 8,您可以在上面引用的文档中找到相应的代码。 I hope that helps. 希望对您有所帮助。

Now to IN . 现在到IN While I have not tried it myself recently, the current documentation states that it can still be used as an operator. 虽然我最近没有尝试过自己,当前文档指出它仍然可以作为运营商。 According to it, something like the code below should work: 据此,类似以下代码的代码应该可以工作:

Filter propertyFilter = new FilterPredicate("height", FilterOperator.IN, minHeights);
Query q = new Query("Person").setFilter(propertyFilter);

Alternatively, you could use Google GQL . 或者,您可以使用Google GQL It will allow you to write SQL-like syntax, in which you can use in(...) . 它将允许您编写类似SQL的语法,您可以在其中使用in(...)

I tried using the repository query methods, but I got an error informing that it is not supported.我尝试使用存储库查询方法,但收到一条错误消息,提示它不受支持。

Only solved for me using the @Query annotation;仅使用@Query注释为我解决;

Example:例子:

  @Query("select * from UserGroup where name IN @names")
  List<Company> findAllByName(List<String> names);

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

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