簡體   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