本节介绍用于管理数据库中图的方法。
ShowGraph()
获取数据库中全部的图。
参数
config: *configuration.RequestConfig
(可选):请求配置。
返回值
[]*structs.GraphSet
:获取的图的指针切片。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Retrieves all graphs and prints the names of those with over 2000 edges
graphs, _ := driver.ShowGraph(nil)
for _, graph := range graphs {
if graph.TotalEdges > 2000 {
println(graph.Name)
}
}
Display_Ad_Click
ERP_DATA2
wikiKG
GetGraph()
获取数据库中一个指定的图。
参数
graphName: string
:图名称。config: *configuration.RequestConfig
(可选):请求配置。
返回值
*structs.GraphSet
:获取的图的指针。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Retrieves the graph named 'miniCircle'
graph, _ := driver.GetGraph("miniCircle", nil)
fmt.Println(graph)
&{1438 miniCircle 307 1961 NORMAL [1 2 3] 256 CityHash64}
HasGraph()
检查数据库中是否存在一个指定的图。
参数
graphName: string
:图名称。config: *configuration.RequestConfig
(可选):请求配置。
返回值
bool
:检查结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Checks the existence of a graph named 'miniCircle'
response, _ := driver.HasGraph("miniCircle", nil)
println(response)
True
CreateGraph()
在数据库中创建一个图。
参数
graphSet: *structs.GraphSet
:待创建的图;Name
字段必填,Shards
、PartitionBy
和Description
可选。config: *configuration.RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Creates a graph
response, _ := driver.CreateGraph(&structs.GraphSet{Name: "testGoSDK", Shards: []string{"1"}, PartitionBy: "Crc32", Description: "testGoSDK desc"}, nil)
fmt.Println(response.Status.Code)
SUCCESS
CreateGraphIfNotExist()
在数据库中创建一个图,并返回是否数据库中已有同名图存在。
参数
graphSet: *structs.GraphSet
:待创建的图;Name
字段必填,Shards
、PartitionBy
和Description
可选。config: *configuration.RequestConfig
(可选):请求配置。
返回值
ResponseWithExistCheck
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
graph := &structs.GraphSet{Name: "testGoSDK", Shards: []string{"1"}, PartitionBy: "Crc32", Description: "testGoSDK desc"}
result, _ := driver.CreateGraphIfNotExist(graph, nil)
fmt.Println("Does the graph already exist?", result.Exist)
if result.Response == nil {
fmt.Println("Graph creation status: No response")
} else {
fmt.Println("Graph creation status:", result.Response.Status.Code)
}
time.Sleep(3 * time.Second)
fmt.Println("----- Creates the graph again -----")
result_1, _ := driver.CreateGraphIfNotExist(graph, nil)
fmt.Println("Does the graph already exist?", result_1.Exist)
if result_1.Response == nil {
fmt.Println("Graph creation status: No response")
} else {
fmt.Println("Graph creation status:", result_1.Response.Status.Code)
}
Does the graph already exist? false
Graph creation status: SUCCESS
----- Creates the graph again -----
Does the graph already exist? true
Graph creation status: No response
AlterGraph()
修改数据库中一个图的名称和描述。
参数
graphName: string
:图名称。alterGraphset: *structs.GraphSet
: 用于设置新的Name
和/或Description
的指向GraphSet
结构体的指针。config: *configuration.RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Alters the name and description of the graph 'testPythonSDK'
newGraphInfo := &structs.GraphSet{Name: "newGraph", Description: "a new graph"}
response, _ := driver.AlterGraph("testGoSDK", newGraphInfo, nil)
fmt.Println(response.Status.Code)
SUCCESS
DropGraph()
从数据库中删除一个指定的图。
参数
graphName: string
:图名称。config: *configuration.RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Drops the graph 'testGoSDK'
response, _ := driver.DropGraph("testGoSDK", nil)
fmt.Println(response.Status.Code)
SUCCESS
Truncate()
清空(删除)图中的指定点或边,或清空整个图。请注意,清空点的同时会删除与点相连的所有边。清空操作仅删除点边,图的schema和属性定义仍保留。
参数
params: *structs.TruncateParams
: 清空操作的参数;GraphName
字段必填,SchemaName
和DBType
可选。config: *configuration.RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Truncates User nodes in 'myGraph'
NodeType := ultipa.DBType_DBNODE
response1, _ := driver.Truncate(&structs.TruncateParams{GraphName: "myGraph", SchemaName: "User", DBType: &NodeType}, nil)
fmt.Println(response1.Status.Code)
// Truncates all edges in the 'myGraph'
EdgeType := ultipa.DBType_DBEDGE
response2, _ := driver.Truncate(&structs.TruncateParams{GraphName: "myGraph", SchemaName: "*", DBType: &EdgeType}, nil)
fmt.Println(response2.Status.Code)
// Truncates 'myGraph'
response3, _ := driver.Truncate(&structs.TruncateParams{GraphName: "myGraph"}, nil)
fmt.Println(response3.Status.Code)
SUCCESS
SUCCESS
SUCCESS
Compact()
清除图中的无效及冗余数据。有效数据不会受到影响。
参数
graphName: string
:图名称。config: *configuration.RequestConfig
(可选):请求配置。
返回值
JobResponse
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Compacts the graph 'miniCircle'
requestConfig := &configuration.RequestConfig{
Graph: "miniCircle",
}
response, _ := driver.Compact("miniCircle", nil)
jobID := response.JobId
time.Sleep(3 * time.Second)
jobs, _ := driver.ShowJob(jobID, requestConfig)
for _, job := range jobs {
fmt.Println(job.Id, "-", job.Status)
}
138 - FINISHED
138_1 - FINISHED
138_2 - FINISHED
138_3 - FINISHED
完整示例
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"
)
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)
}
// Creates a graph
response, _ := driver.CreateGraph(&structs.GraphSet{Name: "testGoSDK", Shards: []string{"1"}, PartitionBy: "Crc32", Description: "testGoSDK desc"}, nil)
fmt.Println(response.Status.Code)
}