本节介绍用于管理图中schema与属性的方法。
Schema
ShowSchema()
获取图中全部schema。
参数
config: *configuration.RequestConfig
(可选):请求配置。
返回值
[]*structs.Schema
:获取的Schema的指针切片。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Retrieves all schemas in the graph 'miniCircle'
requestConfig := &configuration.RequestConfig{
Graph: "miniCircle",
}
schemas, _ := driver.ShowSchema(requestConfig)
for _, schema := range schemas {
fmt.Println(schema.Name, schema.DBType)
}
default DBNODE
account DBNODE
celebrity DBNODE
country DBNODE
movie DBNODE
default DBEDGE
direct DBEDGE
disagree DBEDGE
filmedIn DBEDGE
follow DBEDGE
wishlist DBEDGE
response DBEDGE
review DBEDGE
ShowNodeSchema()
获取图中全部点schema。
参数
config: *configuration.RequestConfig
(可选):请求配置。
返回值
[]*structs.Schema
:获取的Schema的指针切片。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Retrieves all node schemas in the graph 'miniCircle'
requestConfig := &configuration.RequestConfig{
Graph: "miniCircle",
}
schemas, _ := driver.ShowNodeSchema(requestConfig)
for _, schema := range schemas {
fmt.Println(schema.Name, schema.DBType)
}
default DBNODE
account DBNODE
celebrity DBNODE
country DBNODE
movie DBNODE
ShowEdgeSchema()
获取图中全部边schema。
参数
config: *configuration.RequestConfig
(可选):请求配置。
返回值
[]*structs.Schema
:获取的Schema的指针切片。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Retrieves all edge schemas in the graph 'miniCircle'
requestConfig := &configuration.RequestConfig{
Graph: "miniCircle",
}
schemas, _ := driver.ShowEdgeSchema(requestConfig)
for _, schema := range schemas {
fmt.Println(schema.Name, schema.DBType)
}
default DBEDGE
direct DBEDGE
disagree DBEDGE
filmedIn DBEDGE
follow DBEDGE
wishlist DBEDGE
response DBEDGE
review DBEDGE
GetSchema()
获取图中一个指定的schema。
参数
schemaName: string
:Schema名称。dbType: ultipa.DBType
:Schema类型(点或边)。config: *configuration.RequestConfig
(可选):请求配置。
返回值
*structs.Schema
:获取的schema的指针。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Retrieves the node schema named 'account'
requestConfig := &configuration.RequestConfig{
Graph: "miniCircle",
}
schema, _ := driver.GetSchema("account", ultipa.DBType_DBNODE, requestConfig)
fmt.Println(schema.Total)
111
GetNodeSchema()
获取图中一个指定的点schema。
参数
schemaName: string
:Schema名称。config: *configuration.RequestConfig
(可选):请求配置。
返回值
*structs.Schema
:获取的schema的指针。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Retrieves the node schema named 'account'
requestConfig := &configuration.RequestConfig{
Graph: "miniCircle",
}
schema, _ := driver.GetNodeSchema("account", requestConfig)
if schema != nil {
for _, property := range schema.Properties {
println(property.Name)
}
} else {
print("Not found")
}
gender
year
industry
name
GetEdgeSchema()
获取图中一个指定的边schema。
参数
schemaName: string
:Schema名称。config: *configuration.RequestConfig
(可选):请求配置。
返回值
*structs.Schema
:获取的schema的指针。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Retrieves the edge schema named 'disagree'
requestConfig := &configuration.RequestConfig{
Graph: "miniCircle",
}
schema, _ := driver.GetEdgeSchema("agree", requestConfig)
if schema != nil {
for _, property := range schema.Properties {
println(property.Name)
}
} else {
print("Not found")
}
datetime
timestamp
targetPost
CreateSchema()
在图中创建一个schema。
参数
schema: *structs.Schema
:待创建的schema;Name
和DBType
字段必填,Description
和Properties
可选。isCreateProperties: bool
:是否创建属性。config: *configuration.RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
requestConfig := &configuration.RequestConfig{
Graph: "miniCircle",
}
// Creates node schema 'utility' (with properties)
response1, _ := driver.CreateSchema(&structs.Schema{
Name: "utility",
DBType: ultipa.DBType_DBNODE,
Properties: []*structs.Property{
{Name: "name", Type: ultipa.PropertyType_STRING},
{Name: "type", Type: ultipa.PropertyType_UINT32}},
}, true, requestConfig)
fmt.Println(response1.Status.Code)
time.Sleep(3 * time.Second)
// Creates edge schema 'vote' (without properties)
response2, _ := driver.CreateSchema(&structs.Schema{
Name: "vote",
DBType: ultipa.DBType_DBEDGE,
}, false, requestConfig)
fmt.Println(response2.Status.Code)
SUCCESS
SUCCESS
CreateSchemaIfNotExist()
在图中创建一个schema,并返回是否图中已有同名点或边schema存在。
参数
schema: *structs.Schema
:待创建的schema;Name
和DBType
字段必填,Description
和Properties
可选。isCreateProperties: bool
:是否创建属性。config: *configuration.RequestConfig
(可选):请求配置。
返回值
ResponseWithExistCheck
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
requestConfig := &configuration.RequestConfig{
Graph: "miniCircle",
}
schema := &structs.Schema{
Name: "utility",
DBType: ultipa.DBType_DBNODE,
Properties: []*structs.Property{
{Name: "name", Type: ultipa.PropertyType_STRING},
{Name: "type", Type: ultipa.PropertyType_UINT32}},
}
result, _ := driver.CreateSchemaIfNotExist(schema, true, requestConfig)
fmt.Println("Does the schema already exist?", result.Exist)
if result.Response == nil {
fmt.Println("Schema creation status: No response")
} else {
fmt.Println("Schema creation status:", result.Response.Status.Code)
}
time.Sleep(3 * time.Second)
fmt.Println("----- Creates the schema again -----")
result_1, _ := driver.CreateSchemaIfNotExist(schema, true, requestConfig)
fmt.Println("Does the schema already exist?", result_1.Exist)
if result_1.Response == nil {
fmt.Println("Schema creation status: No response")
} else {
fmt.Println("Schema creation status:", result_1.Response.Status.Code)
}
Does the schema already exist? false
Schema creation status: No response
----- Creates the schema again -----
Does the schema already exist? true
Schema creation status: No response
AlterSchema()
修改图中一个schema的名称和描述。
参数
originalSchema: *structs.Schema
:待修改的schema;Name
和DBType
属性必填。newSchema: *structs.Schema
:用于设置新的Name
和/或Description
的指向Schema
结构体的指针。config: *configuration.RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Renames the node schema 'utility' to 'securityUtility' in the graph 'miniCircle'
requestConfig := &configuration.RequestConfig{
Graph: "miniCircle",
}
oldSchema := &structs.Schema{Name: "utility", DBType: ultipa.DBType_DBNODE}
newSchema := &structs.Schema{Name: "securityUtility"}
response, _ := driver.AlterSchema(oldSchema, newSchema, requestConfig)
fmt.Println(response.Status.Code)
SUCCESS
DropSchema()
从图中删除一个指定的schema。
参数
schema: *structs.Schema
:待删除的schema;name
和dbType
字段必填。config: *configuration.RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Drops the edge schema 'vote' from the graph 'miniCircle'
requestConfig := &configuration.RequestConfig{
Graph: "miniCircle",
}
response, _ := driver.DropSchema(&structs.Schema{Name: "vote", DBType: ultipa.DBType_DBEDGE}, requestConfig)
fmt.Println(response.Status.Code)
SUCCESS
属性
ShowProperty()
获取图中全部属性。
参数
dbType: ultipa.DBType
:属性类型(点或边,或全局)。schemaName: string
:Schema名称;使用空字符串(""
)指定全部schemas。config: *configuration.RequestConfig
(可选):请求配置。
返回值
[]*structs.Property
:获取的点属性的指针切片。[]*structs.Property
:获取的边属性的指针切片。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Retrieves all properties in the graph 'citation'
requestConfig := &configuration.RequestConfig{
Graph: "citation",
}
nodeProperties, edgeProperties, _ := driver.ShowProperty(ultipa.DBType_DBGLOBAL, "", requestConfig)
fmt.Println("Node Properties:")
for _, nodeProperty := range nodeProperties {
fmt.Println(nodeProperty.Name, "is associated with schema", nodeProperty.Schema)
}
fmt.Println("Edge Properties:")
for _, edgeProperty := range edgeProperties {
fmt.Println(edgeProperty.Name, "is associated with schema", edgeProperty.Schema)
}
Node Properties:
_id is associated with schema default
_id is associated with schema Paper
title is associated with schema Paper
score is associated with schema Paper
author is associated with schema Paper
Edge Properties:
weight is associated with schema Cites
ShowNodeProperty()
获取图中全部点属性。
参数
schemaName: string
:Schema名称;使用空字符串(""
)指定全部schemas。config: *configuration.RequestConfig
(可选):请求配置。
返回值
[]*structs.Property
:获取的属性的指针切片。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Retrieves properties associated with the node schema 'Paper' in the graph 'citation'
requestConfig := &configuration.RequestConfig{
Graph: "citation",
}
properties, _ := driver.ShowNodeProperty("Paper", requestConfig)
for _, property := range properties {
fmt.Println(property.Name, "-", property.Type)
}
_id - STRING
title - STRING
score - INT32
author - STRING
ShowEdgeProperty()
获取图中全部边属性。
参数
schemaName: string
:Schema名称;使用空字符串(""
)指定全部schemas。config: *configuration.RequestConfig
(可选):请求配置。
返回值
[]*structs.Property
:获取的属性的指针切片。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Retrieves properties associated with the edge schema 'Cites' in the graph 'citation'
requestConfig := &configuration.RequestConfig{
Graph: "citation",
}
properties, _ := driver.ShowEdgeProperty("Cites", requestConfig)
for _, property := range properties {
fmt.Println(property.Name, "-", property.Type)
}
weight - INT32
GetProperty()
获取图中一个指定的属性。
参数
dbType: ultipa.DBType
:属性类型(点或边)。schemaName: string
:Schema名称。propertyName: string
:属性名称。config: *configuration.RequestConfig
(可选):请求配置。
返回值
*structs.Property
:获取的属性的指针。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Retrieves node property 'title' associated with the node schema 'Paper' in the graph 'citation'
requestConfig := &configuration.RequestConfig{
Graph: "citation",
}
property, _ := driver.GetProperty(ultipa.DBType_DBNODE, "Paper", "title", requestConfig)
fmt.Println(property)
&{title Paper STRING [] false false false <nil>}
GetNodeProperty()
获取图中一个指定的点属性。
参数
schemaName: string
:Schema名称。propertyName: string
:属性名称。config: *configuration.RequestConfig
(可选):请求配置。
返回值
*structs.Property
:获取的属性的指针。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Retrieves node property 'title' associated with the node schema 'Paper' in the graph 'citation'
requestConfig := &configuration.RequestConfig{
Graph: "citation",
}
property, _ := driver.GetNodeProperty("Paper", "title", requestConfig)
fmt.Println(property)
&{title Paper STRING [] false false false <nil>}
GetEdgeProperty()
获取图中一个指定的边属性。
参数
schemaName: string
:Schema名称。propertyName: string
:属性名称。config: *configuration.RequestConfig
(可选):请求配置。
返回值
*structs.Property
:获取的属性的指针。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Retrieves edge property 'weight' associated with the edge schema 'Cites' in the graph 'citation'
requestConfig := &configuration.RequestConfig{
Graph: "citation",
}
property, _ := driver.GetEdgeProperty("Cites", "weight", requestConfig)
fmt.Println(property)
&{weight Cites INT32 [] false false false <nil>}
CreateProperty()
在图中创建一个属性。
参数
dbType: ultipa.DBType
:属性类型(点或边)。property: *structs.Property
:待创建的属性;Name
、Type
(以及SubType
,如果Type
为SET
或LIST
)和Schema
(用*
指定全部schema)字段必填,Encrypt
和Description
可选。config: *configuration.RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Creates a property 'year' for all node schemas, creates a property 'tags' for the node schema 'Paper'
requestConfig := &configuration.RequestConfig{
Graph: "citation",
}
response1, _ := driver.CreateProperty(ultipa.DBType_DBNODE, &structs.Property{
Name: "year",
Type: ultipa.PropertyType_INT32,
Encrypt: "AES128",
Schema: "*",
}, requestConfig)
fmt.Println(response1.Status.Code)
response2, _ := driver.CreateProperty(ultipa.DBType_DBNODE, &structs.Property{
Name: "tags",
Type: ultipa.PropertyType_SET,
SubTypes: []ultipa.PropertyType{ultipa.PropertyType_STRING},
Schema: "Paper",
}, requestConfig)
fmt.Println(response2.Status.Code)
SUCCESS
SUCCESS
CreatePropertyIfNotExist()
在图中创建一个属性,并返回是否图中指定点或边schema下已有同名属性存在。
参数
dbType: ultipa.DBType
:属性类型(点或边)。property: *structs.Property
:待创建的属性;Name
、Type
(以及SubType
,如果Type
为SET
或LIST
)和Schema
(用*
指定全部schema)字段必填,Encrypt
和Description
可选。config: *configuration.RequestConfig
(可选):请求配置。
返回值
ResponseWithExistCheck
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
requestConfig := &configuration.RequestConfig{
Graph: "citation",
}
property := &structs.Property{
Name: "tags",
Type: ultipa.PropertyType_SET,
SubTypes: []ultipa.PropertyType{ultipa.PropertyType_STRING},
Schema: "Paper",
}
result, _ := driver.CreatePropertyIfNotExist(ultipa.DBType_DBNODE, property, requestConfig)
fmt.Println("Does the property already exist?", result.Exist)
if result.Response == nil {
fmt.Println("Property creation status: No response")
} else {
fmt.Println("Property creation status:", result.Response.Status.Code)
}
time.Sleep(3 * time.Second)
fmt.Println("----- Creates the property again -----")
result_1, _ := driver.CreatePropertyIfNotExist(ultipa.DBType_DBNODE, property, requestConfig)
fmt.Println("Does the property already exist?", result_1.Exist)
if result_1.Response == nil {
fmt.Println("Property creation status: No response")
} else {
fmt.Println("Property creation status:", result_1.Response.Status.Code)
}
Does the property already exist? false
Property creation status: SUCCESS
----- Creates the property again -----
Does the property already exist? true
Property creation status: No response
AlterProperty()
修改图中指定属性的名称和描述。
参数
dbType: ultipa.DBType
:属性类型(点或边)。originProp: *structs.Property
:待修改的属性;Name
和Schema
字段必填(用*
可指定全部schema)。newProp: *structs.Property
:用于设置新的Name
和/或Description
的指向Property
结构体的指针。config: *configuration.RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Renames the property 'tags' of the node schema 'Paper' to 'keywords' in the graph 'citation'
requestConfig := &configuration.RequestConfig{
Graph: "citation",
}
response, _ := driver.AlterProperty(ultipa.DBType_DBNODE, &structs.Property{
Name: "tags",
Schema: "Paper",
}, &structs.Property{
Name: "keywords",
}, requestConfig)
fmt.Print(response.Status.Code)
SUCCESS
DropProperty()
从图中删除指定的属性。
参数
dbType: ultipa.DBType
:属性类型(点或边)。property: Property
:待删除的属性;Name
和Schema
字段必填(用*
可指定全部schema)。config: *configuration.RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。error
:包含操作过程中遇到的任何问题的错误对象;如果操作成功,则返回nil
。
// Drops the property 'tags' of the node schema in the graph 'citation'
requestConfig := &configuration.RequestConfig{
Graph: "citation",
}
response, _ := driver.DropProperty(ultipa.DBType_DBNODE, &structs.Property{
Name: "tags",
Schema: "Paper",
}, requestConfig)
fmt.Print(response.Status.Code)
SUCCESS
完整示例
package main
import (
"fmt"
"log"
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"
)
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 schemas and properties in the graph 'social'
requestConfig := &configuration.RequestConfig{
Graph: "social",
}
user := &structs.Schema{
Name: "user",
DBType: ultipa.DBType_DBNODE,
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}},
},
}
product := &structs.Schema{
Name: "product",
DBType: ultipa.DBType_DBNODE,
Properties: []*structs.Property{
{Name: "name", Type: ultipa.PropertyType_STRING},
{Name: "price", Type: ultipa.PropertyType_FLOAT},
},
}
follows := &structs.Schema{
Name: "follows",
DBType: ultipa.DBType_DBEDGE,
Properties: []*structs.Property{
{Name: "createdOn", Type: ultipa.PropertyType_TIMESTAMP},
{Name: "weight", Type: ultipa.PropertyType_FLOAT},
},
}
purchased := &structs.Schema{
Name: "purchased",
DBType: ultipa.DBType_DBEDGE,
}
schemas := []*structs.Schema{user, product, follows, purchased}
for _, schema := range schemas {
response, _ := driver.CreateSchema(schema, true, requestConfig)
fmt.Println(response.Status.Code)
}
}