Class GenericMapLoader<K>
- java.lang.Object
-
- com.hazelcast.mapstore.GenericMapLoader<K>
-
- Type Parameters:
K
-
- All Implemented Interfaces:
MapLoader<K,GenericRecord>
,MapLoaderLifecycleSupport
- Direct Known Subclasses:
GenericMapStore
public class GenericMapLoader<K> extends java.lang.Object implements MapLoader<K,GenericRecord>, MapLoaderLifecycleSupport
GenericMapLoader is an implementation ofMapLoader
built on top of Hazelcast SQL engine.It works with any SQL connector supporting SELECT statements.
Usage:
First define data connection, e.g. for JDBC use
JdbcDataConnection
:Config config = new Config(); config.addDataConnectionConfig( new DataConnectionConfig("mysql-ref") .setType("Jdbc") .setProperty("jdbcUrl", dbConnectionUrl) );
Then create a Map with
MapLoader
using the GenericMapLoader implementation:MapConfig mapConfig = new MapConfig(mapName); MapStoreConfig mapStoreConfig = new MapStoreConfig(); mapStoreConfig.setClassName(GenericMapLoader.class.getName()); mapStoreConfig.setProperty(GenericMapLoader.DATA_CONNECTION_REF_PROPERTY, "mysql-ref"); mapConfig.setMapStoreConfig(mapStoreConfig); instance().getConfig().addMapConfig(mapConfig);
The GenericMapLoader creates a SQL mapping with name "__map-store." + mapName. This mapping is removed when the map is destroyed.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<SqlColumnMetadata>
columnMetadataList
static java.lang.String
COLUMNS_PROPERTY
Property key to define column names in databasestatic java.lang.String
DATA_CONNECTION_REF_PROPERTY
Property key to define data connectionstatic java.lang.String
EXTERNAL_NAME_PROPERTY
Property key to define external name of the tableprotected com.hazelcast.mapstore.GenericMapStoreProperties
genericMapStoreProperties
static java.lang.String
ID_COLUMN_PROPERTY
Property key to define id column name in databasestatic java.lang.String
LOAD_ALL_KEYS_PROPERTY
Property key to control loading of all keys when IMap is first createdstatic HazelcastProperty
MAPSTORE_INIT_TIMEOUT
Timeout for initialization of GenericMapLoaderprotected com.hazelcast.mapstore.Queries
queries
protected SqlService
sqlService
static java.lang.String
TYPE_NAME_PROPERTY
Property key to data connection type name
-
Constructor Summary
Constructors Constructor Description GenericMapLoader()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
awaitSuccessfulInit()
Awaits successful initialization; if the initialization fails, throws an exception.void
destroy()
Hazelcast will call this method before shutting down.void
init(HazelcastInstance instance, java.util.Properties properties, java.lang.String mapName)
Initializes this MapLoader implementation.GenericRecord
load(K key)
Loads the value of a given key.java.util.Map<K,GenericRecord>
loadAll(java.util.Collection<K> keys)
Size of thekeys
collection is limited byClusterProperty.MAP_LOAD_CHUNK_SIZE
java.lang.Iterable<K>
loadAllKeys()
Loads all of the keys from the store.
-
-
-
Field Detail
-
DATA_CONNECTION_REF_PROPERTY
public static final java.lang.String DATA_CONNECTION_REF_PROPERTY
Property key to define data connection- See Also:
- Constant Field Values
-
EXTERNAL_NAME_PROPERTY
public static final java.lang.String EXTERNAL_NAME_PROPERTY
Property key to define external name of the table- See Also:
- Constant Field Values
-
ID_COLUMN_PROPERTY
public static final java.lang.String ID_COLUMN_PROPERTY
Property key to define id column name in database- See Also:
- Constant Field Values
-
COLUMNS_PROPERTY
public static final java.lang.String COLUMNS_PROPERTY
Property key to define column names in database- See Also:
- Constant Field Values
-
TYPE_NAME_PROPERTY
public static final java.lang.String TYPE_NAME_PROPERTY
Property key to data connection type name- See Also:
- Constant Field Values
-
LOAD_ALL_KEYS_PROPERTY
public static final java.lang.String LOAD_ALL_KEYS_PROPERTY
Property key to control loading of all keys when IMap is first created- See Also:
- Constant Field Values
-
MAPSTORE_INIT_TIMEOUT
public static final HazelcastProperty MAPSTORE_INIT_TIMEOUT
Timeout for initialization of GenericMapLoader
-
sqlService
protected SqlService sqlService
-
genericMapStoreProperties
protected com.hazelcast.mapstore.GenericMapStoreProperties genericMapStoreProperties
-
queries
protected com.hazelcast.mapstore.Queries queries
-
columnMetadataList
protected java.util.List<SqlColumnMetadata> columnMetadataList
-
-
Method Detail
-
init
public void init(HazelcastInstance instance, java.util.Properties properties, java.lang.String mapName)
Description copied from interface:MapLoaderLifecycleSupport
Initializes this MapLoader implementation. Hazelcast will call this method when the map is first used on the HazelcastInstance. Implementation can initialize required resources for the implementing mapLoader, such as reading a config file and/or creating a database connection. References to maps, other than the one on which thisMapLoader
is configured, can be obtained from thehazelcastInstance
in this method's implementation.On members joining a cluster, this method is executed during finalization of the join operation, therefore care should be taken to adhere to the rules for
PostJoinAwareService.getPostJoinOperation()
. If the implementation executes operations which may wait on locks or otherwise block (e.g. waiting for network operations), this may result in a time-out and obstruct the new member from joining the cluster. If blocking operations are required for initialization of theMapLoader
, consider deferring them with a lazy initialization scheme.- Specified by:
init
in interfaceMapLoaderLifecycleSupport
- Parameters:
instance
- HazelcastInstance of this mapLoader.properties
- Properties set for this mapStore. see MapStoreConfigmapName
- name of the map.
-
destroy
public void destroy()
Description copied from interface:MapLoaderLifecycleSupport
Hazelcast will call this method before shutting down. This method can be overridden to clean up the resources held by this map loader implementation, such as closing the database connections, etc.- Specified by:
destroy
in interfaceMapLoaderLifecycleSupport
-
load
public GenericRecord load(K key)
Description copied from interface:MapLoader
Loads the value of a given key. If distributed map doesn't contain the value for the given key then Hazelcast will call implementation's load (key) method to obtain the value. Implementation can use any means of loading the given key; such as an O/R mapping tool, simple SQL or reading a file etc.- Specified by:
load
in interfaceMapLoader<K,GenericRecord>
- Parameters:
key
- , cannot benull
- Returns:
- value of the key; returning
null
value signals value missing in the underlying store
-
loadAll
public java.util.Map<K,GenericRecord> loadAll(java.util.Collection<K> keys)
Size of thekeys
collection is limited byClusterProperty.MAP_LOAD_CHUNK_SIZE
- Specified by:
loadAll
in interfaceMapLoader<K,GenericRecord>
- Parameters:
keys
- keys of the values entries to load- Returns:
- map of loaded key-value pairs.
-
loadAllKeys
public java.lang.Iterable<K> loadAllKeys()
Description copied from interface:MapLoader
Loads all of the keys from the store. The returnedIterable
may return the keys lazily by loading them in batches. TheIterator
of thisIterable
may implement theCloseable
interface in which case it will be closed once iteration is over. This is intended for releasing resources such as closing a JDBC result set.The returned Iterable should not contain any
null
keys.- Specified by:
loadAllKeys
in interfaceMapLoader<K,GenericRecord>
- Returns:
- all the keys. Keys inside the Iterable cannot be
null
.
-
awaitSuccessfulInit
protected void awaitSuccessfulInit()
Awaits successful initialization; if the initialization fails, throws an exception.
-
-