2017-08-09 64 views
0

我有它使用索引的查詢Maploader延遲初始化與索引

public class Customer implements Serializable { 

    private String id; 
    private String name; 

    public Customer(String id, String name) { 
     super(); 
     this.id = id; 
     this.name = name; 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

} 
  • Maploader maploader

    公共類SimpleMapLoader實現MapLoader { 公衆客戶負載(整數鍵){ 的System.out.println( 「負載」); 返回null; }

    public Map<Integer, Customer> loadAll(Collection<Integer> keys) { 
        System.out.println("loadAll"); 
        Map<Integer, Customer> map = new HashMap<Integer, Customer>(); 
    
        for (Integer k : keys) { 
         map.put(k, new Customer(String.valueOf(k), String.valueOf(k))); 
        } 
    
        return map; 
    } 
    
    public Iterable<Integer> loadAllKeys() { 
        System.out.println("loadallkeys"); 
        List<Integer> map = new ArrayList<Integer>(); 
        for (int i = 0; i < 10; i++) { 
         map.add(i); 
        } 
    
        return map; 
    } 
    

    }

主要

public class Main { 

    public static void main(String[] args) { 

     Config config = new Config(); 
     HazelcastInstance instance = Hazelcast.newHazelcastInstance(config); 

     final MapConfig customerMapConf = config.getMapConfig("customers"); 
     final MapStoreConfig customerMapStoreConf = customerMapConf.getMapStoreConfig(); 
     customerMapStoreConf.setImplementation(new SimpleMapLoader()); 
     customerMapStoreConf.setEnabled(true); 
     customerMapConf.setMapStoreConfig(customerMapStoreConf); 
     config.addMapConfig(customerMapConf); 
     Map<Integer, String> map = instance.getMap("customers"); 
     instance.shutdown(); 
    } 

} 

這工作絕對沒問題地圖將不會被加載,直到我觸摸地圖首次。

但是當我爲地圖添加索引時,無論是否觸摸地圖,地圖都會加載。 來自Hazelcast文檔 類MapStoreConfig中的InitialLoadMode配置參數有兩個值:LAZY和EAGER。如果InitialLoadMode設置爲LAZY,則在地圖創建過程中不會加載數據。如果它被設置爲EAGER,那麼所有的數據都會在地圖創建時加載,並且所有的東西都可以使用。另外,如果使用MapIndexConfig類或addIndex方法向您的地圖添加索引,則重寫InitialLoadMode,並且MapStoreConfig的行爲與EAGER模式一樣。

但是有沒有什麼辦法可以覆蓋這個Eager InitialLoadMode行爲。 我添加索引通過以下方式使用hazelcast 3.7.4.Please

public class Main { 

    public static void main(String[] args) { 

     Config config = new Config(); 
     HazelcastInstance instance = Hazelcast.newHazelcastInstance(config); 

     final MapConfig customerMapConf = config.getMapConfig("customers"); 
     final MapStoreConfig customerMapStoreConf = customerMapConf.getMapStoreConfig(); 
     customerMapStoreConf.setImplementation(new SimpleMapLoader()); 
     customerMapStoreConf.setEnabled(true); 
     customerMapConf.setMapStoreConfig(customerMapStoreConf); 
     config.addMapConfig(customerMapConf); 
     MapIndexConfig indexConfig = new MapIndexConfig(); 
     indexConfig.setAttribute("name"); 
     indexConfig.setOrdered(false); 
     customerMapConf.setMapIndexConfigs(Arrays.asList(indexConfig)); 
     customerMapStoreConf.setInitialLoadMode(InitialLoadMode.LAZY); 
     Map<Integer, String> map = instance.getMap("customers"); 

     // System.out.println(map.get("1")); 

     instance.shutdown(); 
    } 

} 

上午建議如果有一種方法

回答

0

由於文件說,初始加載模式被覆蓋,當你創建索引設置爲EAGER 。您可以通過分支您自己的Hazelcast代碼分支來修改生產行爲,修改您想要的並重建它。但是,這可能會對系統的其他區域產生負面影響(例如,由於分區線程被佔用時間過長而導致負載緩慢),因此您需要注意存在不受支持版本的風險。

0

正如醫生說 The InitialLoadMode configuration parameter in the class MapStoreConfig has two values: LAZY and EAGER. If InitialLoadMode is set to LAZY, data is not loaded during the map creation. If it is set to EAGER, all the data is loaded while the map is created, and everything becomes ready to use. Also, if you add indices to your map with the MapIndexConfig class or the addIndex method, then InitialLoadMode is overridden and MapStoreConfig behaves as if EAGER mode is on. http://docs.hazelcast.org/docs/3.8/manual/html-single/index.html