1
votes

I have a Hazelcast instance that organizes data based on map names. The map names are dynamic, so I do not know what they will be until the Hazelcast instance is already started. I now want to store this data in a database via the MapStore functionality, but retain the organization I have setup with the map names. When looking at the MapStore functionality, I do not see any way to retrieve the map or map name that the object came from. Looks like all I get is the key-value pair in the MapStore implementation.

On a broader note, is there any way to get ANY information (not just map name) about the key-value pair needing to be stored? I would like to transfer some knowledge about how to store the data... I know the information when I call map.put(..), but I do not know how to transfer that information to the MapStore call...

2
could you add extra information to the key?MarianP

2 Answers

2
votes

I just needed something similar and found that you can implement com.hazelcast.core.MapStoreFactory interface whose 'newMapStore' method gives you the map name and properties from config. From there 'new' your own MapStore implementation passing the map name.

public class MyMapStoreFactory implements MapStoreFactory {

    @Override
    public MapStore newMapStore(String mapName, Properties properties) {
        return new MyMapStoreImplementation(mapName, properties);
    }
}

And configure this factory class in hazelcast.xml config like this:

<map name="MapX*">
    <map-store enabled="true" initial-mode="EAGER">
        <factory-class-name>MyMapStoreFactory</factory-class-name>
    </map-store>
</map>

Notice the wildcard on the map name and that <class-name> element must not appear once you set <factory-class-name>.

Tested this with Hazelcast 3.6.1 and it works very fine.

0
votes

As per my understanding, there is no support out of the box in hazelcast. The following are couple of workarounds I can think of:

  1. Encapsulate the extra information (map name, how to store the data etc) in a context object and store it in a different java map against your key. Use this map in your MapStore implementation later to retrieve respective information which will help you to persist your key,value pairs.

You put operation might look like.

hzMap.put(key, value);
Context context = new Context();
context.setHowToStoreData();
context.setMapName();
// any othe rother information
context.xxx();
// create a unique context key which can be later deduced from (key,value) pair.
contextKey = getUniqueContextKey(key, value);
contextMap.put(contextKey, context);

In your MapStore implementation, you can then make use of this contextMap to retrieve additional values.

  1. Second way would be to encapsulate the information within the (key, value) pair. You can create a new class called CacheEntry to store cache values as well as additional information. You can then later retrieve your cache value as well as additional information from iMap itself.

You put operation might look like.

CacheEntry<YourValueClass> cacheEntry = new CacheEntry<YourValueClass>();
cacheEntry.setValue(value);
cacheEntry.howToStoreData(..);
cacheEntry.setMapName(..);
imap.put(key, cacheEntry);

In your MapStore implementation, you can then make use of the value (which would be a CacheEntry object) itself to retrieve additional information as well as actual value (instance of YourValueClass).