本节为您介绍如何使用Connection
对象的方法管理图集中点和边的schema与属性。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
Schema
showSchema()
从当前图集获取全部点、边的schema信息。
参数:
RequestConfig
(可选):配置请求。
返回值:
ResponseListSchema
:当前图集全部schema列表。
# 获取图集UltipaTeam的所有schema并打印schema的类型和名称
requestConfig = RequestConfig(graphName="UltipaTeam")
schemas = Conn.showSchema(requestConfig)
for schema in schemas:
print(f"{schema.name}, type: {schema.DBType}")
default, type: 0
member, type: 0
organization, type: 0
default, type: 1
reportsTo, type: 1
relatesTo, type: 1
getSchema()
从当前图集获取一个点schema或一个边schema。
参数:
str
:schema名称。DBtype
:schema类型(点schema 或边schema)。RequestConfig
(可选):配置请求。
返回值:
Schema
:目标schema。
# 获取图集UltipaTeam中名为organization的点schema和名为connectsTo的边schema,并打印其信息
requestConfig = RequestConfig(graphName="UltipaTeam")
schema1 = Conn.getSchema("organization", DBType.DBNODE, requestConfig)
if schema1:
print(schema1.toJSON())
else:
print("Not found")
schema2 = Conn.getSchema("connectsTo", DBType.DBEDGE, requestConfig)
if schema2:
print(schema2.toJSON())
else:
print("Not found")
{"DBType": 0, "description": "", "name": "organization", "properties": [{"description": "", "lte": true, "name": "name", "schema": null, "subTypes": null, "type": "string"}, {"description": "", "lte": false, "name": "logo", "schema": null, "subTypes": null, "type": "string"}], "total": "19", "type": "node"}
Not found
showNodeSchema()
获取当前图集的全部点schema。
参数:
RequestConfig
(可选): 配置请求。
返回值:
List[Schema]
: 当前图集全部点schema列表。
# 获取图集UltipaTeam的全部点schema并打印其名称
requestConfig = RequestConfig(graphName="UltipaTeam")
schemas = Conn.showNodeSchema(requestConfig)
for schema in schemas:
print(schema.name)
default
member
organization
showEdgeSchema()
获取当前图集的全部边schema。
参数:
RequestConfig
(可选): 配置请求。
返回值:
List[Schema]
: 当前图集的全部边schema列表。
# 获取图集UltipaTeam的全部边schema并打印其名称
requestConfig = RequestConfig(graphName="UltipaTeam")
schemas = Conn.showEdgeSchema(requestConfig)
for schema in schemas:
print(schema.name)
default
reportsTo
relatesTo
getNodeSchema()
获取当前图集的一个点schema。
参数:
str
:schema名称。RequestConfig
(可选): 配置请求。
返回值:
Schema
:目标点schema。
# 获取图集UltipaTeam中名为member的点schema并打印其属性信息
requestConfig = RequestConfig(graphName="UltipaTeam")
schema = Conn.getNodeSchema("member", requestConfig)
for property in schema.properties:
print(property.name)
title
profile
age
getEdgeSchema()
获取当前图集的一个边schema。
参数:
String
:schema名称。RequestConfig
(可选):配置请求。
返回值:
Schema
:目标边schema。
# 获取图集UltipaTeam中名为relatesTo的边schema并打印其属性信息
requestConfig = RequestConfig(graphName="UltipaTeam")
schema = Conn.getEdgeSchema("relatesTo", requestConfig)
for property in schema.properties:
print(property.name)
type
createSchema()
在当前图集新建一个schema。
参数:
Schema
:待创建的schema;必须设置name
字段和dbType
字段,description
字段和properties
字段可选。bool
(可选):是否创建属性,默认为false
。RequestConfig
(可选):配置请求。
返回值:
UltipaResponse
:请求的结果。
requestConfig = RequestConfig(graphName="UltipaTeam")
# 在图集UltipaTeam中新建名为utility的点schema(包含属性),并打印其全部信息。
utility = Schema(
name="utility",
description="Office utilities",
dbType=DBType.DBNODE,
properties=[
Property(name="name", type=PropertyTypeStr.PROPERTY_STRING),
Property(name="purchaseDate", type=PropertyTypeStr.PROPERTY_DATETIME)
]
)
response1 = Conn.createSchema(utility, True, requestConfig)
print(response1.status.code)
time.sleep(3)
schema1 = Conn.getNodeSchema("utility", requestConfig)
print(schema1.toJSON())
# 在图集UltipaTeam中新建名为managedBy(不包含属性)的边schema并打印其全部信息
managedBy = Schema(
name="managedBy",
dbType=DBType.DBEDGE
)
response2 = Conn.createSchema(managedBy, False, requestConfig)
print(response2.status.code)
time.sleep(3)
schema2 = Conn.getEdgeSchema("managedBy", requestConfig)
print(schema2.toJSON())
0
{"DBType": 0, "description": "Office utilities", "name": "utility", "properties": [{"description": "", "lte": false, "name": "name", "schema": null, "subTypes": null, "type": "string"}, {"description": "", "lte": false, "name": "purchaseDate", "schema": null, "subTypes": null, "type": "datetime"}], "total": "0", "type": "node"}
0
{"DBType": 1, "description": "", "name": "managedBy", "properties": [], "total": "0", "type": "edge"}
createSchemaIfNotExist()
在当前图集新建一个schema,若同名schema已存在,忽略报错。
参数:
Schema
:待创建的schema;必须设置name
字段和dbType
字段,description
字段和properties
字段可选。bool
(可选):是否创建属性,默认为false
。RequestConfig
(可选):配置请求。
返回值:
bool
:该Schema是否已经存在。UltipaResponse
:请求的结果。
requestConfig = RequestConfig(graphName="UltipaTeam")
utility = Schema(
name="utility",
description="Office utilities",
dbType=DBType.DBNODE
)
# 在图集UltipaTeam中新建一个schema并打印创建是否发生
response1 = Conn.createSchemaIfNotExist(schema=utility, requestConfig=requestConfig)
if response1[0] is False:
print("Code =", response1[1].status.code)
else:
print("No response")
# 再次创建同个schema并打印创建是否发生
time.sleep(3)
response2 = Conn.createSchemaIfNotExist(utility, requestConfig)
if response2[0] is False:
print("Code =", response1[1].status.code)
else:
print("No response")
Code = 0
No response
alterSchema()
根据schema名称修改当前图集中某个schema的名称和描述。
参数:
Schema
:图集中待修改的schema;必须设置name
字段和dbType
字段。Schema
:更新已有schema的配置;必须设置dbType
字段, 对于name
字段和description
字段,必须至少设置其中一个字段。RequestConfig
(可选): 配置请求。
返回值:
UltipaResponse
:请求的结果。
# 将图集UltipaTeam中的一个点schema由utility改为securityUtility并删除其描述
requestConfig = RequestConfig(graphName="UltipaTeam")
schema = Conn.getNodeSchema("utility", requestConfig)
newSchema = Schema(name="securityUtility", description="yyyy", dbType=DBType.DBNODE)
response = Conn.alterSchema(schema, newSchema, requestConfig)
print(response.status.code)
0
dropSchema()
根据schema名称从当前图集删除某个schema。
参数:
Schema
:T图集中待删除的schema;必须设置name
字段和dbType
字段。RequestConfig
(可选):配置请求。
返回值:
UltipaResponse
:请求的结果。
# 在图集UltipaTeam中删除名为utility的点schema
requestConfig = RequestConfig(graphName="UltipaTeam")
schema = Conn.getNodeSchema("utility", requestConfig)
response = Conn.dropSchema(schema, requestConfig)
print(response.status.code)
0
Property
showProperty()
获取当前图集的自定义点属性信息或边属性信息。
参数:
dbType
:属性类型(点属性或边属性)。str
(可选):schema名称。RequestConfig
(可选):配置请求。
返回值:
List[Property]
:当前图集全部目标属性列表。
# 获取图集UltipaTeam的所有自定义点属性信息并打印其名称、类型和关联的schema
requestConfig = RequestConfig(graphName="UltipaTeam")
properties = Conn.showProperty(dbType=DBType.DBNODE,requestConfig=requestConfig)
for property in properties:
print(f"{property.name} ({property.type}) is associated with schema named {property.schema}")
name (string) is associated with schema named member
title (string) is associated with schema named member
profile (string) is associated with schema named member
name (string) is associated with schema named organization
logo (string) is associated with schema named organization
showNodeProperty()
获取当前图集的自定义点属性信息。
参数:
str
(可选):schema名称。RequestConfig
(可选):配置请求。
返回值:
List[Property]
:当前图集全部目标属性列表。
# 获取图集UltipaTeam中名为member的点schema其关联的所有自定义点属性信息并打印属性数量
requestConfig = RequestConfig(graphName="UltipaTeam")
properties = Conn.showNodeProperty("member", requestConfig)
print(len(properties))
3
showEdgeProperty()
获取当前图集的自定义边属性信息。
参数:
str
(可选):schema名称。RequestConfig
(可选):配置请求。
返回值:
List[Property]
:当前图集全部目标属性列表。
# 获取图集UltipaTeam中名为relatesTo的边schema其关联的所有自定义边属性并打印属性名称
requestConfig = RequestConfig(graphName="UltipaTeam")
properties = Conn.showEdgeProperty("relatesTo", requestConfig)
for property in properties:
print(property.name)
type
getProperty()
获取当前图集的一个自定义点属性或边属性。
参数:
DBType
:属性类型(点属性或边属性)。str
:属性名称。str
:schema名称。RequestConfig
(可选):配置请求。
返回值:
Property
:目标属性。
# 获取图集UltipaTeam的点属性@member.title并打印其全部信息
requestConfig = RequestConfig(graphName="UltipaTeam")
property = Conn.getProperty(DBType.DBNODE, "title", "member", requestConfig)
print(property.toJSON())
{"description": "", "encrypt": "", "encrypted": false, "extra": "{}", "ignored": false, "lte": false, "name": "title", "propertyType": "", "read": true, "schema": "member", "subTypes": null, "type": "string", "write": true}
getNodeProperty()
获取当前图集的一个自定义点属性。
参数:
str
:属性名称。str
:schema名称。RequestConfig
(可选):配置请求。
返回值:
Property
:目标属性。
# 获取图集UltipaTeam的点属性@member.title并打印其全部信息
requestConfig = RequestConfig(graphName="UltipaTeam")
property = Conn.getNodeProperty("title", "member", requestConfig)
print(property.toJSON())
{"description": "", "encrypt": "", "encrypted": false, "extra": "{}", "ignored": false, "lte": false, "name": "title", "propertyType": "", "read": true, "schema": "member", "subTypes": null, "type": "string", "write": true}
getEdgeProperty()
获取当前图集的一个自定义边属性。
参数:
str
:属性名称。str
:schema名称。RequestConfig
(可选):配置请求。
返回值:
Property
:目标属性。
# 获取图集UltipaTeam的边属性@relatesTo.type并打印其全部信息
requestConfig = RequestConfig(graphName="UltipaTeam")
property = Conn.getEdgeProperty("type", "relatesTo", requestConfig)
print(property.toJSON())
{"description": "", "encrypt": "", "encrypted": false, "extra": "{}", "ignored": false, "lte": false, "name": "type", "propertyType": "", "read": true, "schema": "relatesTo", "subTypes": null, "type": "string", "write": true}
createProperty()
在当前图集为点schema或边schema创建一个属性。
参数:
DBType
:属性类型(边属性或点属性)。str
:schema名称,可用*
指定全部schema。Property
:待创建的属性;必须设置name
字段和type
字段,description
字段可选。RequestConfig
(可选):配置请求。
返回值:
UltipaResponse
:请求的结果。
# Creates two properties for node schema 'member' in graphset 'UltipaTeam' and prints error codes
requestConfig = RequestConfig(graphName="UltipaTeam")
property1 = Property(
name="startDate",
type="datetime"
)
property2 = Property(
name="age",
type="int32"
)
response1 = Conn.createProperty(DBType.DBNODE, "member", property1, requestConfig)
print(response1.status.code)
response2 = Conn.createProperty(DBType.DBNODE, "member", property2, requestConfig)
print(response2.status.code)
0
0
createPropertyIfNotExist()
在当前图集为某个点schema或边schema新建一个属性,若同名属性已存在,忽略报错。
参数:
DBType
:属性类型(点属性或边属性)。str
:schema名称,可用*
指定全部schema。Property
:待创建的属性;必须设置name
字段和type
字段,description
字段可选。RequestConfig
(可选):配置请求。
返回值:
boolean
:该属性是否已经存在。UltipaResponse
:请求的结果。
requestConfig = RequestConfig(graphName="UltipaTeam")
prop = Property(
name="startDate",
type="datetime"
)
# 在图集UltipaTeam中为名为member的点schema新建一个属性并打印创建是否发生
response1 = Conn.createPropertyIfNotExist(DBType.DBNODE, "member", prop, requestConfig)
if response1[0] is False:
print("Code =", response1[1].status.code)
else:
print("No response")
# 在图集UltipaTeam中再次创建同个属性并打印创建是否发生
time.sleep(3)
response2 = Conn.createPropertyIfNotExist(DBType.DBNODE, "member", prop, requestConfig)
if response2[0] is False:
print("Code =", response2[1].status.code)
else:
print("No response")
Code = 0
No response
alterProperty()
根据属性名称修改当前图集中某个自定义属性的名称和描述。
参数:
DBType
:属性类型(点属性或边属性)。Property
:图集中待修改的属性;必须设置name
字段和schema
字段(可用*
指定全部schema)。Property
:更新已有属性的配置;对于name
字段和description
字段,必须至少设置其中一个字段。RequestConfig
(可选):配置请求。
返回值:
UltipaResponse
:请求的结果。
requestConfig = RequestConfig(graphName="UltipaTeam")
# 将图集UltipaTeam中全部点schema下的name属性改为Name
oldProperty = Property(
name="name",
schema="*"
)
newProperty = Property(
name="Name"
)
response = Conn.alterProperty(DBType.DBNODE, oldProperty, newProperty, requestConfig)
print(response.status.code)
0
dropProperty()
根据属性名称和关联schema从当前图集删除某自定义属性。
参数:
DBType
:属性类型(点属性或边属性)。str
:schema名称,可用*
指定全部schema。str
:属性名称。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
requestConfig = RequestConfig(graphName="UltipaTeam")
# 删除图集UltipaTeam中与全部点schema关联的startDate属性并打印错误代码
response1 = Conn.dropProperty(DBType.DBNODE, "*", "startDate", requestConfig)
print(response1.status.code)
# 删除图集UltipaTeam的点属性@member.name并打印错误代码
response2 = Conn.dropProperty(DBType.DBNODE, "member", "name", requestConfig)
print(response2.status.code)
0
0
完整示例
from ultipa.configuration.RequestConfig import RequestConfig
from ultipa import Connection, UltipaConfig, Schema, DBType
from ultipa.structs import Property
ultipaConfig = UltipaConfig()
# URI示例:ultipaConfig.hosts = ["mqj4zouys.us-east-1.cloud.ultipa.com:60010"]
ultipaConfig.hosts = ["192.168.1.85:60061", "192.168.1.87:60061", "192.168.1.88:60061"]
ultipaConfig.username = "<username>"
ultipaConfig.password = "<password>"
Conn = Connection.NewConnection(defaultConfig=ultipaConfig)
# 配置请求
requestConfig = RequestConfig(graphName="UltipaTeam")
# 在图集UltipaTeam中新建名为utility的点schema(同时创建属性)并打印错误代码
utility = Schema(
name="utility",
description="Office utilities",
dbType=DBType.DBNODE,
properties=[
Property(name="name", type="string"),
Property(name="purchaseDate", type="datetime")
]
)
response = Conn.createSchema(utility, True, requestConfig)
print(response.status.code)