From f9ed5a6610dd288c010284594820e63e1499596d Mon Sep 17 00:00:00 2001 From: Eureka Date: Mon, 24 Mar 2025 20:15:09 +0800 Subject: [PATCH 1/2] data vec go sdk --- .../zh/docs/DataVec/DataVec-integrations.md | 3 +- .../zh/docs/DataVec/integrationGo.md | 98 +++++++++++++++++++ .../zh/docs/DataVec/DataVec-integrations.md | 2 +- content/zh/docs/DataVec/integrationGo.md | 98 +++++++++++++++++++ 4 files changed, 198 insertions(+), 3 deletions(-) create mode 100644 content/docs-lite/zh/docs/DataVec/integrationGo.md create mode 100644 content/zh/docs/DataVec/integrationGo.md diff --git a/content/docs-lite/zh/docs/DataVec/DataVec-integrations.md b/content/docs-lite/zh/docs/DataVec/DataVec-integrations.md index 9345bc5a4..0ef76e1c0 100644 --- a/content/docs-lite/zh/docs/DataVec/DataVec-integrations.md +++ b/content/docs-lite/zh/docs/DataVec/DataVec-integrations.md @@ -15,5 +15,4 @@ openGauss DataVec提供多种第三方组件的集成教程,并通过多语言 - [Python](https://gitee.com/opengauss/openGauss-connector-python-psycopg2) - [Java](https://gitee.com/opengauss/openGauss-connector-jdbc) - [Node.js](https://gitee.com/opengauss/openGauss-connector-nodejs) -- [Go](https://gitee.com/opengauss/openGauss-connector-go-pq) - +- [Go](integrationGo.md) diff --git a/content/docs-lite/zh/docs/DataVec/integrationGo.md b/content/docs-lite/zh/docs/DataVec/integrationGo.md new file mode 100644 index 000000000..845022e8f --- /dev/null +++ b/content/docs-lite/zh/docs/DataVec/integrationGo.md @@ -0,0 +1,98 @@ +# Go SDK对接向量数据库 +本文介绍如何使用Go语言调用openGauss向量数据库 + +## 要求 +- 安装Go 1.19及以上版本 + +## 安装SDK +开发者可以运行以下命令安装GO SDK[社区官网](http://gitee.com/opengauss/openGauss-connector-go-pq),并在项目中导入该包 +``` +安装SDK +go get gitee.com/opengauss/openGauss-connector-go-pq + +在项目中导入该包 +import ( + "database/sql" + + _ "gitee.com/opengauss/openGauss-connector-go-pq" +) + +``` +## 基本操作 +### 1.连接数据库 +```go +// connectInfo格式: "host=127.0.0.1 port=5432 user=username password=userpassword dbname=userdbname sslmode=disable" +func CreateDBClient(connectInfo string) (*sql.DB, error) { + return sql.Open("opengauss", connectInfo) +} +``` +### 2.创建表 +```go +func CreateTable(client *sql.DB, dim int) error { + execSql := fmt.Sprintf("CREATE TABLE IF NOT EXISTS demotable(id INTEGER, content TEXT, embedding vector(%d))", dim) + _, err := client.Exec(execSql) + return err +} +``` +### 3.创建索引 +```go +// 用L2距离创建HNSW类型的向量索引 +func CreateIndex(client *sql.DB) error { + execSql := fmt.Sprint("CREATE INDEX ON demotable USING hnsw (embedding vector_l2_ops)") + _, err := client.Exec(execSql) + return err +} +``` +### 4.插入/删除/更新 +- 插入 + ```go +// 单条插入 +type TableData struct { + Id int + Content string + Vector string +} +func InsertDataSingle(client *sql.DB, data TableData) error { + execSql := fmt.Sprintf("INSERT INTO demotable VALUES(%d, '%s', '%s')", data.Id, data.Content, data.Vector) + _, err := client.Exec(execSql) + return err +} +``` +- 删除 +```go +func DeleteData(client *sql.DB) error { + execSql := fmt.Sprint("DELETE FROM demotable where id > 10") + _, err := client.Exec(execSql) + return err +} +``` +- 更新 +```go +func UpdateData(client *sql.DB, vector string) error { + execSql := fmt.Sprintf("UPDATE demotable set embedding = '%s' where id = 10", vector) + _, err := client.Exec(execSql) + return err +} +``` +### 5.查询 +```go +func FindNearestVectors(client *sql.DB, efsearch int, vector string, topK int) []string { + var res []string + // 设置查询参数 + paramsql := fmt.Sprintf("set hnsw_ef_search = %d", efsearch); + querysql := fmt.Sprintf("SELECT * FROM demotable ORDER BY embedding <-> '%s' LIMIT %d;", vector, topK); + _, _ = client.Exec(paramsql) + rows, _ := client.Query(querysql) + for rows.Next() { + var id int + var content string + var vector []byte + _ = rows.Scan(&id, &content, &vector) + embedding := string(vector) + row := fmt.Sprintf("id: %d, content: %s, embedding: %s", id, content, embedding); + res = append(res, row) + } + return res +} +``` +[更多操作示例参考](https://gitee.com/opengauss/openGauss-connector-go-pq) \ No newline at end of file diff --git a/content/zh/docs/DataVec/DataVec-integrations.md b/content/zh/docs/DataVec/DataVec-integrations.md index 9345bc5a4..1bd50b142 100644 --- a/content/zh/docs/DataVec/DataVec-integrations.md +++ b/content/zh/docs/DataVec/DataVec-integrations.md @@ -15,5 +15,5 @@ openGauss DataVec提供多种第三方组件的集成教程,并通过多语言 - [Python](https://gitee.com/opengauss/openGauss-connector-python-psycopg2) - [Java](https://gitee.com/opengauss/openGauss-connector-jdbc) - [Node.js](https://gitee.com/opengauss/openGauss-connector-nodejs) -- [Go](https://gitee.com/opengauss/openGauss-connector-go-pq) +- [Go](integrationGo.md) diff --git a/content/zh/docs/DataVec/integrationGo.md b/content/zh/docs/DataVec/integrationGo.md new file mode 100644 index 000000000..845022e8f --- /dev/null +++ b/content/zh/docs/DataVec/integrationGo.md @@ -0,0 +1,98 @@ +# Go SDK对接向量数据库 +本文介绍如何使用Go语言调用openGauss向量数据库 + +## 要求 +- 安装Go 1.19及以上版本 + +## 安装SDK +开发者可以运行以下命令安装GO SDK[社区官网](http://gitee.com/opengauss/openGauss-connector-go-pq),并在项目中导入该包 +``` +安装SDK +go get gitee.com/opengauss/openGauss-connector-go-pq + +在项目中导入该包 +import ( + "database/sql" + + _ "gitee.com/opengauss/openGauss-connector-go-pq" +) + +``` +## 基本操作 +### 1.连接数据库 +```go +// connectInfo格式: "host=127.0.0.1 port=5432 user=username password=userpassword dbname=userdbname sslmode=disable" +func CreateDBClient(connectInfo string) (*sql.DB, error) { + return sql.Open("opengauss", connectInfo) +} +``` +### 2.创建表 +```go +func CreateTable(client *sql.DB, dim int) error { + execSql := fmt.Sprintf("CREATE TABLE IF NOT EXISTS demotable(id INTEGER, content TEXT, embedding vector(%d))", dim) + _, err := client.Exec(execSql) + return err +} +``` +### 3.创建索引 +```go +// 用L2距离创建HNSW类型的向量索引 +func CreateIndex(client *sql.DB) error { + execSql := fmt.Sprint("CREATE INDEX ON demotable USING hnsw (embedding vector_l2_ops)") + _, err := client.Exec(execSql) + return err +} +``` +### 4.插入/删除/更新 +- 插入 + ```go +// 单条插入 +type TableData struct { + Id int + Content string + Vector string +} +func InsertDataSingle(client *sql.DB, data TableData) error { + execSql := fmt.Sprintf("INSERT INTO demotable VALUES(%d, '%s', '%s')", data.Id, data.Content, data.Vector) + _, err := client.Exec(execSql) + return err +} +``` +- 删除 +```go +func DeleteData(client *sql.DB) error { + execSql := fmt.Sprint("DELETE FROM demotable where id > 10") + _, err := client.Exec(execSql) + return err +} +``` +- 更新 +```go +func UpdateData(client *sql.DB, vector string) error { + execSql := fmt.Sprintf("UPDATE demotable set embedding = '%s' where id = 10", vector) + _, err := client.Exec(execSql) + return err +} +``` +### 5.查询 +```go +func FindNearestVectors(client *sql.DB, efsearch int, vector string, topK int) []string { + var res []string + // 设置查询参数 + paramsql := fmt.Sprintf("set hnsw_ef_search = %d", efsearch); + querysql := fmt.Sprintf("SELECT * FROM demotable ORDER BY embedding <-> '%s' LIMIT %d;", vector, topK); + _, _ = client.Exec(paramsql) + rows, _ := client.Query(querysql) + for rows.Next() { + var id int + var content string + var vector []byte + _ = rows.Scan(&id, &content, &vector) + embedding := string(vector) + row := fmt.Sprintf("id: %d, content: %s, embedding: %s", id, content, embedding); + res = append(res, row) + } + return res +} +``` +[更多操作示例参考](https://gitee.com/opengauss/openGauss-connector-go-pq) \ No newline at end of file -- Gitee From a650d4f83621e9312edff6b75ff2cc00ddfde620 Mon Sep 17 00:00:00 2001 From: Eureka Date: Mon, 24 Mar 2025 21:09:25 +0800 Subject: [PATCH 2/2] data vec go sdk --- content/docs-lite/zh/docs/DataVec/integrationGo.md | 14 ++++++++------ content/zh/docs/DataVec/integrationGo.md | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/content/docs-lite/zh/docs/DataVec/integrationGo.md b/content/docs-lite/zh/docs/DataVec/integrationGo.md index 845022e8f..1bc4c1c65 100644 --- a/content/docs-lite/zh/docs/DataVec/integrationGo.md +++ b/content/docs-lite/zh/docs/DataVec/integrationGo.md @@ -1,11 +1,12 @@ # Go SDK对接向量数据库 -本文介绍如何使用Go语言调用openGauss向量数据库 +本文介绍如何使用Go语言调用openGauss向量数据库。 -## 要求 -- 安装Go 1.19及以上版本 +## 环境要求 +- 安装Go 1.19及以上版本。 +- openGauss数据库安装部署 [容器镜像安装](../InstallationGuide/容器镜像安装.md)。 ## 安装SDK -开发者可以运行以下命令安装GO SDK[社区官网](http://gitee.com/opengauss/openGauss-connector-go-pq),并在项目中导入该包 +开发者可以运行以下命令安装GO SDK[官方仓库](http://gitee.com/opengauss/openGauss-connector-go-pq),并在项目中导入该包。 ``` 安装SDK go get gitee.com/opengauss/openGauss-connector-go-pq @@ -21,7 +22,8 @@ import ( ## 基本操作 ### 1.连接数据库 ```go -// connectInfo格式: "host=127.0.0.1 port=5432 user=username password=userpassword dbname=userdbname sslmode=disable" +// connectInfo格式: +// "host=127.0.0.1 port=5432 user=username password=userpassword dbname=userdbname sslmode=disable" func CreateDBClient(connectInfo string) (*sql.DB, error) { return sql.Open("opengauss", connectInfo) } @@ -76,7 +78,7 @@ func UpdateData(client *sql.DB, vector string) error { ``` ### 5.查询 ```go -func FindNearestVectors(client *sql.DB, efsearch int, vector string, topK int) []string { +func SearchVectors(client *sql.DB, efsearch int, vector string, topK int) []string { var res []string // 设置查询参数 paramsql := fmt.Sprintf("set hnsw_ef_search = %d", efsearch); diff --git a/content/zh/docs/DataVec/integrationGo.md b/content/zh/docs/DataVec/integrationGo.md index 845022e8f..1bc4c1c65 100644 --- a/content/zh/docs/DataVec/integrationGo.md +++ b/content/zh/docs/DataVec/integrationGo.md @@ -1,11 +1,12 @@ # Go SDK对接向量数据库 -本文介绍如何使用Go语言调用openGauss向量数据库 +本文介绍如何使用Go语言调用openGauss向量数据库。 -## 要求 -- 安装Go 1.19及以上版本 +## 环境要求 +- 安装Go 1.19及以上版本。 +- openGauss数据库安装部署 [容器镜像安装](../InstallationGuide/容器镜像安装.md)。 ## 安装SDK -开发者可以运行以下命令安装GO SDK[社区官网](http://gitee.com/opengauss/openGauss-connector-go-pq),并在项目中导入该包 +开发者可以运行以下命令安装GO SDK[官方仓库](http://gitee.com/opengauss/openGauss-connector-go-pq),并在项目中导入该包。 ``` 安装SDK go get gitee.com/opengauss/openGauss-connector-go-pq @@ -21,7 +22,8 @@ import ( ## 基本操作 ### 1.连接数据库 ```go -// connectInfo格式: "host=127.0.0.1 port=5432 user=username password=userpassword dbname=userdbname sslmode=disable" +// connectInfo格式: +// "host=127.0.0.1 port=5432 user=username password=userpassword dbname=userdbname sslmode=disable" func CreateDBClient(connectInfo string) (*sql.DB, error) { return sql.Open("opengauss", connectInfo) } @@ -76,7 +78,7 @@ func UpdateData(client *sql.DB, vector string) error { ``` ### 5.查询 ```go -func FindNearestVectors(client *sql.DB, efsearch int, vector string, topK int) []string { +func SearchVectors(client *sql.DB, efsearch int, vector string, topK int) []string { var res []string // 设置查询参数 paramsql := fmt.Sprintf("set hnsw_ef_search = %d", efsearch); -- Gitee