繁体   English   中英

可以通过密钥而不是Hazelcast中的值进行查询(使用Predicates)?

[英]Possible to query by key instead of value in Hazelcast (using Predicates)?

在Hazelcast中,是否可以根据键的属性而不是值来查询IMap? 所有Hazelcast示例都显示了按值查询。 例如,对于具有字符串键的员工的地图:

IMap<String, Employee> employees;

然后,典型的搜索谓词将根据员工属性(姓名,薪水等)进行搜索。 但我的情况使用更复杂的键,例如:

IMap<DataAttributes, DataValue> myData;

因此,如果DataAttributes具有以下字段:

 class DataAttributes {
     String theDescription;
     Date   theStartTime;
     public String getDescription() { return theDescription; }
     // etc....
 }

我想编写一个可以通过键查询的谓词,以返回一个合适的DataValue对象。 这不起作用:

Predicate pred = Predicates.equal("description", "myDescription");
myData.keySet(pred);  // Throws IllegalArgumentException: "There is no suitable accessor for..."

我可以按照这个答案的建议滚动自己,但如果可以,我宁愿使用开箱即用的解决方案。

如果我最终使用Criteria API或Distributed SQL Query API并不重要。 任何有效的查询都会很棒。 适用于嵌套属性的解决方案的加分点(即:DataAttributes theStartTime.getYear() )。

可以使用PredicateBuilder( com.hazelcast.query.PredicateBuilder )。 PredicateBuilder范例允许您基于键进行查询,如下所示:

EntryObject eo = new PredicateBuilder().getEntryObject();
Predicate fredWithStartTimeThisYear = eo.key().get("Description").equal("Fred")
  .and(eo.key().get("theStartTime.Year").equal(2015));

请注意,您可以通过访问器方法(“getter”)或字段名称来引用类成员,如上面的示例代码所示。 我在hazelcast.org“掌握Hazelcast”在线书籍中找到了这些信息(但您必须填写一份注册表才能访问它)。

暂无
暂无

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

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