简体   繁体   English

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

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

In Hazelcast, is it possible to query an IMap based on attributes of a key instead of the values? 在Hazelcast中,是否可以根据键的属性而不是值来查询IMap? All the Hazelcast examples show querying by value. 所有Hazelcast示例都显示了按值查询。 Eg, for a map of employees with keys that are strings: 例如,对于具有字符串键的员工的地图:

IMap<String, Employee> employees;

The typical search predicates then search based on employee attributes (name, salary, etc). 然后,典型的搜索谓词将根据员工属性(姓名,薪水等)进行搜索。 But my case uses more complex keys, such as: 但我的情况使用更复杂的键,例如:

IMap<DataAttributes, DataValue> myData;

So if DataAttributes has fields such as: 因此,如果DataAttributes具有以下字段:

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

I want to write a predicate that can query by the keys, to return an appropriate DataValue object. 我想编写一个可以通过键查询的谓词,以返回一个合适的DataValue对象。 This does not work: 这不起作用:

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

I could roll-my-own as suggested in this answer , but I'd rather use an out-of-the-box solution if I can. 我可以按照这个答案的建议滚动自己,但如果可以,我宁愿使用开箱即用的解决方案。

It doesn't matter if I wind up using the Criteria API, or the Distributed SQL Query API. 如果我最终使用Criteria API或Distributed SQL Query API并不重要。 Any working query would be great. 任何有效的查询都会很棒。 Bonus points for a solution that works on nested attributes (ie: DataAttributes theStartTime.getYear() ). 适用于嵌套属性的解决方案的加分点(即:DataAttributes theStartTime.getYear() )。

It is possible using PredicateBuilder ( com.hazelcast.query.PredicateBuilder ). 可以使用PredicateBuilder( com.hazelcast.query.PredicateBuilder )。 The PredicateBuilder paradigm allows you to query based on keys, like so: PredicateBuilder范例允许您基于键进行查询,如下所示:

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

Note that you can refer to class members by accessor method ("getter") or field name, as you can see in the above example code. 请注意,您可以通过访问器方法(“getter”)或字段名称来引用类成员,如上面的示例代码所示。 I found this information in the "Mastering Hazelcast" online book, available at hazelcast.org (but you have to fill out a registration form to gain access to it). 我在hazelcast.org“掌握Hazelcast”在线书籍中找到了这些信息(但您必须填写一份注册表才能访问它)。

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

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