我正在使用Hazelcast和mysql 我在mysql中为我的表创建了一个mapstore。我想要做的是使用data.loadAll从数据库填充数据。我的地图商店实现就是这个
public class CityMapStore
implements MapStore<Long, City>
{
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<Long> keys )
{
for ( Long key : keys )
delete( key );
}
public synchronized void storeAll( Map<Long, City> map )
{
for ( Map.Entry<Long, City> entry : map.entrySet() )
store( entry.getKey(), entry.getValue() );
}
public synchronized Map<Long, City> loadAll( Collection<Long> keys )
{
Map<Long, City> result = new HashMap<Long, City>();
for ( Long key : keys )
result.put( key, load( key ) );
return result;
}
public Set<Long> loadAllKeys()
{
return null;
}
}
我的装载等级是这个
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<Long, City> 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<Long, City> 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;
}
}
答案 0 :(得分:0)
要在启动时加载数据首先覆盖loadAllKeys方法,或者如果您不想要它,请使用选定的密钥加载oveloaded loadAll方法