概述
在赢图系统中,一个schema代表一类点或边。如果将真实世界还原到图系统上,那么点schema通常用来描述不同的实体类别(如账户、公司、银行卡),而边schema则用来描述实体间的各种关系(如关注、持有、转账)。
创建点或边数据,必须先定义其schema。每个点或边数据对应且仅对应一个schema。一个图集通常包含多个点schema和边schema。
创建图集时,系统会默认创建一个点schema(default)和边schema(default),供用户自由使用,但二者不可修改和删除。
UQL中使用@
符号指代一个schema。使用表达式@<schema>
过滤指定的schema,如@Account
。
查看Schema
// 查看当前图集中的所有schema
show().schema()
// 查看当前图集中的所有点schema
show().node_schema()
// 查看当前图集中的所有边schema
show().edge_schema()
// 查看当前图集中名为movie的点schema
show().node_schema(@movie)
// 查看当前图集中名为filmedIn的边schema
show().edge_schema(@filmedIn)
结果示例:
_nodeSchema
name |
totalNodes |
description |
properties |
---|---|---|---|
default | 0 | default schema | [] |
movie | 92 | [{name: "name", type: "string", description: "", lte: "true", extra: "{}"}, {name: "genre", type: "string", description: "", lte: "false", extra: "{}"}, {name: "rating", type: "double", description: "", lte: "false", extra: "{}"}] |
|
country | 78 | [{name: "name", type: "string", description: "", lte: "false", extra: "{}"}] |
_edgeSchema
name |
totalEdges |
description |
properties |
---|---|---|---|
default | 0 | default schema | [] |
filmedIn | 192 | [{name: "time", type: "timestamp", description: "", lte: "false", extra: "{}"}] |
properties
列仅包含自定义属性信息;系统属性不在结果中显示。
创建Schema
// 在当前图集中创建名为movie的点schema,并添加描述信息
create().node_schema("movie", "The movies added by the admin")
// 在当前图集中创建名为filmedIn的边schema
create().edge_schema("filmedIn")
// 在当前图集中同时创建多个点或边schema
create()
.node_schema("movie", "The movies added by the admin")
.node_schema("country")
.edge_schema("filmedIn")
命名规则
Schema命名规则如下:
- 名称长度为2~64个字符。
- 不能以波浪号
~
开头。 - 不能包含反引号
`
。 - 不能与系统保留字重名。
一个图集中的点schema不能重名,边schema也不能重名,但点schema可以和边schema重名。
Schema名称含有字母(A-Z,a-z)、数字(0-9)、下划线(_
)以外的字符时,在UQL语句中使用时需要用一对反引号(`
)包裹。
find().nodes({@`movie*`}) as n
return n
使用前缀TRY
在当前图集中,同时创建3个点schema,但其中一个点schema(default)与已有点schema重名。
create().node_schema("new_1").node_schema("default").node_schema("new_2")
new_1这个点schema在重名schema之前创建,因此创建成功。而new_2在重名schema之后创建,因此创建失败,并返回报错信息Schema already exist!
。
TRY create().node_schema("new_1").node_schema("default").node_schema("new_2")
此次点schema创建过程同上,但由于使用了前缀TRY
,报错信息被屏蔽,返回信息为SUCCEED
。
修改Schema
// 修改当前图集中movie这个点schema的名称和描述
alter().node_schema(@movie)
.set({name: "Adm_movie", description: "Movies added by the admin"})
// 修改当前图集中filmedIn这个边schema的描述
alter().edge_schema(@filmedIn).set({description: "The country where a movie is filmed"})
删除Schema
删除schema时,会同时删除与其关联的所有点边数据。默认的点、边schema(default)无法删除。
// 从当前图集删除名为movie的点schema
drop().node_schema(@movie)
// 从当前图集删除名为filmedIn的边schema
drop().edge_schema(@filmedIn)
// 从当前图集同时删除多个点、边schema
drop()
.node_schema(@movie)
.node_schema(@country)
.edge_schema(@filmedIn)