Ignite实战( 九 )

确保调用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 查询匹配的结果 。
4.本地执行要强制本地执行查询,请使用SqlFieldsQuery.setLocal(true). 在这种情况下,查询是针对存储在运行查询的节点上的数据执行的 。这意味着查询的结果几乎总是不完整的 。仅当您确信自己了解此限制时才使用本地模式 。
5.WHERE子句中的子查询SELECT在INSERTandMERGE语句中使用的查询以及SELECT由UPDATEandDELETE操作生成的查询以colocated 或 non-colocated 分布式模式分布和执行 。
但是,如果有一个子查询作为WHERE子句的一部分执行,则它只能在 colocated 模式下执行 。
例如,让我们考虑以下查询:
DELETE FROM Person WHERE id IN(SELECT personId FROM Salary s WHERE s.amount > 2000);SQL 引擎生成SELECT查询以获取要删除的条目列表 。该查询在整个集群中分布和执行,如下所示:
SELECT _key, _val FROM Person WHERE id IN(SELECT personId FROM Salary s WHERE s.amount > 2000);但是,IN子句 ( SELECT personId FROM Salary …?) 中的子查询不会进一步分布,而是在节点上可用的本地数据集上执行 。
6.插入、更新、删除和合并SqlFieldsQuery您可以执行其他 DML 命令以修改数据:
// 插入IgniteCache<Long, Person> cache = ignite.cache("personCache");cache.query(new SqlFieldsQuery("INSERT INTO Person(id, firstName, lastName) VALUES(?, ?, ?)").setArgs(1L, "John", "Smith")).getAll();// 更新IgniteCache<Long, Person> cache = ignite.cache("personCache");cache.query(new SqlFieldsQuery("UPDATE Person set lastName = ? " + "WHERE id >= ?").setArgs("Jones", 2L)).getAll();// 删除IgniteCache<Long, Person> cache = ignite.cache("personCache");cache.query(new SqlFieldsQuery("DELETE FROM Person " + "WHERE id >= ?").setArgs(2L)).getAll();// 合并IgniteCache<Long, Person> cache = ignite.cache("personCache");cache.query(new SqlFieldsQuery("MERGE INTO Person(id, firstName, lastName)"+ " values (1, 'John', 'Smith'), (5, 'Mary', 'Jones')")).getAll();用于SqlFieldsQuery执行 DDL 语句时,必须调用getAll()从query(…?)方法返回的游标 。
7.指定架构默认情况下,执行的任何 SELECT 语句SqlFieldsQuery都将针对 PUBLIC 模式进行解析 。但是,如果您要查询的表在不同的架构中,您可以通过调用来指定架构SqlFieldsQuery.setSchema(…?) 。在这种情况下,语句在给定的模式中执行 。

经验总结扩展阅读