本文主要是介绍Hazelcast--Map数据类型中文版之中篇,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
4.1.4 In Memory Format 内存存储模型
IMap 拥有可配置的内存存储格式.缺省的Hazelcast存储数据时,会将二进制序列化后的结果放入内存中存储起来.但有时,它也会将他们对象的键值以对象的形式进行有效率的存储,尤其是在本地数据处理比如说在查询或者键值对处理的时候.设置map在内存中的配置您可以决定数据具体以什么样的方式储存在内存当中,下面是可供选择的配置:
-
BINARY
(default): This is the default option. The data will be stored in serialized binary format. You can use this option if you mostly perform regular map operations like put and get. - 二进制:缺省的配置,数据将存储为序列化后的二进制格式,如果你经常使用map的常规方法比如说get或者put方法的时候,选择这个比较好.
-
OBJECT
: The data will be stored in deserialized form. This configuration is good for maps where entry processing and queries form the majority of all operations and the objects are complex ones, so serialization cost is respectively high. By storing objects, entry processing will not contain the deserialization cost. - 对象:数据将会存储为反序列化后的格式.这个配置适用于对于一个负责的map键值对进行大量的数据处理或者查询的时候,推荐使用这个,因为序列化会占用大量的资源.使用这种方式存储对象,将不会花费多余的时间或资源在反序列化上面.
常规操作像get方法.它依赖于对象实例.当使用对象模式存储时,调用get方法,map并不会返回存储的实例,而是创建一份这个map的拷贝.因此当使用get操作时,它将会(在本节点拥有的实例上进行操作)首先进行序列化,然后进行反序列化操作(在本节点上调用实例).但是当二进制模式使用时,只有反序列化操作,这种模式下速度回比较快.
类似的,像put方法使用二进制存储时速度会比较快.如果用对象方式存储的话,map会创建一份该对象克隆的实例.因此首先会序列化然后会反序列化.如果使用二进制存储方式的话,只需要反序列化就可以了.
NOTE: If a value is stored in OBJECT
format, a change on a returned value does not effect the stored instance. In this case, the returned instance is not the actual one but a clone. Therefore, changes made on an object after it is returned will not reflect on the actual stored data. Similarly, when a value is written to a map and the value is stored in OBJECT
format, it will be a copy of the put value. So changes made on the object after it is stored, will not reflect on the actual stored data.
4.1.5 Map Persistence 字典持久化
Hazelcast允许你从一个持久化存储仓库(比如说一个关系型数据库)加载或存储分布式的map.因此如果需要你可以使用MapStore或者MapLoader接口来实现.
当你需要使用MapLoader的时候,如果一个键值对(IMap.get())并不存在于内存中,可使用MapLoader的load或者loadall方法将键值对由数据库加载过来.加载后的键值对将会放入map中(将会一直放在map中直到你将它移除).
当一个MapStore被实现时,键值对将会自动放入数据库中.
NOTE: Data store needs to be a centralized system that is accessible from all Hazelcast Nodes. Persisting to local file system is not supported.
接下来进一个例子:
public class PersonMapStore implements MapStore<Long, Person> {private final Connection con;public PersonMapStore() {try {con = DriverManager.getConnection("jdbc:hsqldb:mydatabase", "SA", "");con.createStatement().executeUpdate("create table if not exists person (id bigint, name varchar(45))");} catch (SQLException e) {throw new RuntimeException(e);}}public synchronized void delete(Long key) {System.out.println("Delete:" + key);try {con.createStatement().executeUpdate(format("delete from person where id = %s", key));} catch (SQLException e) {throw new RuntimeException(e);}}public synchronized void store(Long key, Person value) {try {con.createStatement().executeUpdate(format("insert into person values(%s,'%s')", key, value.name));} catch (SQLException e) {throw new RuntimeException(e);}}public synchronized void storeAll(Map<Long, Person> map) {for (Map.Entry<Long, Person> entry : map.entrySet())store(entry.getKey(), entry.getValue());}public synchronized void deleteAll(Collection<Long> keys) {for (Long key : keys) delete(key);}public synchronized Person load(Long key) {try {ResultSet resultSet = con.createStatement().executeQuery(format("select name from person where id =%s", key));try {
这篇关于Hazelcast--Map数据类型中文版之中篇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!