
本示例演示如何使用 Spring AI Alibaba 集成 OceanBase 作为向量数据库,实现文档的向量存储和相似性搜索功能。通过这个示例,您将了解如何构建一个基于向量数据库的检索增强生成(RAG)应用。
1. 示例目标
我们将创建一个 Spring Boot 应用,实现以下功能:
- 文档导入 (
/oceanbase/import):将示例文档向量化并存储到 OceanBase 数据库中。 - 相似性搜索 (
/oceanbase/search):根据查询内容,从 OceanBase 中检索相似的文档。
2. 技术栈与核心依赖
- Spring Boot 3.x
- Spring AI Alibaba (用于对接阿里云 DashScope 通义大模型)
- Spring AI Alibaba OceanBase Store (用于向量存储)
- OceanBase (作为向量数据库)
- Maven (项目构建工具)
在 pom.xml 中,我们需要引入以下核心依赖:
<dependencies>
<!-- Spring AI Alibaba 核心启动器,集成 DashScope -->
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
</dependency>
<!-- Spring Web 用于构建 RESTful API -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring JDBC 用于数据库连接 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<!-- Spring AI Transformers 用于文档向量化 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-transformers</artifactId>
</dependency>
<!-- Spring AI Alibaba OceanBase Store 启动器 -->
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-store-oceanbase</artifactId>
<version>${spring-ai-alibaba.version}</version>
</dependency>
</dependencies>
3. 项目配置
在 src/main/resources/application.yml 文件中,配置 DashScope API Key 和 OceanBase 数据库连接信息。
server:
port: 8080
spring:
application:
name: oceanbase-example
ai:
dashscope:
api-key: ${AI_DASHSCOPE_API_KEY} # 建议使用环境变量,更安全
vectorstore:
oceanbase:
enabled: true
url: ${OCEANBASE_URL:jdbc:oceanbase://localhost:2881/springai}
username: ${OCEANBASE_USERNAME:root@test}
password: ${OCEANBASE_PASSWORD:test}
tableName: test_ai
重要提示 :请将 AI_DASHSCOPE_API_KEY 环境变量设置为你从阿里云获取的有效 API Key。同时,确保 OceanBase 数据库已正确配置并可访问。
4. 编写 Java 代码
4.1 OceanBaseApplication.java
Spring Boot 主应用程序类。
package com.alibaba.cloud.ai.example.vector.redis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OceanBaseApplication {
public static void main(String[] args) {
SpringApplication.run(OceanBaseApplication.class, args);
}
}
4.2 OceanBaseController.java
实现文档导入和相似性搜索功能的控制器。
package com.alibaba.cloud.ai.example.vector.redis.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.cloud.ai.vectorstore.oceanbase.OceanBaseVectorStore;
@RestController
@RequestMapping("/oceanbase")
public class OceanBaseController {
private static final Logger logger = LoggerFactory.getLogger(OceanBaseController.class);
@Autowired
private OceanBaseVectorStore oceanBaseVectorStore;
@GetMapping("/import")
public void importData() {
logger.info("start import data");
HashMap<String, Object> map = new HashMap<>();
map.put("id", "12345");
map.put("year", "2025");
map.put("name", "yingzi");
List<Document> documents = List.of(
new Document("The World is Big and Salvation Lurks Around the Corner"),
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("year", 2024)),
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", map));
oceanBaseVectorStore.add(documents);
}
@GetMapping("/search")
public List<Document> search() {
logger.info("start search data");
return oceanBaseVectorStore.similaritySearch(SearchRequest
.builder()
.query("Spring")
.topK(2)
.build());
}
}
5. 代码解析
5.1 文档导入功能
在 importData() 方法中:
- 创建了一个包含元数据的 HashMap,用于存储文档的附加信息。
- 创建了三个示例文档:
- 第一个文档是简单的文本内容。
- 第二个文档包含文本和元数据(年份)。
- 第三个文档包含重复的文本和丰富的元数据(ID、年份、名称)。
- 通过
oceanBaseVectorStore.add(documents)将文档向量化并存储到 OceanBase 数据库中。
5.2 相似性搜索功能
在 search() 方法中:
- 使用
SearchRequest.builder()构建搜索请求。 - 设置查询词为 "Spring"。
- 设置返回最相似的前 2 个结果(topK=2)。
- 通过
oceanBaseVectorStore.similaritySearch()执行相似性搜索。 - 返回与查询词最相似的文档列表。
6. 运行与测试
- 启动应用 :运行 Spring Boot 主程序
OceanBaseApplication。 - 使用浏览器或 API 工具(如 Postman, curl)进行测试。
测试 1:导入文档
访问以下 URL,将示例文档导入到 OceanBase 数据库中。
http://localhost:8080/oceanbase/import
预期响应:无直接返回内容,但会在日志中看到 "start import data" 的信息,表示文档已成功导入。
测试 2:相似性搜索
访问以下 URL,搜索与 "Spring" 相关的文档。
http://localhost:8080/oceanbase/search
预期响应:返回与 "Spring" 最相似的前 2 个文档,可能包含类似以下的 JSON 数据:
[
{
"content": "Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!",
"metadata": {
"id": "12345",
"year": "2025",
"name": "yingzi"
}
},
{
"content": "The World is Big and Salvation Lurks Around the Corner",
"metadata": {}
}
]
7. 实现思路与扩展建议
实现思路
本示例的核心思想是利用向量数据库实现文档的语义检索。具体实现步骤如下:
- 文档向量化:Spring AI 自动将文本文档转换为向量表示。
- 向量存储:将文档向量存储到 OceanBase 数据库中。
- 相似性搜索:根据查询词的向量表示,在数据库中查找最相似的文档向量。
这种方法的优点是:
- 语义理解:不仅匹配关键词,还能理解语义上的相似性。
- 高效检索:向量数据库针对高维向量检索进行了优化。
- 易于扩展:可以轻松集成到大型的 RAG 系统中。
扩展建议
- 批量导入:实现从文件系统或数据库批量导入文档的功能。
- 文档预处理:添加文档分块、清洗等预处理步骤,提高检索质量。
- 元数据过滤:在搜索时添加元数据过滤条件,实现更精确的检索。
- 集成大模型:将检索到的文档作为上下文,结合大模型实现问答功能。
- 性能优化:添加缓存层,优化向量索引,提高检索速度。
- 监控与日志:添加更详细的日志和监控指标,便于系统维护。
8. OceanBase 向量数据库简介
OceanBase 是一款分布式关系型数据库,近年来增加了对向量数据的支持,使其能够作为向量数据库使用。在 Spring AI Alibaba 中,OceanBase 向量存储的主要特点包括:
- 高可用性:OceanBase 的分布式架构确保了数据的高可用性和可靠性。
- 强一致性:保证向量数据的一致性,适合对数据一致性要求高的场景。
- 水平扩展:支持水平扩展,可以处理大规模的向量数据。
- 混合查询:支持向量相似性查询与传统关系型查询的结合。
通过本示例,您可以了解如何将 OceanBase 作为向量数据库集成到 Spring AI 应用中,为构建企业级的 RAG 系统提供支持。