Apache Ignite 索引(Indexes)定义和使用
这段内容是关于 Apache Ignite 中如何定义和使用 索引(Indexes) 的说明。下面我将为你 逐段解释这段内容的含义,并帮助你理解 Apache Ignite 中 SQL 索引机制的核心概念。
📌 一、索引的定义
原文:
In addition to common DDL commands, such as CREATE/DROP INDEX, developers can use Ignite’s SQL APIs to define indexes.
理解:
除了使用标准的 DDL 命令(如 CREATE INDEX
, DROP INDEX
)外,开发者还可以通过 Ignite 的 SQL API 来定义索引。
📌 二、索引模块
原文:
Indexing capabilities are provided by the ‘ignite-indexing’ module. If you start Ignite from Java code, add this module to your classpath.
理解:
索引功能由 ignite-indexing
模块提供。如果你是通过 Java 代码启动 Ignite,需要将这个模块加入 classpath。
📌 三、自动创建索引
原文:
Ignite automatically creates indexes for each primary key and affinity key field.
理解:
Ignite 会自动为每个主键(primary key)和关联键(affinity key)字段创建索引。
📌 四、自定义索引字段
原文:
When you define an index on a field in the value object, Ignite creates a composite index consisting of the indexed field and the cache’s primary key.
理解:
当你在值对象(value object)中为某个字段定义索引时,Ignite 会创建一个 复合索引,包含这个字段和缓存的主键。也就是说,SQL 中的索引实际上是一个包含两个列的索引:你指定的字段 + 主键。
例如:
CREATE INDEX idx_name ON Person(name);
实际上创建的索引结构类似于:
CREATE INDEX idx_name ON Person(name, id);
📌 五、通过注解定义索引
原文代码:
public class Person implements Serializable {@QuerySqlField(index = true)private long id;@QuerySqlFieldprivate String name;private int age;@QuerySqlField(index = true, descending = true)private float salary;
}
理解:
@QuerySqlField(index = true)
:表示该字段会被索引,并可用于 SQL 查询。@QuerySqlField
(无index = true
):表示该字段是“可查询字段”,不会被索引,但可以在 SQL 查询中使用。- 未加注解的字段(如
age
)既不可查询,也不可索引。 descending = true
:表示该字段的索引是按降序排列。
📌 六、表名与类名的关系
原文:
The type name is used as the table name in SQL queries.
理解:
Java 类名将作为 SQL 查询中的表名。例如 Person
类对应 SQL 中的 Person
表。
📌 七、运行时更新索引
原文:
Use the CREATE/DROP INDEX commands if you need to manage indexes or make an object’s new fields visible to the SQL engine at runtime.
理解:
如果你需要在运行时修改索引结构或添加新的可查询字段,可以使用 CREATE INDEX
或 DROP INDEX
命令。
📌 八、嵌套对象索引
原文代码:
public class Person {@QuerySqlField(index = true)private Address address;
}public class Address {@QuerySqlField(index = true)private String street;@QuerySqlField(index = true)private int zip;
}
理解:
- 嵌套对象(如
Address
)中的字段也可以被索引。 - 在 SQL 查询中,可以直接使用嵌套字段,例如:
不需要写成SELECT * FROM Person WHERE street = 'Main St'
address.street
,因为嵌套字段会被“展平”处理。
⚠️ 注意:如果对嵌套对象创建了索引,将无法对表执行 UPDATE
或 INSERT
操作。
📌 九、注册索引类型
原文:
After indexed and queryable fields are defined, they have to be registered in the SQL engine along with the object types they belong to.
理解:
在定义了索引和可查询字段之后,需要通过 CacheConfiguration.setIndexedTypes()
方法将这些类型注册到 SQL 引擎中。
示例代码:
CacheConfiguration<Long, Person> ccfg = new CacheConfiguration<>();
ccfg.setIndexedTypes(Long.class, Person.class);
- 该方法接受键值对类型(key class 和 value class)。
- 基本类型需要以包装类形式传入(如
Long.class
而不是long.class
)。
📌 十、预定义字段 _key
和 _val
原文:
Each table will have two special predefined fields: _key and _val, which represent links to whole key and value objects.
理解:
每张表都会有两个预定义字段:
_key
:表示整个 key 对象。_val
:表示整个 value 对象。
例如,你可以这样查询:
SELECT * FROM Person WHERE _key = 100
这对在 key 是基本类型时非常有用。
📌 十一、支持二进制对象(Binary Objects)
原文:
Since Ignite supports Binary Objects, there is no need to add classes of indexed types to the classpath of cluster nodes.
理解:
Ignite 支持二进制对象(Binary Objects),这意味着即使集群节点上没有类定义,SQL 引擎也能识别索引字段和可查询字段的内容,无需反序列化整个对象。
✅ 总结:Ignite 中索引机制的核心要点
项目 | 说明 |
---|---|
索引模块 | 使用 ignite-indexing 模块 |
自动索引 | 主键和关联键字段自动创建索引 |
索引字段 | 使用 @QuerySqlField(index = true) 注解 |
可查询字段 | 使用 @QuerySqlField (不带 index) |
嵌套字段 | 支持嵌套对象索引,但限制 INSERT /UPDATE |
复合索引 | 索引字段 + 主键组成复合索引 |
表名 | Java 类名作为 SQL 表名 |
注册类型 | 使用 setIndexedTypes() 注册索引类型 |
预定义字段 | _key 和 _val 可用于查询 |
二进制对象 | 不需要类定义也能使用索引字段 |
如果你有具体的代码示例或问题,我可以进一步帮你分析和优化。是否需要我为你写一个完整的示例?