简体   繁体   English

Hazelcast IMap以二进制形式获取

[英]Hazelcast IMap get as binary

Is there any way I can retrieve the entry as bytes from a Hazelcast map? 有什么方法可以从Hazelcast映射中以字节为单位检索条目? We have an intermediate (Hz) message queue, where we would offer the map entry (value) through a local entry listener. 我们有一个中间(Hz)消息队列,在这里我们将通过本地条目侦听器提供映射条目(值)。

To reduce the (de)serialization cost, we are passing byte[] to the Hz queue. 为了降低(反序列化)成本,我们将byte []传递给Hz队列。 However, in order to do so we have to manually deserialize the entry object again before adding to the queue. 但是,为此,我们必须在添加到队列之前再次手动反序列化入口对象。 It is the same object that is being passed and I was thinking of some way to reduce this 'redundant' deserialization. 传递的对象是同一对象,我正在考虑某种方法来减少这种“冗余”反序列化。

You can do it by using internal APIs but those might change at any time in the future. 您可以使用内部API来实现,但是将来可能随时更改。 I can give you an example but I'm not sure this is what you want to use. 我可以举一个例子,但是我不确定这是您要使用的东西。

edit: As mentioned before this uses a lot of internal APIs so PLEASE be aware of the fact it might change at any time (even without mentioning): 编辑:如前所述,它使用了很多内部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");
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM