当前位置: 首页 > news >正文

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 INDEXDROP 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,因为嵌套字段会被“展平”处理。

⚠️ 注意:如果对嵌套对象创建了索引,将无法对表执行 UPDATEINSERT 操作。


📌 九、注册索引类型

原文:

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 可用于查询
二进制对象不需要类定义也能使用索引字段

如果你有具体的代码示例或问题,我可以进一步帮你分析和优化。是否需要我为你写一个完整的示例?

http://www.lryc.cn/news/596336.html

相关文章:

  • 实操:AWS CloudFront的动态图像转换
  • 服务器租用:网络钓鱼具体是指什么?
  • 扇形区域拉普拉斯方程傅里叶解法2
  • Windows Cmake Vs2017/2010 编译安装Protobuf
  • 算法训练营day28 贪心算法②122.买卖股票的最佳时机II、55. 跳跃游戏、 45.跳跃游戏II 、1005.K次取反后最大化的数组和
  • Flutter基础(前端教程①⑦-Column竖直-Row水平-Warp包裹-Stack堆叠)
  • Flutter基础(前端教程①⑨-margin-padding)
  • 全星FMEA软件系统:FMEA、PC、PFD一体化管理的智能解决方案
  • Scrapyd与ScrapydAPI深度解析:企业级爬虫部署与管理解决方案
  • ComfyUI怎样通过接口调用?如何接入dify?
  • 我的第一个开源项目 -- 实时语音识别工具
  • patch-package 教程
  • 什么是AI思维:它是智能优先与世界模型重构商业逻辑
  • 当直播间告别“真人时代”:AI数字人重构商业新秩序
  • 卷积操作尺寸计算公式
  • @DateTimeFormat、@JsonFormat、@JSONField区别及用法
  • Linux_基础IO详解
  • 聊聊DevOps,开发与运维如何分工协作?
  • 什么是检索增强生成(RAG)?
  • 引擎动画系统设计
  • 【PTA数据结构 | C语言版】不相交集基本操作
  • Visual Studio Code 远端云服务器开发使用指南
  • 服务器连接Hugging Face
  • 【机器学习深度学习】微调量化与模型导出量化:区分与应用
  • HttpSecurity详解
  • 生存分析机器学习问题
  • Oracle定时清理归档日志
  • 通过 Docker 部署测试 CODESYS PLC示例
  • Linux_Ext系列文件系统基本认识(一)
  • 【实战】Dify从0到100进阶--文档解读(11)其他节点