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 查詢匹配的結果 。

推薦閱讀