本节为您介绍如何使用Connection
对象的方法向图集插入或从图集删除点和边。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
图数据模型示例
以下各示例为您展示如何在具有以下schema和属性的图集里插入或删除点和边:
数据属性类型映射关系
插入点或边时,需指定不同属性类型。嬴图属性类型和Go/驱动程序数据类型的映射关系如下:
嬴图支持的属性类型 | Go/驱动程序支持的数据类型 |
---|---|
int32 | int32 |
uint32 | uint32 |
int64 | int64 |
uint64 | uint64 |
float | float32 |
double | float64 |
decimal | 支持多种数值类型 (int32 , int64 , float32 , float64 , uint32 , uint64 ) 和 string |
string | string |
text | string |
datetime | string |
timestamp | string |
point | string , type |
blob | []byte{} , 支持多种数值类型 (int32 , int64 , float32 , float64 , uint32 , uint64 ) 和 string |
list | slice |
set | slice |
插入
InsertNodes()
向当前图集的某个schema插入新的点。
参数:
string
:schema名称。[]Node
:待插入的Node
列表。InsertRequestConfig
(可选):配置请求。如果填入nil
,函数将使用默认的配置设置。
返回值:
Response
:请求的结果。若将InsertRequestConfig.silent
设定为true,Response
对象会包含一个nodes
别名,所有插入的点均在其中。error
:一个错误对象,包含有关发生的任何问题的详细信息。如果操作成功,则返回nil
。
// 向图集lcc名为user的schema插入两个点,打印错误代码和插入点的信息
requestConfig := &configuration.RequestConfig{
UseMaster: true,
GraphName: "lcc",
}
// 如果InsertRequestConfig.Slient设定为false,Response不返回别名
insertRequestConfig := &configuration.InsertRequestConfig{
RequestConfig: requestConfig,
InsertType: ultipa.InsertType_NORMAL,
Silent: true,
}
var nodes []*structs.Node
node1 := structs.NewNode()
node1.UUID = 1
node1.ID = "U001"
node1.Set("name", "Alice")
node1.Set("age", 18)
node1.Set("score", 65.32)
node1.Set("birthday", "1993-5-4")
node1.Set("location", "point(23.63 104.25)")
node1.Set("profile", "abc")
node1.Set("interests", []string{"tennis", "violin"})
node1.Set("permissionCodes", []int32{2004, 3025, 1025})
node2 := structs.NewNode()
node2.UUID = 2
node2.ID = "U002"
node2.Set("name", "Bob")
nodes = append(nodes, node1, node2)
myInsert, _ := conn.InsertNodes("user", nodes, insertRequestConfig)
println("Operation succeeds:", myInsert.Status.IsSuccess())
nodeList, schemaList, _ := myInsert.Alias("nodes").AsNodes()
printers.PrintNodes(nodeList, schemaList)
Operation succeeds: true
+------+------+--------+-------+-------+---------------+--------------------------+-----------------------------+------------+-----------------+------------------+
| ID | UUID | Schema | name | age | score | birthday | location | profile | interests | permissionCodes |
+------+------+--------+-------+-------+---------------+--------------------------+-----------------------------+------------+-----------------+------------------+
| U001 | 1 | user | Alice | 18 | 65.3200000000 | 1993-05-04T00:00:00.000Z | POINT(23.630000 104.250000) | [97 98 99] | [tennis violin] | [2004 3025 1025] |
| U002 | 2 | user | Bob | <nil> | <nil> | <nil> | <nil> | <nil> | <nil> | <nil> |
+------+------+--------+-------+-------+---------------+--------------------------+-----------------------------+------------+-----------------+------------------+
InsertEdges()
向当前图集的某个schema插入新的边。
参数:
string
:schema名称。[]Edge
:待插入的Edge
列表。InsertRequestConfig
(可选):配置请求。如果填入nil
,函数将使用默认的配置设置。
返回值:
Response
:请求的结果。若将InsertRequestConfig.Slient
设定为true,Response
对象会包含一个edges
别名,所有插入的边均在其中。error
:一个错误对象,包含有关发生的任何问题的详细信息。如果操作成功,则返回nil
。
// 向图集lcc名为follows的schema插入两条边,打印错误代码和插入边的信息
requestConfig := &configuration.RequestConfig{
UseMaster: true,
GraphName: "lcc",
}
// 如果InsertRequestConfig.Slient设定为false,Response不返回别名
insertRequestConfig := &configuration.InsertRequestConfig{
RequestConfig: requestConfig,
InsertType: ultipa.InsertType_NORMAL,
Silent: true,
}
var edges []*structs.Edge
edge1 := structs.NewEdge()
edge1.UUID = 1
edge1.From = "U001"
edge1.To = "U002"
edge1.Set("createdOn", "2024-5-6")
edge2 := structs.NewEdge()
edge2.UUID = 2
edge2.From = "U002"
edge2.To = "U001"
edge2.Set("createdOn", "2024-5-8")
edges = append(edges, edge1, edge2)
myInsert, err := conn.InsertEdges("follows", edges, insertRequestConfig)
if err != nil {
println(err)
}
println("Operation succeeds:", myInsert.Status.IsSuccess())
edgeList, schemaList, _ := myInsert.Alias("edges").AsEdges()
printers.PrintEdges(edgeList, schemaList)
Operation succeeds: true
+------+-----------+------+---------+------+---------+-------------------------------+
| UUID | FROM_UUID | FROM | TO_UUID | TO | SCHEMA | createdOn |
+------+-----------+------+---------+------+---------+-------------------------------+
| 1 | 1 | U001 | 2 | U002 | follows | 2024-05-06T00:00:00.000+08:00 |
| 2 | 2 | U002 | 1 | U001 | follows | 2024-05-08T00:00:00.000+08:00 |
+------+-----------+------+---------+------+---------+-------------------------------+
InsertNodesBatchBySchema()
通过gRPC向当前图集的某个schema插入新点。插入的点,其属性必须和schema结构中的声明保持一致。
参数:
Schema
:目标schema。[]Node
:待插入的Node
列表。InsertRequestConfig
(可选):配置请求。如果填入nil
,函数将使用默认的配置设置。
返回值:
InsertResponse
:请求的结果。error
:一个错误对象,包含有关发生的任何问题的详细信息。如果操作成功,则返回nil
。
// 向图集lcc名为user的schema插入两个点,打印错误代码和插入结果
requestConfig := &configuration.RequestConfig{
UseMaster: true,
GraphName: "lcc",
}
insertRequestConfig := &configuration.InsertRequestConfig{
RequestConfig: requestConfig,
InsertType: ultipa.InsertType_OVERWRITE,
}
schema := structs.NewSchema("user")
schema.Properties = append(schema.Properties, &structs.Property{
Name: "name",
Type: ultipa.PropertyType_STRING,
}, &structs.Property{
Name: "age",
Type: ultipa.PropertyType_INT32,
}, &structs.Property{
Name: "score",
Type: ultipa.PropertyType_DECIMAL,
}, &structs.Property{
Name: "birthday",
Type: ultipa.PropertyType_DATETIME,
}, &structs.Property{
Name: "location",
Type: ultipa.PropertyType_POINT,
}, &structs.Property{
Name: "profile",
Type: ultipa.PropertyType_BLOB,
}, &structs.Property{
Name: "interests",
Type: ultipa.PropertyType_LIST,
SubTypes: []ultipa.PropertyType{ultipa.PropertyType_STRING},
}, &structs.Property{
Name: "permissionCodes",
Type: ultipa.PropertyType_SET,
SubTypes: []ultipa.PropertyType{ultipa.PropertyType_INT32},
})
var nodes []*structs.Node
node1 := structs.NewNode()
node1.UUID = 1
node1.ID = "U001"
node1.Set("name", "Alice")
node1.Set("age", 18)
node1.Set("score", 65.32)
node1.Set("birthday", "1993-5-4")
node1.Set("location", "point(23.63 104.25)")
node1.Set("profile", "abc")
node1.Set("interests", []string{"tennis", "violin"})
node1.Set("permissionCodes", []int32{2004, 3025, 1025})
node2 := structs.NewNode()
node2.UUID = 2
node2.ID = "U002"
node2.Set("name", "Bob")
node2.Set("age", nil)
node2.Set("score", nil)
node2.Set("birthday", nil)
node2.Set("location", nil)
node2.Set("profile", nil)
node2.Set("interests", nil)
node2.Set("permissionCodes", nil)
nodes = append(nodes, node1, node2)
myInsert, err := conn.InsertNodesBatchBySchema(schema, nodes, insertRequestConfig)
if err != nil {
println(err)
}
println("Operation succeeds:", myInsert.Status.IsSuccess())
Operation succeeds: true
InsertEdgesBatchBySchema()
通过gRPC向当前图集的某个schema插入新边。插入的边,其属性必须和schema结构中的声明保持一致。
参数:
Schema
:目标schema。[]Edge
:待插入的Edge
列表。InsertRequestConfig
(可选):配置请求。如果填入nil
,函数将使用默认的配置设置。
返回值:
InsertResponse
:请求的结果。error
:一个错误对象,包含有关发生的任何问题的详细信息。如果操作成功,则返回nil
。
// 向图集lcc名为follows的schema插入两条边,打印错误代码
requestConfig := &configuration.RequestConfig{
UseMaster: true,
GraphName: "lcc",
}
insertRequestConfig := &configuration.InsertRequestConfig{
RequestConfig: requestConfig,
InsertType: ultipa.InsertType_OVERWRITE,
}
schema := structs.NewSchema("follows")
schema.Properties = append(schema.Properties, &structs.Property{
Name: "createdOn",
Type: ultipa.PropertyType_TIMESTAMP,
})
var edges []*structs.Edge
edge1 := structs.NewEdge()
edge1.UUID = 1
edge1.From = "U001"
edge1.To = "U002"
edge1.Set("createdOn", "2024-5-6")
edge2 := structs.NewEdge()
edge2.UUID = 2
edge2.From = "U002"
edge2.To = "U001"
edge2.Set("createdOn", "2024-5-8")
edges = append(edges, edge1, edge2)
myInsert, err := conn.InsertEdgesBatchBySchema(schema, edges, insertRequestConfig)
if err != nil {
println(err)
}
println("Operation succeeds:", myInsert.Status.IsSuccess())
Operation succeeds: true
InsertNodesBatchAuto()
通过gRPC向当前图集的一个或多个schema插入新点。插入的点,其属性必须和schema结构中的声明保持一致。
参数:
[]Node
:待插入的Node
列表。InsertRequestConfig
(可选):配置请求。如果填入nil
,函数将使用默认的配置设置。
返回值:
InsertBatchAutoResponse
:请求的结果。error
:一个错误对象,包含有关发生的任何问题的详细信息。如果操作成功,则返回nil
。
// 向图集lcc名为user和product的两个schema各插入一个点,打印错误代码和插入结果
requestConfig := &configuration.RequestConfig{
UseMaster: true,
GraphName: "lcc",
}
insertRequestConfig := &configuration.InsertRequestConfig{
RequestConfig: requestConfig,
InsertType: ultipa.InsertType_NORMAL,
}
var N1 []*structs.Node
node1 := structs.NewNode()
node1.Schema = "user"
node1.UUID = 1
node1.ID = "U001"
node1.Set("name", "Alice")
node1.Set("age", 18)
node1.Set("score", 65.32)
node1.Set("birthday", "1993-5-4")
node1.Set("location", "point(23.63 104.25)")
node1.Set("profile", "abc")
node1.Set("interests", []string{"tennis", "violin"})
node1.Set("permissionCodes", []int32{2004, 3025, 1025})
N1 = append(N1, node1)
insert1, err := conn.InsertNodesBatchAuto(N1, insertRequestConfig)
if err != nil {
println(err)
}
for _, item := range insert1.Resps {
println("Operation succeeds:", item.Status.IsSuccess())
}
var N2 []*structs.Node
node2 := structs.NewNode()
node2.Schema = "user"
node2.UUID = 2
node2.ID = "U002"
node2.Set("name", "Bob")
node2.Set("age", nil)
node2.Set("score", nil)
node2.Set("birthday", nil)
node2.Set("location", nil)
node2.Set("profile", nil)
node2.Set("interests", nil)
node2.Set("permissionCodes", nil)
N2 = append(N2, node2)
insert2, err := conn.InsertNodesBatchAuto(N2, insertRequestConfig)
if err != nil {
println(err)
}
println(insert1.ErrorCode)
for _, item := range insert2.Resps {
println("Operation succeeds:", item.Status.IsSuccess())
}
var N3 []*structs.Node
node3 := structs.NewNode()
node3.Schema = "product"
node3.UUID = 3
node3.ID = "P001"
node3.Set("name", "Wireless Earbud")
node3.Set("price", float32(93.2))
N3 = append(N3, node3)
insert3, err := conn.InsertNodesBatchAuto(N3, insertRequestConfig)
if err != nil {
println(err)
}
for _, item := range insert3.Resps {
println("Operation succeeds:", item.Status.IsSuccess())
}
Operation succeeds: true
Operation succeeds: true
Operation succeeds: true
InsertEdgesBatchAuto()
通过gRPC向当前图集的一个或多个schema插入新边。边的属性值必须与对应schema结构中定义的属性值类型一致。
参数:
[]Edge
:待插入的Edge
列表。InsertRequestConfig
(可选):配置请求。如果填入nil
,函数将使用默认的配置设置。
返回值:
InsertBatchAutoResponse
:请求的结果。error
:一个错误对象,包含有关发生的任何问题的详细信息。如果操作成功,则返回nil
。
// 向图集lcc名为follows的schema插入两条边,向名为purchased的schema插入一条边,打印错误代码
requestConfig := &configuration.RequestConfig{
UseMaster: true,
GraphName: "lcc",
}
insertRequestConfig := &configuration.InsertRequestConfig{
RequestConfig: requestConfig,
InsertType: ultipa.InsertType_NORMAL,
}
var E1 []*structs.Edge
edge1 := structs.NewEdge()
edge1.Schema = "follows"
edge1.UUID = 1
edge1.From = "U001"
edge1.To = "U002"
edge1.Set("createdOn", "2024-5-6")
E1 = append(E1, edge1)
insert1, err := conn.InsertEdgesBatchAuto(E1, insertRequestConfig)
if err != nil {
println(err)
}
for _, item := range insert1.Resps {
println("Operation succeeds:", item.Status.IsSuccess())
}
var E2 []*structs.Edge
edge2 := structs.NewEdge()
edge2.Schema = "follows"
edge2.UUID = 2
edge2.From = "U002"
edge2.To = "U001"
edge2.Set("createdOn", "2024-5-8")
E2 = append(E2, edge2)
insert2, err := conn.InsertEdgesBatchAuto(E2, insertRequestConfig)
if err != nil {
println(err)
}
println(insert1.ErrorCode)
for _, item := range insert2.Resps {
println("Operation succeeds:", item.Status.IsSuccess())
}
var E3 []*structs.Edge
edge3 := structs.NewEdge()
edge3.Schema = "purchased"
edge3.UUID = 3
edge3.From = "U002"
edge3.To = "P001"
edge3.Set("qty", 1)
E3 = append(E3, edge3)
insert3, err := conn.InsertEdgesBatchAuto(E3, insertRequestConfig)
if err != nil {
println(err)
}
for _, item := range insert3.Resps {
println("Operation succeeds:", item.Status.IsSuccess())
}
Operation succeeds: true
Operation succeeds: true
Operation succeeds: true
Deletion
DeleteNodes()
从图集删除符合指定条件的点。需特别注意,删除点的同时也会删除与点相连的边。
参数:
string
:待删除的点的条件。InsertRequestConfig
(可选):配置请求。如果填入nil
,函数将使用默认的配置设置。
返回值:
Response
:请求的结果。若将InsertRequestConfig.Slient
设定为true,Response
对象会包含一个nodes
别名,所有删除的点均在其中。error
:一个错误对象,包含有关发生的任何问题的详细信息。如果操作成功,则返回nil
。
// 删除图集lcc里@user下名为Alice的点,打印错误代码
// 与删除的点相连的边也会全部删除
requestConfig := &configuration.RequestConfig{
UseMaster: true,
GraphName: "lcc",
}
insertRequestConfig := &configuration.InsertRequestConfig{
RequestConfig: requestConfig,
InsertType: ultipa.InsertType_NORMAL,
}
myDeletion, _ := conn.DeleteNodes("{@user.name == 'Alice'}", insertRequestConfig)
println("Operation succeeds:", myDeletion.Status.IsSuccess())
Operation succeeds: true
DeleteEdges()
从图集删除符合指定条件的边。
参数:
string
:待删除的边的条件。InsertRequestConfig
(可选):配置请求。如果填入nil
,函数将使用默认的配置设置。
返回值:
Response
:请求的结果。当InsertRequestConfig.Silent
设置为true时,Response
对象包含一个别名edges
,用于保存所有被删除的边。error
:一个错误对象,包含有关发生的任何问题的详细信息。如果操作成功,则返回nil
。
// 从图集lcc删除@purchased中的全部边,打印错误代码
requestConfig := &configuration.RequestConfig{
UseMaster: true,
GraphName: "lcc",
}
insertRequestConfig := &configuration.InsertRequestConfig{
RequestConfig: requestConfig,
InsertType: ultipa.InsertType_NORMAL,
}
deleteEdge, _ := conn.DeleteEdges("{@purchased}", insertRequestConfig)
println("Operation succeeds:", deleteEdge.Status.IsSuccess()
Operation succeeds: true
完整示例
package main
import (
ultipa "github.com/ultipa/ultipa-go-sdk/rpc"
"github.com/ultipa/ultipa-go-sdk/sdk"
"github.com/ultipa/ultipa-go-sdk/sdk/configuration"
"github.com/ultipa/ultipa-go-sdk/sdk/structs"
"github.com/ultipa/ultipa-go-sdk/utils"
)
func main() {
// 设置连接
// URI 示例: hosts="mqj4zouys.us-east-1.cloud.ultipa.com:60010"
config, _ := configuration.NewUltipaConfig(&configuration.UltipaConfig{
Hosts: []string{"192.168.1.85:60061", "192.168.1.86:60061", "192.168.1.87:60061"},
Username: "***",
Password: "***",
})
// 建立与数据库的连接
conn, _ := sdk.NewUltipa(config)
// 请求配置
requestConfig := &configuration.RequestConfig{
UseMaster: true,
GraphName: "lcc",
}
// 配置插入请求
insertRequestConfig := &configuration.InsertRequestConfig{
RequestConfig: requestConfig,
InsertType: ultipa.InsertType_NORMAL,
}
// 向图集lcc中名为user的schema插入两个点,向名为product的schema插入一个点,打印错误代码
var N1 []*structs.Node
node1 := structs.NewNode()
node1.Schema = "user"
node1.UUID = 1
node1.ID = "U001"
node1.Set("name", "Alice")
node1.Set("age", 18)
node1.Set("score", 65.32)
node1.Set("birthday", "1993-5-4")
node1.Set("location", "point(23.63 104.25)")
node1.Set("profile", "abc")
node1.Set("interests", []string{"tennis", "violin"})
node1.Set("permissionCodes", []int32{2004, 3025, 1025})
N1 = append(N1, node1)
insert1, err := conn.InsertNodesBatchAuto(N1, insertRequestConfig)
if err != nil {
println(err)
}
for _, item := range insert1.Resps {
println("Operation succeeds:", item.Status.IsSuccess())
}
var N2 []*structs.Node
node2 := structs.NewNode()
node2.Schema = "user"
node2.UUID = 2
node2.ID = "U002"
node2.Set("name", "Bob")
node2.Set("age", nil)
node2.Set("score", nil)
node2.Set("birthday", nil)
node2.Set("location", nil)
node2.Set("profile", nil)
node2.Set("interests", nil)
node2.Set("permissionCodes", nil)
N2 = append(N2, node2)
insert2, err := conn.InsertNodesBatchAuto(N2, insertRequestConfig)
if err != nil {
println(err)
}
println(insert1.ErrorCode)
for _, item := range insert2.Resps {
println("Operation succeeds:", item.Status.IsSuccess())
}
var N3 []*structs.Node
node3 := structs.NewNode()
node3.Schema = "product"
node3.UUID = 3
node3.ID = "P001"
node3.Set("name", "Wireless Earbud")
node3.Set("price", float32(93.2))
N3 = append(N3, node3)
insert3, err := conn.InsertNodesBatchAuto(N3, insertRequestConfig)
if err != nil {
println(err)
}
for _, item := range insert3.Resps {
println("Operation succeeds:", item.Status.IsSuccess())
}
};