Configuring a Map with the Generic MapStore
With the generic MapStore, you can configure a map to cache data in an external data store. This topic includes an example of how to configure a map with a generic MapStore that connects to a MySQL database.
For a list of all supported databases, see the built-in data store factories.
Before you Begin
You need a data store connection that’s configured on all cluster members.
Quickstart Configuration
This example shows a basic map configuration for an external datastore called my-mysql-database
. See Distributed Map for the details of other properties that you include in your map configuration.
<hazelcast>
...
<map name="default">
<map-store enabled="true">
<class-name>com.hazelcast.mapstore.GenericMapStore</class-name> (1)
<properties>
<property name="external-data-store-ref">my-mysql-database</property> (2)
</properties>
</map-store>
</map>
...
</hazelcast>
hazelcast:
map:
default:
map-store:
enabled: true
class-name: com.hazelcast.mapstore.GenericMapStore (1)
properties:
external-data-store-ref: my-mysql-database (2)
MapConfig mapConfig = new MapConfig("default");
MapStoreConfig mapStoreConfig = new MapStoreConfig();
mapStoreConfig.setClassName("com.hazelcast.mapstore.GenericMapStore"); (1)
mapStoreConfig.setProperty("external-data-store-ref", "my-mysql-database"); (2)
mapConfig.setMapStoreConfig(mapStoreConfig);
instance().getConfig().addMapConfig(mapConfig);
1 | The class name of the generic MapStore that’s built into Hazelcast. This implementation reads from the external data store configuration to create a SQL mapping. |
2 | The name of your external data store configuration. |
SQL Mapping for the Generic MapStore
When you configure a map with the generic MapStore, Hazelcast creates a SQL mapping with the JDBC connector. The name of the mapping is the same name as your map prefixed with __map-store.
. This mapping is used to read data from the data store or write data to the data store and it is removed whenever the configured map is removed. You can also configure this SQL mapping, using configuration properties.
Configuration Properties for the Generic MapStore
These configuration properties allow you to configure the generic MapStore and its SQL mapping.
As well as these properties, you can also configure general MapStore settings. |
Option | Description | Default | Example |
---|---|---|---|
The name of the configured external data store to use to set up a mapping. Use this property when you have an existing connection to an external data store. |
|
|
|
Name of the table to read from. |
The name of the map. |
|
|
SQL connector to use for the mapping. |
The SQL connector is derived from the external data store in the configuration. |
|
|
Name of the column that contains the primary key. |
|
|
|
Names of the columns to map. This value must include a subset of columns in the table. Missing columns must have a default value defined. |
|
Related Resources
-
To monitor MapStores for each loaded entry, use the
EntryLoadedListener
interface. See the Listening for Map Events section to learn how you can catch entry-based events.
Next Steps
See the MapStore configuration guide for details about configuring the MapStore and a list of configuration options.