7.自定义键如果您只对主键使用预定义的 SQL 数据类型,那么您不需要对 SQL 模式配置执行额外的操作 。这些数据类型由GridQueryProcessor.SQL_TYPES常量定义,如下所示 。
预定义的 SQL 数据类型包括:
- 所有原语及其包装器,除了char和Character
- String
- BigDecimal
- byte[]
- java.util.Date, java.sql.Date,java.sql.Timestamp
- java.util.UUID
- 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 中,可以通过两种方式配置可查询字段:
- 使用注释
- 通过定义查询实体
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 查询匹配的结果 。
推荐阅读
- 快读《ASP.NET Core技术内幕与项目实战》WebApi3.1:WebApi最佳实践
- IQueryable和IEnumerable 快读《ASP.NET Core技术内幕与项目实战》EFCore2.5:集合查询原理揭秘
- Spring事务传播行为实战
- 四十八 SpringCloud微服务实战——搭建企业级开发框架:【移动开发】整合uni-app搭建移动端快速开发框架-使用第三方UI框架
- React +SpreadJS+Echarts 项目实战:在线报价采购系统
- 四十七 SpringCloud微服务实战——搭建企业级开发框架:【移动开发】整合uni-app搭建移动端快速开发框架-添加Axios并实现登录功能
- 【一】ERNIE:飞桨开源开发套件,入门学习,看看行业顶尖持续学习语义理解框架,如何取得世界多个实战的SOTA效果?
- Module XAF新手入门 - 模块
- 3 Python全栈工程师之从网页搭建入门到Flask全栈项目实战 - 入门Flask微框架
- 机器学习实战-AdaBoost