本节介绍用于插入点、边的方法。
方法 | 机制 | 使用场景 | 备注 |
---|---|---|---|
insertNodes() insertEdges() |
使用UQL | 插入少量点或边 | |
insertNodesBatchBySchema() insertEdgesBatchBySchema() |
使用gRPC将数据直接发送到服务器 | 插入大量相同schema的点或边 | 需使用与嬴图属性类型对应的Go数据类型来给属性赋值(参见属性类型映射) |
insertNodesBatchAuto() insertEdgesBatchAuto() |
插入大量不同schema的点或边 |
属性类型映射
嬴图属性类型和Go数据类型的映射关系如下:
嬴图属性类型 |
Go数据类型 |
示例 |
---|---|---|
INT32 |
int32 |
18 |
UINT32 |
uint32 |
18 |
INT64 |
int64 |
18 |
UINT64 |
uint64 |
18 |
FLOAT |
float32 |
170.5 |
DOUBLE |
float64 |
65.32 |
DECIMAL |
int32 、int64 、float32 、float64 、uint32 、uint64 或string |
18 , 170.5 , "65.32" |
STRING |
string |
"John Doe" |
TEXT |
string |
"John Doe" |
DATETIME |
string [1] |
"1993-05-06" |
TIMESTAMP |
string [1]或int |
"1993-05-06" ,"1715169600" |
BOOL |
bool |
true 或false |
POINT |
string , type |
"point({latitude: 132.1, longitude: -1.5})" |
LIST |
slice |
["tennis", "violin"] |
SET |
slice |
[2004, 3025, 1025] |
[1] 支持的日期字符串格式包括[YY]YY-MM-DD HH:MM:SS
、[YY]YY-MM-DD HH:MM:SSZ
、[YY]YY-MM-DDTHH:MM:SSZ
、[YY]YY-MM-DDTHH:MM:SSXX
、[YY]YY-MM-DDTHH:MM:SSXXX
、[YY]YY-MM-DD HH:MM:SS.SSS
以及它们的变体。
示例图结构
本节示例的点、边插入和删除基于如下的Schema和属性定义进行:

参考此示例创建这个图结构。
InsertNodes()
向图中某个Schema插入点。
参数
schemaName: str
:Schema名称。nodes: []*structs.Node
:待插入的点列表。confg: *configuration.RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Inserts two 'user' nodes into the graph 'social'
requestConfig := &configuration.RequestConfig{
Graph: "social",
}
insertRequestConfig := &configuration.InsertRequestConfig{
RequestConfig: requestConfig,
}
nodes := []*structs.Node{
{
ID: "U1",
Values: &structs.Values{
Data: map[string]interface{}{
"name": "Alice",
"age": 18,
"score": 65.32,
"birthday": "1993-5-4",
"active": 0,
"location": "point({latitude: 132.1, longitude: -1.5})",
"interests": []string{"tennis", "violin"},
"permissionCodes": []int32{2004, 3025, 1025},
},
},
},
{
ID: "U2",
Values: &structs.Values{
Data: map[string]interface{}{
"name": "Bob",
},
},
},
}
response, _ := driver.InsertNodes("user", nodes, insertRequestConfig)
if response.Status.Code.String() == "SUCCESS" {
fmt.Println(response.Status.Code)
} else {
fmt.Println(response.Status.Message)
}
SUCCESS
InsertEdges()
向图中某个Schema插入边。
参数
schemaName: str
:Schema名称。edges: []*structs.Edge
:待插入的边列表;每个Edge
的From
和To
字段必填。confg: *configuration.RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Inserts two 'follows' edges to the graph 'social'
requestConfig := &configuration.RequestConfig{
Graph: "social",
}
insertRequestConfig := &configuration.InsertRequestConfig{
RequestConfig: requestConfig,
}
edges := []*structs.Edge{
{
From: "U1",
To: "U2",
Values: &structs.Values{
Data: map[string]interface{}{
"createdOn": "2024-5-6",
"weight": 3.2,
},
},
},
{
From: "U2",
To: "U1",
Values: &structs.Values{
Data: map[string]interface{}{
"createdOn": 1715169600,
},
},
},
}
response, _ := driver.InsertEdges("follows", edges, insertRequestConfig)
if response.Status.Code.String() == "SUCCESS" {
fmt.Println(response.Status.Code)
} else {
fmt.Println(response.Status.Message)
}
SUCCESS
InsertNodesBatchBySchema()
通过gRPC向图中某个Schema插入点。此方法针对批量插入进行了优化。
参数
schema: *structs.Schema
:目标Schema;字段Name
必填,Properties
包含图中对应Schema关联的部分或所有属性。nodes: []*structs.Node
:待插入的点列表;每个Node
的Values
字段必须与Schema.Properties
结构相同。confg: *configuration.RequestConfig
(可选):请求配置。
返回值
InsertResponse
:插入请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Inserts two 'user' nodes into the graph 'social'
requestConfig := &configuration.RequestConfig{
Graph: "social",
}
insertRequestConfig := &configuration.InsertRequestConfig{
RequestConfig: requestConfig,
}
schema := &structs.Schema{
Name: "user",
Properties: []*structs.Property{
{Name: "name", Type: ultipa.PropertyType_STRING},
// {Name: "age", Type: ultipa.PropertyType_INT32},
{Name: "score", Type: ultipa.PropertyType_DECIMAL, DecimalExtra: &structs.DecimalExtra{Precision: 25, Scale: 10}},
{Name: "birthday", Type: ultipa.PropertyType_DATETIME},
{Name: "active", Type: ultipa.PropertyType_BOOL},
{Name: "location", Type: ultipa.PropertyType_POINT},
{Name: "interests", Type: ultipa.PropertyType_LIST, SubTypes: []ultipa.PropertyType{ultipa.PropertyType_STRING}},
{Name: "permissionCodes", Type: ultipa.PropertyType_SET, SubTypes: []ultipa.PropertyType{ultipa.PropertyType_INT32}},
},
}
nodes := []*structs.Node{
{
ID: "U1",
Values: &structs.Values{
Data: map[string]interface{}{
"name": "Alice",
// "age": 18,
"score": 65.32,
"birthday": "1993-5-4",
"active": false,
"location": types.NewPoint(132.1, -1.5),
"interests": []string{"tennis", "violin"},
"permissionCodes": []int32{2004, 3025, 1025},
},
},
},
{
ID: "U2",
Values: &structs.Values{
Data: map[string]interface{}{
"name": "Bob",
// "age": nil,
"score": nil,
"birthday": nil,
"active": nil,
"location": nil,
"interests": nil,
"permissionCodes": nil,
},
},
},
}
insertResponse, err := driver.InsertNodesBatchBySchema(schema, nodes, insertRequestConfig)
if err != nil {
log.Fatalf("Insert failed: %v", err)
}
if insertResponse != nil && len(insertResponse.ErrorItem) > 0 {
fmt.Println("Error items:", insertResponse.ErrorItem)
} else {
fmt.Println("All nodes inserted successfully")
}
All nodes inserted successfully
InsertEdgesBatchBySchema()
通过gRPC向图中某个Schema插入边。此方法针对批量插入进行了优化。
参数
schema: *structs.Schema
:目标Schema;字段Name
必填,Properties
包含图中对应Schema关联的部分或所有属性。edges: []*structs.Edge
: 待插入的边列表;每个Edge
的From
和To
字段必填;每个Edge
的Values
字段必须与Schema.Properties
结构相同。confg: *configuration.RequestConfig
(可选):请求配置。
返回值
InsertResponse
:插入请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Inserts two 'follows' edges into the graph 'social'
requestConfig := &configuration.RequestConfig{
Graph: "social",
}
insertRequestConfig := &configuration.InsertRequestConfig{
RequestConfig: requestConfig,
}
schema := &structs.Schema{
Name: "follows",
Properties: []*structs.Property{
// {Name: "createdOn", Type: ultipa.PropertyType_TIMESTAMP},
{Name: "weight", Type: ultipa.PropertyType_FLOAT},
},
}
edges := []*structs.Edge{
{
From: "U1",
To: "U2",
Values: &structs.Values{
Data: map[string]interface{}{
// "createdOn": "2024-5-6",
"weight": float32(3.2),
},
},
},
{
From: "U2",
To: "U1",
Values: &structs.Values{
Data: map[string]interface{}{
// "createdOn": 1715169600,
"weight": nil,
},
},
},
}
insertResponse, err := driver.InsertEdgesBatchBySchema(schema, edges, insertRequestConfig)
if err != nil {
log.Fatalf("Insert failed: %v", err)
}
if insertResponse != nil && len(insertResponse.ErrorItem) > 0 {
fmt.Println("Error items:", insertResponse.ErrorItem)
} else {
fmt.Println("All edges inserted successfully")
}
All edges inserted successfully
InsertNodesBatchAuto()
通过gRPC向图中一个或多个Schema插入点。此方法针对批量插入进行了优化。
参数
nodes:List[Node]
:待插入的点列表;每个Node
的Schema
必填,所有点的Values
结构相同并且包含图中对应Schema关联的所有属性。confg: *configuration.RequestConfig
(可选):请求配置。
返回值
map[string]*http.InsertResponse
:Schema名称,插入请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Inserts two 'user' nodes and a 'product' node into the graph 'social'
requestConfig := &configuration.RequestConfig{
Graph: "social",
}
insertRequestConfig := &configuration.InsertRequestConfig{
RequestConfig: requestConfig,
}
nodes := []*structs.Node{
{
Schema: "user",
ID: "U1",
Values: &structs.Values{
Data: map[string]interface{}{
"name": "Alice",
"age": 18,
"score": 65.32,
"birthday": "1993-5-4",
"active": false,
"location": types.NewPoint(132.1, -1.5),
"interests": []string{"tennis", "violin"},
"permissionCodes": []int32{2004, 3025, 1025},
},
},
},
{
Schema: "user",
ID: "U2",
Values: &structs.Values{
Data: map[string]interface{}{
"name": "Bob",
"age": nil,
"score": nil,
"birthday": nil,
"active": nil,
"location": nil,
"interests": nil,
"permissionCodes": nil,
},
},
},
{
Schema: "product",
Values: &structs.Values{
Data: map[string]interface{}{
"name": "Wireless Earbud",
"price": float32(93.2),
},
},
},
}
result, err := driver.InsertNodesBatchAuto(nodes, insertRequestConfig)
if err != nil {
log.Fatalf("Insert failed: %v", err)
}
for schemaName, insertResponse := range result {
if len(insertResponse.ErrorItem) > 0 {
fmt.Println("Error items of", schemaName, "nodes:", insertResponse.ErrorItem)
} else {
fmt.Println("All", schemaName, "nodes inserted successfully")
}
}
All product nodes inserted successfully
All user nodes inserted successfully
InsertEdgesBatchAuto()
通过gRPC向图中一个或多个Schema插入边。此方法针对批量插入进行了优化。
参数
edges: List[Edge]
:待插入的边列表;每个Edge
的Schema
、From
和To
必填,所有边的Values
结构相同并且包含图中对应Schema关联的所有属性。confg: *configuration.RequestConfig
(可选):请求配置。
返回值
map[string]*http.InsertResponse
:Schema名称,插入请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Inserts two 'follows' edges and a 'purchased' edge into the graph 'social'
requestConfig := &configuration.RequestConfig{
Graph: "social",
}
insertRequestConfig := &configuration.InsertRequestConfig{
RequestConfig: requestConfig,
}
edges := []*structs.Edge{
{
Schema: "follows",
From: "U1",
To: "U2",
Values: &structs.Values{
Data: map[string]interface{}{
"createdOn": "2024-5-6",
"weight": float32(3.2),
},
},
},
{
Schema: "follows",
From: "U2",
To: "U1",
Values: &structs.Values{
Data: map[string]interface{}{
"createdOn": 1715169600,
"weight": nil,
},
},
},
{
Schema: "purchased",
From: "U2",
To: "684bd6a70000020020000001",
},
}
result, err := driver.InsertEdgesBatchAuto(edges, insertRequestConfig)
if err != nil {
log.Fatalf("Insert failed: %v", err)
}
for schemaName, insertResponse := range result {
if len(insertResponse.ErrorItem) > 0 {
fmt.Println("Error items of", schemaName, "edges:", insertResponse.ErrorItem)
} else {
fmt.Println("All", schemaName, "edges inserted successfully")
}
}
All follows edges inserted successfully
All purchased edges inserted successfully
完整示例
package main
import (
"fmt"
"log"
"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/sdk/types"
)
func main() {
config := &configuration.UltipaConfig{
// URI example: Hosts: []string{"mqj4zouys.us-east-1.cloud.ultipa.com:60010"},
Hosts: []string{"192.168.1.85:60061", "192.168.1.87:60061", "192.168.1.88:60061"},
Username: "<usernmae>",
Password: "<password>",
}
driver, err := sdk.NewUltipaDriver(config)
if err != nil {
log.Fatalln("Failed to connect to Ultipa:", err)
}
// Inserts two 'user' nodes, a 'product' node, two 'follows' edges, and a 'purchased' edge into the graph 'social'
requestConfig := &configuration.RequestConfig{
Graph: "social",
}
insertRequestConfig := &configuration.InsertRequestConfig{
RequestConfig: requestConfig,
}
nodes := []*structs.Node{
{
Schema: "user",
ID: "U1",
Values: &structs.Values{
Data: map[string]interface{}{
"name": "Alice",
"age": 18,
"score": 65.32,
"birthday": "1993-5-4",
"active": false,
"location": types.NewPoint(132.1, -1.5),
"interests": []string{"tennis", "violin"},
"permissionCodes": []int32{2004, 3025, 1025},
},
},
},
{
Schema: "user",
ID: "U2",
Values: &structs.Values{
Data: map[string]interface{}{
"name": "Bob",
"age": nil,
"score": nil,
"birthday": nil,
"active": nil,
"location": nil,
"interests": nil,
"permissionCodes": nil,
},
},
},
{
Schema: "product",
ID: "P1",
Values: &structs.Values{
Data: map[string]interface{}{
"name": "Wireless Earbud",
"price": float32(93.2),
},
},
},
}
edges := []*structs.Edge{
{
Schema: "follows",
From: "U1",
To: "U2",
Values: &structs.Values{
Data: map[string]interface{}{
"createdOn": "2024-5-6",
"weight": float32(3.2),
},
},
},
{
Schema: "follows",
From: "U2",
To: "U1",
Values: &structs.Values{
Data: map[string]interface{}{
"createdOn": 1715169600,
"weight": nil,
},
},
},
{
Schema: "purchased",
From: "U2",
To: "P1",
},
}
result_n, err := driver.InsertNodesBatchAuto(nodes, insertRequestConfig)
if err != nil {
log.Fatalf("Insert failed: %v", err)
}
for schemaName, insertResponse := range result_n {
if len(insertResponse.ErrorItem) > 0 {
fmt.Println("Error items of", schemaName, "nodes:", insertResponse.ErrorItem)
} else {
fmt.Println("All", schemaName, "nodes inserted successfully")
}
}
result_e, err := driver.InsertEdgesBatchAuto(edges, insertRequestConfig)
if err != nil {
log.Fatalf("Insert failed: %v", err)
}
for schemaName, insertResponse := range result_e {
if len(insertResponse.ErrorItem) > 0 {
fmt.Println("Error items of", schemaName, "edges:", insertResponse.ErrorItem)
} else {
fmt.Println("All", schemaName, "edges inserted successfully")
}
}
}