Ignite实战( 七 )

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);}确保调用CacheConfiguration.setIndexedTypes(…?)以让 SQL 引擎知道带注释的字段 。
2.查询实体QueryEntity您可以使用该类定义可查询字段 。查询实体可以通过 XML 配置进行配置 。
class Person implements Serializable {private long id;private String name;private int age;private float salary;}public static void main(String[] args) {Ignite ignite = Ignition.start();CacheConfiguration<Long, Person> personCacheCfg = new CacheConfiguration<Long, Person>();personCacheCfg.setName("Person");QueryEntity queryEntity = new QueryEntity(Long.class, Person.class).addQueryField("id", Long.class.getName(), null).addQueryField("age", Integer.class.getName(), null).addQueryField("salary", Float.class.getName(), null).addQueryField("name", String.class.getName(), null);queryEntity.setIndexes(Arrays.asList(new QueryIndex("id"), new QueryIndex("salary", false)));personCacheCfg.setQueryEntities(Arrays.asList(queryEntity));IgniteCache<Long, Person> cache = ignite.createCache(personCacheCfg);}3.查询要在缓存上执行选择查询,只需创建一个对象,SqlFieldsQuery将查询字符串提供给构造函数并运行cache.query(…?) 。请注意,在以下示例中,必须将 Person 缓存配置为对 SQL 引擎可见 。
IgniteCache<Long, Person> cache = ignite.cache("Person");SqlFieldsQuery sql = new SqlFieldsQuery("select concat(firstName, ' ', lastName) from Person");// Iterate over the result set.try (QueryCursor<List<?>> cursor = cache.query(sql)) {for (List<?> row : cursor)System.out.println("personName=" + row.get(0));}SqlFieldsQuery返回一个游标 , 该游标遍历与 SQL 查询匹配的结果 。

推荐阅读