我有它使用索引的查詢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();
}
}
上午建議如果有一種方法