在 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..."
我可以按照 this answer 中的建议自行滚动,但如果可以的话,我宁愿使用开箱即用的解决方案。
我最终使用的是 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”)或字段名称来引用类成员,如您在上面的示例代码中所见。我在 "Mastering Hazelcast" 中找到了此信息在线图书,可在 hazelcast.org 获取(但您必须填写注册表才能访问它)。
关于java - 可以在 Hazelcast 中通过键而不是值进行查询(使用谓词)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30486837/