这段内容是关于 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;
@QuerySqlField
private 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
)中的字段也可以被索引。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);
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 是基本类型时非常有用。
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-indexing 模块 |
自动索引 | 主键和关联键字段自动创建索引 |
索引字段 | 使用 @QuerySqlField(index = true) 注解 |
可查询字段 | 使用 @QuerySqlField (不带 index) |
嵌套字段 | 支持嵌套对象索引,但限制 INSERT /UPDATE |
复合索引 | 索引字段 + 主键组成复合索引 |
表名 | Java 类名作为 SQL 表名 |
注册类型 | 使用 setIndexedTypes() 注册索引类型 |
预定义字段 | _key 和 _val 可用于查询 |
二进制对象 | 不需要类定义也能使用索引字段 |
如果你有具体的代码示例或问题,我可以进一步帮你分析和优化。是否需要我为你写一个完整的示例?