Hazelcast--Map数据类型中文版之中篇

2024-06-08 01:18

本文主要是介绍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会创建一份该对象克隆的实例.因此首先会序列化然后会反序列化.如果使用二进制存储方式的话,只需要反序列化就可以了.

image 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被实现时,键值对将会自动放入数据库中.

image 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数据类型中文版之中篇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1040843

相关文章

Java compiler level does not match the version of the installed Java project facet. map解决方法

右键项目“Properties”,在弹出的“Properties”窗口左侧,单击“Project Facets”,打开“Project Facets”页面。 在页面中的“Java”下拉列表中,选择相应版本就OK了。

算法13—Bit Map算法简介

1. Bit Map算法简介          来自于《编程珠玑》。所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素。由于采用了Bit为单位来存储数据,因此在存储空间方面,可以大大节省。 2、 Bit Map的基本思想         我们先来看一个具体的例子,假设我们要对0-7内的5个元素(4,7,2,5,3)排

玩转Web之Json(四)---json与(Object/List/Map)的相互转化

在做web应用时,经常需要将json转化成Object/list/map或者将Object/List/map转化成json,通过简单封装可以在写代码是减轻很多负担。本文将给出json转化的一系列方法。 闲话不 多说,直接上代码: 先是Object /List /Map转化为Json /* 功能 :将一个对象转成json数组* 参数 :object对象* retu

【C++11 之新增容器 array、foward_list、tuple、unordered_(multi)map/set】应知应会

C++11 标准中新增了多个容器,这些容器为 C++ 程序员提供了更多的选择,以满足不同的编程需求。以下是对这些新容器的介绍和使用案例: std::array 介绍: std::array 是一个固定大小的数组容器,它在栈上分配内存,并提供了类似于标准库容器的接口。它提供了更好的类型安全性和范围检查,同时保持了与原生数组相似的性能。std::array 的大小必须在编译时确定,并且不能更改。

双层嵌套json字符串(即json对象内嵌json数组)解析为Map

无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程。 之前我层写过一篇文章,介绍了json与map的相互转化,但当时只涉及到单一的json对象或json数组,对json对象内嵌套这json数组的json字符串无法处理,这篇文章主要解决这个问题。 之前的那篇文章址:http://blo

C++ 关联容器使用 map, unordered_map, set, unordered_set, multiset, unordered_multiset

关联容器是否有序是否关联值是否可重复访问时间set是否否对数map是是否对数multiset是否是对数multimap是是是对数unordered_map否是否常数unordered_set否否否常数unordered_multiset否否是常数unordered_multimap否是是常数 #include <map>#include <set>#i

JS六大数据类型

js的六大数据类型

Python高阶函数map、reduce、filter应用

定义 map映射函数 map()通过接收一个函数F和一个可迭代序列,作用是F依次作用序列的每个元素,并返回一个新的list。reduce递归映射函数 reduce()把一个函数作用在一个序列上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做函数运算。filter过滤函数 filter()与map()类似,接收一个函数F和一个可迭代序列,只不过这里的函数F是条件判断函数。

使用不同数据类型实例化Stack泛型类的代码

package 泛型;import java.math.BigDecimal;import java.math.MathContext;public class StackDemoApp {public static void main(String[] args) {//长类型栈System.out.println("创建'Long'类型栈");//创建用于保存Long数据类型的Stack对象