英文:
How to use Hazelcast MapStore data in the IMap
问题
我在我的应用程序中使用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);
// 如果需要,可以使用近期缓存
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());
// 虽然我在数据库中有2行数据,但这里显示大小为0
}
}
因此,现在如何将所有的MapStore数据传输/存储到Hazelcast中
HazelcastInstance hazelCast = Hazelcast.newHazelcastInstance();
IMap<Integer, HazelcastJsonValue> hazelInstance = hazelCast.getMap("data");
这样,我就可以使用IMap执行所有的查询操作了。
如何配置MapStore并在IMap中使用MapStore呢?
英文:
I'm using hazelcast in my application..
In the first step I'm loading all the table data into 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);
}
}
model class
public class Person {
private String id;
private String name;
private HazelcastJsonValue object_value;
public Person() {
super();
// TODO Auto-generated constructor stub
}
MapStore Configurartion
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
}
}
So now how to transfer/Store all the MapStore data into the Hazelcast
HazelcastInstance hazelCast = Hazelcast.newHazelcastInstance();
IMap<Integer, HazelcastJsonValue> hazelInstance = hazelCast.getMap("data");
So that i can perform all the query operation using IMap..
How to configure the MapStore and use the MapStore in the IMap?
专注分享java语言的经验与见解,让所有开发者获益!
评论