Ignite实战( 八 )


在大多数情况下,您只需为可变长度字段(例如字符串或数组)上的索引设置内联大小 。默认值为 10 。
您可以通过设置来更改默认值

  • 每个索引单独的内联大小,或
  • CacheConfiguration.sqlIndexMaxInlineSize给定缓存中所有索引的属性,或
  • IGNITE_MAX_INDEX_PAYLOAD_SIZE集群中所有索引的系统属性
设置按上面列出的顺序应用 。
您还可以单独为每个索引配置内联大小,这将覆盖默认值 。要为用户定义的索引设置索引内联大小,请使用以下方法之一 。在所有情况下,该值都以字节为单位 。
  • 使用注解时:
@QuerySqlField(index = true, inlineSize = 13)private String country;
  • 使用时QueryEntity:
QueryIndex idx = new QueryIndex("country");idx.setInlineSize(13);queryEntity.setIndexes(Arrays.asList(idx));
  • 如果您使用该CREATE INDEX命令创建索引,则可以使用该INLINE_SIZE选项设置内联大小:
create index country_idx on Person (country) INLINE_SIZE 13;7.自定义键如果您只对主键使用预定义的 SQL 数据类型,那么您不需要对 SQL 模式配置执行额外的操作 。这些数据类型由GridQueryProcessor.SQL_TYPES常量定义,如下所示 。
预定义的 SQL 数据类型包括:
  • 所有原语及其包装器,除了char和Character
  • String
  • BigDecimal
  • byte[]
  • java.util.Date, java.sql.Date,java.sql.Timestamp
  • java.util.UUID
但是,一旦您决定引入自定义复杂键并从 DML 语句中引用其字段,您需要:
  • QueryEntity以与为值对象设置字段相同的方式定义这些字段 。
  • 使用新的配置参数QueryEntity.setKeyFields(..)来区分键字段和值字段 。
下面的示例显示了如何执行此操作 。
// Preparing cache configuration.CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<Long, Person>("personCache");// Creating the query entity.QueryEntity entity = new QueryEntity("CustomKey", "Person");// Listing all the queryable fields.LinkedHashMap<String, String> fields = new LinkedHashMap<>();fields.put("intKeyField", Integer.class.getName());fields.put("strKeyField", String.class.getName());fields.put("firstName", String.class.getName());fields.put("lastName", String.class.getName());entity.setFields(fields);// Listing a subset of the fields that belong to the key.Set<String> keyFlds = new HashSet<>();keyFlds.add("intKeyField");keyFlds.add("strKeyField");entity.setKeyFields(keyFlds);// End of new settings, nothing else here is DML relatedentity.setIndexes(Collections.<QueryIndex>emptyList());cacheCfg.setQueryEntities(Collections.singletonList(entity));ignite.createCache(cacheCfg);2.6.5 SQL API除了使用 JDBC 驱动程序之外,Java 开发人员还可以使用 Ignite 的 SQL API 来查询和修改存储在 Ignite 中的数据 。
该类SqlFieldsQuery是用于执行 SQL 语句和浏览结果的接口 。SqlFieldsQuery通过IgniteCache.query(SqlFieldsQuery)返回查询游标的方法执行 。
1.配置可查询字段如果要使用 SQL 语句查询缓存,则需要定义值对象的哪些字段是可查询的 。可查询字段是 SQL 引擎可以“看到”和查询的数据模型的字段 。
在 Java 中,可以通过两种方式配置可查询字段:
  • 使用注释
  • 通过定义查询实体
要使特定字段可查询,??请在值类定义中使用@QuerySqlField注解和调用来注解字段CacheConfiguration.setIndexedTypes(…?)
class Person implements Serializable {/** Indexed field. Will be visible to the SQL engine. */@QuerySqlField(index = true)private long id;/** Queryable field. Will be visible to the SQL engine. */@QuerySqlFieldprivate String name;/** Will NOT be visible to the SQL engine. */private int age;/*** Indexed field sorted in descending order. Will be visible to the SQL engine.*/@QuerySqlField(index = true, descending = true)private float salary;}public static void main(String[] args) {Ignite ignite = Ignition.start();CacheConfiguration<Long, Person> personCacheCfg = new CacheConfiguration<Long, Person>();personCacheCfg.setName("Person");personCacheCfg.setIndexedTypes(Long.class, Person.class);IgniteCache<Long, Person> cache = ignite.createCache(personCacheCfg);}

经验总结扩展阅读