hazelcast mysql,Mapstore无法从数据库Hazelcast加载数据

I am using Hazelcast and mysql.

I created a mapstore for my table in mysql. What i am trying to do is to populate data using map.loadAll from data base. My Map store implementation is this

public class CityMapStore

implements MapStore

{

private final Connection con;

public CityMapStore() {

try {

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hazel", "root", "root123");

con.createStatement().executeUpdate(

"create table if not exists City (id bigint, name varchar(45), primary key (id))");

} catch (SQLExpublic synchronized void delete(Long key) {

System.out.println("Delete:" + key);

try {

con.createStatement().executeUpdate(

String.format("delete from City where id = %s", key));

} catch (SQLException e) {

throw new RuntimeException(e);

}

}ception e) {

throw new RuntimeException(e);

}

}

public synchronized void delete( Long key )

{

System.out.println( "Delete:" + key );

try

{

con.createStatement().executeUpdate( String.format( "delete from City where id = %s", key ) );

}

catch ( SQLException e )

{

throw new RuntimeException( e );

}

}

public synchronized void deleteAll( Collection keys )

{

for ( Long key : keys )

delete( key );

}

public synchronized void storeAll( Map map )

{

for ( Map.Entry entry : map.entrySet() )

store( entry.getKey(), entry.getValue() );

}

public synchronized Map loadAll( Collection keys )

{

Map result = new HashMap();

for ( Long key : keys )

result.put( key, load( key ) );

return result;

}

public Set loadAllKeys()

{

return null;

}

}

My Load Class is this

public class LoadAllCity

{

public static void main( String[] args )

{

final Long numberOfEntriesToAdd = 1000l;

final String mapName = LoadAllCity.class.getCanonicalName();

final Config config = createNewConfig( mapName );

final HazelcastInstance node = Hazelcast.newHazelcastInstance( config );

final IMap map = node.getMap( mapName );

populateMap( map, numberOfEntriesToAdd );

System.out.printf( "# After populating map size\t: %d\n", map.size() );

System.out.printf( "# Map store has %d elements\n", numberOfEntriesToAdd );

map.evictAll();

System.out.printf( "# After evictAll map size\t: %d\n", map.size() );

map.loadAll( true );

System.out.printf( "# After loadAll map size\t: %d\n", map.size() );

}

private static void populateMap( IMap map, Long itemCount )

{

for ( long i = 0; i < itemCount; i++ )

{

City city = new City( "Viren" + i );

// map.put(i, city);

}

}

private static Config createNewConfig( String mapName )

{

final CityMapStore cityStore = new CityMapStore();

XmlConfigBuilder configBuilder = new XmlConfigBuilder();

Config config = configBuilder.build();

MapConfig mapConfig = config.getMapConfig( mapName );

MapStoreConfig mapStoreConfig = new MapStoreConfig();

mapStoreConfig.setClassName( CityMapStore.class.getCanonicalName() );

mapStoreConfig.setImplementation( cityStore );

mapStoreConfig.setWriteDelaySeconds( 0 );

mapConfig.setMapStoreConfig( mapStoreConfig );

return config;

}

}

解决方案

To load data at start up override loadAllKeys method first or if you do not want it than use selected keys to load with oveloaded loadAll method

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值