如何在IMap中使用Hazelcast MapStore数据

huangapple 未分类评论43阅读模式
英文:

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&lt;String, Person&gt; {

    private final Connection con;
    private final PreparedStatement allKeysStatement;

    
    public PersonMapStore() throws ClassNotFoundException {
        try {
        	System.out.println(&quot;hello&quot;);
        	Class.forName(&quot;org.postgresql.Driver&quot;);
            con = DriverManager.getConnection(&quot;jdbc:&quot;, &quot;&quot;, &quot;&quot;);
            con.createStatement().executeUpdate(
                    &quot;create table if not exists person (id varchar(45) not null, name varchar(45), primary key (id))&quot;);
            allKeysStatement = con.prepareStatement(&quot;SELECT * FROM public.person&quot;);
        } 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(&quot;data&quot;);
		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&lt;String, Person&gt; hazelInstance = hazelCast.getMap(&quot;data&quot;);
		 
		 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&lt;Integer, HazelcastJsonValue&gt; hazelInstance = hazelCast.getMap(&quot;data&quot;);

So that i can perform all the query operation using IMap..

How to configure the MapStore and use the MapStore in the IMap?

huangapple
  • 本文由 发表于 2020年7月25日 02:18:45
  • 转载请务必保留本文链接:https://java.coder-hub.com/63079349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定