在Hazelcast中,是否可以根据键的属性而不是值来查询IMap?所有Hazelcast示例都显示了按值查询.例如,对于具有字符串键的员工的地图:
IMap<String, Employee> employees;
Run Code Online (Sandbox Code Playgroud)
然后,典型的搜索谓词将根据员工属性(姓名,薪水等)进行搜索.但我的情况使用更复杂的键,例如:
IMap<DataAttributes, DataValue> myData;
Run Code Online (Sandbox Code Playgroud)
因此,如果DataAttributes具有以下字段:
class DataAttributes {
String theDescription;
Date theStartTime;
public String getDescription() { return theDescription; }
// etc....
}
Run Code Online (Sandbox Code Playgroud)
我想编写一个可以通过键查询的谓词,以返回一个合适的DataValue对象.这不起作用:
Predicate pred = Predicates.equal("description", "myDescription");
myData.keySet(pred); // Throws IllegalArgumentException: "There is no suitable accessor for..."
Run Code Online (Sandbox Code Playgroud)
我可以按照这个答案的建议滚动自己,但如果可以,我宁愿使用开箱即用的解决方案.
如果我最终使用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));
Run Code Online (Sandbox Code Playgroud)
请注意,您可以通过访问器方法("getter")或字段名称来引用类成员,如上面的示例代码所示.我在hazelcast.org的"掌握Hazelcast"在线书籍中找到了这些信息(但您必须填写一份注册表才能访问它).