[关闭]
@liyuj 2017-03-31T13:26:44.000000Z 字数 8915 阅读 2798

Apache-Ignite-1.9.0-中文开发手册

10.编组

10.1.二进制编组器

10.1.1.基本概念

从1.5版本开始,Ignite引入了一个在缓存中存储数据的新概念,名为二进制对象,这个新的序列化格式提供了若干个优势:

二进制对象只可以用于使用默认的二进制编组器时(即没有在配置中显式地设置其他的编组器)

限制
BinaryObject格式实现也带来了若干个限制:
1. 在内部Ignite不会写属性以及类型的名字,但是使用一个小写的名字哈希来标示一个属性或者类型,这意味着属性或者类型不能有同样的名字哈希。即使序列化不会在哈希冲突的情况下工作,但Ignite在配置级别提供了一种方法来解决此冲突;
2.同样的原因,BinaryObject格式在类的不同层次上也不允许有同样的属性名;
3.默认会忽略Externalizable接口。如果使用了BinaryObject格式,Externalizable类型会与Serializable类型是同样的处理方式,没有writeExternal()readExternal()方法。如果由于某些原因这样不行,需要实现Binarylizable接口,加入一个自定义BinarySerializer或者切换到OptimizedMarshaller

IgniteBinary入口,可以从Ignite的实例获得,包含了操作二进制对象的所有必要的方法。

10.1.2.处理HashCode生成和equals执行

当一个对象被序列化到二进制格式时,Ignite默认会捕获他的哈希值然后将其与二进制对象属性并排地保存,这个恰当而且一致的哈希值可以在集群内的所有节点为所有对象提供。对于equals比较,Ignite默认会依赖序列化对象的二进制表示进行逐字节地比较,这意味着它会比较一个对象内的所有序列化字段,从第一个直到最后一个。由于这个比较类型,结果依赖于字段序列化的顺序。
这个默认的行为是由BinaryArrayIdentityResolver接口实现的,每个对象序列化到二进制格式时都会进行设定。
如果这个默认的方式不合适,那么可以通过BinaryIdentityResolver接口进行自定义实现,或者使用BinaryFieldIdentityResolver实现进行替代。
Binary Identity Resolver
这个接口可以自定义哈希值生成的逻辑,以及二进制对象进行一致性比较的方式。

  1. public interface BinaryIdentityResolver {
  2. /**
  3. * Compute hash code for binary object.
  4. */
  5. public int hashCode(BinaryObject obj);
  6. /**
  7. * Compare two binary objects for equality.
  8. */
  9. public boolean equals(@Nullable BinaryObject o1, @Nullable BinaryObject o2);
  10. }

这个解析器是通过BinaryTypeConfiguration对象进行配置的,下面的示例显示如何实现:

  1. <bean class="org.apache.ignite.configuration.IgniteConfiguration">
  2. ....
  3. <property name="binaryConfiguration">
  4. <bean class="org.apache.ignite.configuration.BinaryConfiguration">
  5. <!-- Listing specific configuration for binary types -->
  6. <property name="typeConfigurations">
  7. <list>
  8. <bean class="org.apache.ignite.binary.BinaryTypeConfiguration">
  9. <!-- Defining types that will used the custom resolver. -->
  10. <property name="typeName" value="org.app.model.*"/>
  11. <!-- Setting the custom resolver for the types -->
  12. <property name="identityResolver">
  13. <bean class="org.app.example.MyCustomIdentityResolver"/>
  14. </property>
  15. </bean>
  16. <bean class="org.apache.ignite.binary.BinaryTypeConfiguration">
  17. <property name="typeName" value="org.app.Person" />
  18. <!-- Setting specific resolver for Person type -->
  19. <property name="identityResolver">
  20. <bean class="org.app.example.CustomPersonIdentityResolver"/>
  21. </property>
  22. </bean>
  23. </list>
  24. </property>
  25. </bean>
  26. </property>

Binary Field Identity Resolver
这个类型的解析器使用特定对象的字段值进行哈希值的计算以及一致性比较。当不希望比较的结果依赖于字段序列化的顺序时,可以用这个解析器替代BinaryArrayIdentityResolver,当进行哈希值生成或者对象比较时,BinaryFieldIdentityResolver会只使用配置中列出的字段以及它们在配置中定义的顺序。

  1. <bean class="org.apache.ignite.configuration.IgniteConfiguration">
  2. ....
  3. <property name="binaryConfiguration">
  4. <bean class="org.apache.ignite.configuration.BinaryConfiguration">
  5. <!-- Listing specific configuration for binary types -->
  6. <property name="typeConfigurations">
  7. <list>
  8. <bean class="org.apache.ignite.binary.BinaryTypeConfiguration">
  9. <property name="typeName" value="org.app.Person" />
  10. <!-- Setting BinaryFieldIdentityResolver for Person type -->
  11. <property name="identityResolver">
  12. <bean class="org.apache.ignite.binary.BinaryFieldIdentityResolver">
  13. <!--
  14. List of fields, whose values will be used for hash
  15. calculation and equality comparision.
  16. -->
  17. <property name="fieldNames">
  18. <list>
  19. <value>id</value>
  20. <value>firstName</value>
  21. </list>
  22. </property>
  23. </bean>
  24. </property>
  25. </bean>
  26. </list>
  27. </property>
  28. </bean>
  29. </property>

10.1.3.配置二进制对象

在绝大多数情况下不需要额外地配置二进制对象,在IgniteConfiguration中如果不配置其他的编组器,BinaryObject编组器会被默认启用。
如果需要覆写默认的类型和属性ID计算或者加入BinarySerializer,可以为IgniteConfiguration设置一个BinaryConfiguration对象,这个对象除了为每个类型指定映射以及序列化器之外还可以指定一个全局的Name映射、一个ID映射以及一个全局的二进制序列化器。对于每个类型的配置,通配符也是支持的,这时提供的配置会适用于匹配类型名称模板的所有类型。
配置二进制类型:

  1. <bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
  2. <property name="binaryConfiguration">
  3. <bean class="org.apache.ignite.configuration.BinaryConfiguration">
  4. <property name="nameMapper" ref="globalNameMapper"/>
  5. <property name="idMapper" ref="globalIdMapper"/>
  6. <property name="typeConfigurations">
  7. <list>
  8. <bean class="org.apache.ignite.binary.BinaryTypeConfiguration">
  9. <property name="typeName" value="org.apache.ignite.examples.*"/>
  10. <property name="serializer" ref="exampleSerializer"/>
  11. </bean>
  12. <bean class="org.apache.ignite.binary.BinaryTypeConfiguration">
  13. <property name="typeName" value="org.app.Person" />
  14. <!-- Setting BinaryFieldIdentityResolver for Person type -->
  15. <property name="identityResolver">
  16. <bean class="org.apache.ignite.binary.BinaryFieldIdentityResolver">
  17. <!--
  18. List of fields, whose values will be used for hash
  19. calculation and equality comparision.
  20. -->
  21. <property name="fieldNames">
  22. <list>
  23. <value>id</value>
  24. <value>firstName</value>
  25. </list>
  26. </property>
  27. </bean>
  28. </property>
  29. </bean>
  30. </list>
  31. </property>
  32. </bean>
  33. </property>
  34. ...

10.1.4.BinaryObject缓存API

Ignite默认使用反序列化值作为最常见的使用场景,要启用BinaryObject处理,需要获得一个IgniteCache的实例然后使用withKeepBinary()方法。启用之后,如果可能,这个标志会确保从缓存返回的对象都是BinaryObject格式的。将值传递给EntryProcessorCacheInterceptor也是同样的处理。

平台类型
注意当通过withKeepBinary()方法启用BinaryObject处理时并不是所有的对象都会表示为BinaryObject,会有一系列的平台类型,包括基本类型,String,UUID,Date,Timestamp,BigDecimal,Collections,Maps和Arrays,他们不会被表示为BinaryObject
注意在下面的示例中,键类型为Integer,他是不会被修改,因为他是平台类型。

获取BinaryObject:

  1. // Create a regular Person object and put it to the cache.
  2. Person person = buildPerson(personId);
  3. ignite.cache("myCache").put(personId, person);
  4. // Get an instance of binary-enabled cache.
  5. IgniteCache<Integer, BinaryObject> binaryCache = ignite.cache("myCache").withKeepBinary();
  6. // Get the above person object in the BinaryObject format.
  7. BinaryObject binaryPerson = binaryCache.get(personId);

10.1.5.使用BinaryObjectBuilder修改二进制对象

BinaryObject实例是不能修改的,要更新属性或者创建新的BinaryObject,必须使用BinaryObjectBuilder的实例。
BinaryObjectBuilder的实例可以通过IgniteBinary入口获得。他可以使用类型名创建,这时返回的对象不包含任何属性,或者他也可以通过一个已有的BinaryObject创建,这时返回的对象会包含从给定的BinaryObject中拷贝的所有属性。
获取BinaryObjectBuilder实例的另外一个方式是调用已有BinaryObject实例的toBuilder()方法,这种方式创建的对象也会从BinaryObject中拷贝所有的数据。

BinaryObjectBuilder和哈希值
注意如果构造的BinaryObject用做缓存键,很重要的是要为BinaryObjectBuilder设置正确的哈希值,因为构造器不会自动计算哈希值,然后导致返回的BinaryObject哈希值为0。

下面是一个使用BinaryObjectAPI来处理服务端节点的数据而不需要将程序部署到服务端以及不需要实际的数据反序列化的示例:
EntryProcessor内的BinaryObject:

  1. cache.<Integer, BinaryObject>withKeepBinary().invoke(
  2. new CacheEntryProcessor<Integer, BinaryObject, Void>() {
  3. @Override Void process(
  4. MutableEntry<Integer, BinaryObject> entry, Object... args) {
  5. // Create builder from the old value.
  6. BinaryObjectBuilder bldr = entry.getValue().toBuilder();
  7. //Update the field in the builder.
  8. bldr.setField("name", "Ignite");
  9. // Set new value to the entry.
  10. entry.setValue(bldr.build());
  11. return null;
  12. }
  13. });

10.1.6.BinaryObject类型元数据

像上面描述的那样,二进制对象结构可以在运行时进行修改,因此获取一个存储在缓存中的一个特定类型的信息也可能是有用的,比如属性名,属性类型,属性类型名,关系属性名,Ignite通过BinaryType接口满足这样的需求。
这个接口还引入了一个属性getter的更快的版本,叫做BinaryField。这个概念类似于Java的反射,可以缓存BinaryField实例中读取的属性的特定信息,他有助于从一个很大的二进制对象集合中读取同一个属性。

  1. Collection<BinaryObject> persons = getPersons();
  2. BinaryField salary = null;
  3. double total = 0;
  4. int cnt = 0;
  5. for (BinaryObject person : persons) {
  6. if (salary == null)
  7. salary = person.type().field("salary");
  8. total += salary.value(person);
  9. cnt++;
  10. }
  11. double avg = total / cnt;

10.1.7.BinaryObject和CacheStore

在缓存API上调用withKeepBinary()方法对于将用户对象传入CacheStore的方式不起作用,这么做是故意的,因为大多数情况下单个CacheStore实现要么使用反序列化类,要么使用BinaryObject表示。要控制对象传入Store的方式,需要使用CacheConfigurationstoreKeepBinary标志,当该标志设置为false时,会将反序列化值传入Store,否则会使用BinaryObject表示。
下面是一个使用BinaryObject的Store的伪代码实现的示例:

  1. public class CacheExampleBinaryStore extends CacheStoreAdapter<Integer, BinaryObject> {
  2. @IgniteInstanceResource
  3. private Ignite ignite;
  4. /** {@inheritDoc} */
  5. @Override public BinaryObject load(Integer key) {
  6. IgniteBinary binary = ignite.binary();
  7. List<?> rs = loadRow(key);
  8. BinaryObjectBuilder bldr = binary.builder("Person");
  9. for (int i = 0; i < rs.size(); i++)
  10. bldr.setField(name(i), rs.get(i));
  11. return bldr.build();
  12. }
  13. /** {@inheritDoc} */
  14. @Override public void write(Cache.Entry<? extends Integer, ? extends BinaryObject> entry) {
  15. BinaryObject obj = entry.getValue();
  16. BinaryType type = obj.type();
  17. Collection<String> fields = type.fieldNames();
  18. List<Object> row = new ArrayList<>(fields.size());
  19. for (String fieldName : fields)
  20. row.add(obj.field(fieldName));
  21. saveRow(entry.getKey(), row);
  22. }
  23. }

10.1.8.二进制Name映射器和二进制ID映射器

在内部,Ignite不会写属性或者类型名字的完整字符串,而是因为性能的原因,为类型或者属性名写一个整型哈希值作为替代。经过测试,在类型相同时,属性名或者类型名的哈希值冲突实际上是不存在的,为了获得性能,使用哈希值是安全的。对于当不同的类型或者属性确实冲突的场合,BinaryNameMapperBinaryIdMapper可以为该类型或者属性名覆写自动生成的哈希值。
BinaryNameMapper - 映射类型/类和属性名到不同的名字;
BinaryIdMapper - 映射从BinaryNameMapper来的类型和属性名到ID,以便于Ignite内部使用。
Ignite提供了下面的开箱即用的映射器实现:

如果使用了纯Java客户端并且在BinaryConfiguration中没有指定映射器,那么Ignite会使用BinaryBasicNameMapper并且simpleName属性会被设置为false,使用BinaryBasicIdMapper并且lowerCase属性会被设置为true
如果使用了.Net或者C++客户端并且在BinaryConfiguration中没有指定映射器,那么Ignite会使用BinaryBasicNameMapper并且simpleName属性会被设置为true,使用BinaryBasicIdMapper并且lowerCase属性会被设置为true
如果使用纯Java、.Net或者C++,默认是不需要任何配置的,只有当需要平台协同、名字转换复杂的情况下,才需要配置映射器。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注