2017-09-13 69 views
0

使用案例: IMAP和具有70K條目。多數操作都是GET呼叫多個密鑰。 GET調用(90%)> PUT調用(10%)。我們使用EP進行PUT調用。Hazelcast IMap - 什麼可以爲多個密鑰獲取價值最有效的方式?

問題:什麼可能是最有效的方式來獲得多個按鍵,可以在多個實例存在的數據?

可能的解決方案: 1. EP與只讀,Offloadable和使用executeOnKeys方法。 2.對所有鍵並行執行map.get(key)。

是否有任何其他有效的方式來獲得多個密鑰的數據?

  • 謂詞

    公共類ExecuteOnSelectedKeyPredicateTest { 公共靜態無效的主要(字串[] args){ HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();

     IMap<String, Integer> map = hazelcastInstance.getMap("testMap"); 
    
         map.put("testKey1", 1); 
         map.put("test", 2); 
         map.put("testKey3", 3); 
    
         // Get entries which ends with a number 
         Set<Map.Entry<String, Integer>> entries = map.entrySet(new ExecuteOnSelectedKeyPredicate("^.+?\\d$")); 
    
         System.out.println(entries); 
        } 
    
        public static class ExecuteOnSelectedKeyPredicate implements Predicate<String, Integer> { 
    
         private String keyRegex; 
         private transient Pattern pattern; 
    
         public ExecuteOnSelectedKeyPredicate(String keyRegex) { 
          this.keyRegex = keyRegex; 
         } 
    
    
         @Override 
         public boolean apply(Map.Entry<String, Integer> mapEntry) { 
          if (pattern == null) { 
           pattern = Pattern.compile(keyRegex); 
          } 
          Matcher matcher = pattern.matcher(mapEntry.getKey()); 
          return matcher.matches(); 
         } 
        } 
    } 
    
  • 回答

    1

    獲取多個密鑰請求有可能去多個成員。 executeOnKeys在這裏非常適合,因爲它計算了操作需要在其上執行的分區,並且僅將這些操作發送到這些分區。 Offloadable確保您不會阻止partitionThreads,並且ReadOnly會進一步優化處理。

    get操作會產生更多的網絡流量,因爲你必須每一個關鍵操作。

    +0

    @tombujok - 你覺得謂詞會比E.P.表現得更好實施?我已經添加了上面的代碼。 – Hiten

    +0

    @tombujok - IMap還有另外1個實現 - getAll(設置爲)。你認爲EP實施會更有效嗎? – Hiten

    +0

    Hiten,如果您執行的唯一邏輯是讀取比getAll應該非常相似。就發送操作的數量而言,它與executeOnKeys的工作方式相同,所以應該沒什麼區別。我記得你的用例也有寫邏輯,因此使用EP來建立一個統一的邏輯可能會更好。 –

    相關問題