我在应用程序中使用了hazelcast。
第一步,我将所有表数据加载到MapStore中。
public class PersonMapStore implements MapStore<String, Person> {
private final Connection con;
private final PreparedStatement allKeysStatement;
public PersonMapStore() throws ClassNotFoundException {
try {
System.out.println("hello");
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection("jdbc:", "", "");
con.createStatement().executeUpdate(
"create table if not exists person (id varchar(45) not null, name varchar(45), primary key (id))");
allKeysStatement = con.prepareStatement("SELECT * FROM public.person");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
模型类
public class Person {
private String id;
private String name;
private HazelcastJsonValue object_value;
public Person() {
super();
// TODO Auto-generated constructor stub
}
MapStore配置
public void mapConf() {
Config config = new Config();
MapConfig mapCfg = new MapConfig();
mapCfg.setName("data");
mapCfg.setBackupCount(2);
mapCfg.getMaxSizeConfig().setSize(10000);
mapCfg.setTimeToLiveSeconds(300);
MapStoreConfig mapStoreCfg = new MapStoreConfig();
mapStoreCfg.setClassName(PersonMapStore.class.getName()).setEnabled(true);
mapCfg.setMapStoreConfig(mapStoreCfg);
// use near cache if needed
NearCacheConfig nearCacheConfig = new NearCacheConfig();
nearCacheConfig.setMaxSize(1000).setMaxIdleSeconds(120).setTimeToLiveSeconds(300);
mapCfg.setNearCacheConfig(nearCacheConfig);
config.addMapConfig(mapCfg);
HazelcastInstance hazelCast = Hazelcast.newHazelcastInstance(config);
IMap<String, Person> hazelInstance = hazelCast.getMap("data");
System.out.println(hazelInstance.size());
//giving size 0 but i have 2 rows data in the database
}
}
所以现在如何将所有MapStore数据传输/存储到Hazelcast中
HazelcastInstance hazelCast = Hazelcast.newHazelcastInstance();
IMap<Integer, HazelcastJsonValue> hazelInstance = hazelCast.getMap("data");
这样我就可以使用IMap执行所有查询操作。
如何在IMap中配置MapStore和使用MapStore?