2015-04-23 109 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

我真的很喜歡看到它。那麼,正如你所暗示的那樣,如果它看起來足夠嚇人,我會回到我的'冗餘':)。並感謝您的迴應 –