2015-04-23 97 views
0

有没有什么方法可以从Hazelcast地图中检索条目?我们有一个中间(Hz)消息队列,我们​​通过本地入口监听器提供地图条目(值)。Hazelcast IMap以二进制形式获取

为了减少(反)序列化成本,我们将byte []传递给Hz队列。但是,为了这样做,我们必须在添加到队列之前再次手动反序列化条目对象。这是正在通过的同一个对象,我正在想办法减少这种“冗余”的反序列化。

回答

0

您可以通过使用内部API来实现,但这些API可能会在将来随时更改。我可以给你一个例子,但我不确定这是你想用的。

编辑:提到这个使用了大量的内部API之前,所以请知道的事实它可能在任何时候(即使不提)改变:

public class Test { 
    public static void main(String[] args) throws Exception { 
    HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(); 

    IMap<String, String> map = hazelcastInstance.getMap("myMapName"); 

    String key = "mySearchedKey"; 
    map.put(key, "foo"); 

    SerializationService serializationService = getSerializationService(map); 
    Data keyData = serializationService.toData(key); 

    RecordStore recordStore = getRecordStore(map, key); 
    Record record = recordStore.getRecord(keyData); 

    // This will do no serialization in most cases since Data is a DataRecord 
    // only for InMemoryFormat::OBJECT this will serialize the object to a Data 
    // instance 
    Data serializedValue = serializationService.toData(record.getValue()); 
    } 

    /** 
    * This method handles internal APIs!!! 
    */ 
    private static SerializationService getSerializationService(IMap<String, String> map) { 
    MapProxyImpl<String, String> proxy = (MapProxyImpl<String, String>) map; 
    return proxy.getNodeEngine().getSerializationService(); 
    } 

    /** 
    * This method handles internal APIs!!! 
    */ 
    private static RecordStore getRecordStore(IMap<String, String> map, String key) throws Exception { 
    MapProxyImpl<String, String> proxy = (MapProxyImpl<String, String>) map; 
    HazelcastInstance hazelcastInstance = proxy.getNodeEngine().getHazelcastInstance(); 

    MapService mapService = proxy.getService(); 
    MapServiceContext mapServiceContext = mapService.getMapServiceContext(); 

    PartitionService partitionService = hazelcastInstance.getPartitionService(); 
    Partition partition = partitionService.getPartition(key); 

    PartitionContainer partitionContainer = mapServiceContext.getPartitionContainer(partition.getPartitionId()); 
    return partitionContainer.getRecordStore("myMapName"); 
    } 
} 
+0

我真的很喜欢看到它。那么,正如你所暗示的那样,如果它看起来足够吓人,我会回到我的'冗余':)。并感谢您的回应 –