本文介绍如何在 Ultipa Manager 中进行图建模、插入及修改图数据,并给出 UI 操作及 UQL 语句的示例。
图模型
图模型(Graph Model)是指图集(GraphSet)的 schemas 和属性:
对于 Ultipa Cloud 的用户,可以通过服务器列表中的 'load sample' 功能让 Ultipa Cloud 向某个服务器中自动创建图集、建模并加载图数据。
对于 自主部署 Ultipa Manager 的用户,请跟着下面的介绍手动建图。
如何执行 UQL
create().graph("retail_test")
上面这句 UQL 的字面意思是 “创建一个名为 retail_test 的图”。
以这句 “建图” 的 UQL 为例,在 Ultipa Manager 中一个 UQL 语句可以在顶部的 CLI 窗口中书写并运行(图2),也可以通过 UI 操作进行组装并完成(图3):
注意:创建图集后须在图集列表中点击 Select 切换至该图集,才能保证后续操作均为针对该图集而进行(图3)。 切换图集是由 SDK 进行的,而并非 UQL 功能。
建模
图4 展示了一个简单的建模过程,其本质是通过交互拼接了如下一些用于创建 schema 和属性的 UQL:
create().node_schema("customer") // 创建 点schema 'customer'
create().edge_schema("transfer") // 创建 边schema 'transfer'
create().node_property(@customer, "balance", float) // 为 点schema 'customer' 创建属性 'balance',数据类型为 'float'
create().edge_property(@transfer, "tran_type", int32) // 为 边schema 'transfer' 创建属性 'tran_type',数据类型为 'int32'
...
以上这些 UQL 的特征:
- 以命令
create()
为开头 - 参数
node_schema()
、edge_schema()
等通过英文句点 '.' 连在命令后 - 先创建 schema 再创建其属性
- 用户只创建 自定义属性,不创建 系统属性(解释见下文)
关于建模的详细语法请见 UQL 文档中的 图集、Schema、属性。
元数据
元数据是对 点和边 的统称,也就是之前所讲的图数据。
插入点
点有两个系统属性,_id
和 _uuid
,它们均为点的唯一标识符,只是数据类型不同。
点击阅读更多关于元数据的 唯一标识符。
下面这些 UQL 语句均向 schema customer 中插入了点:
insert().into(@customer).nodes({}) // 插入一个点,属性使用默认值
insert().into(@customer).nodes([{},{}]) // 插入两个点,属性使用默认值
insert().into(@customer).nodes({cust_name: "Jason"}) // 插入一个点,属性 'cust_name' 为 'Jason'
insert().into(@customer).nodes({_id: "CU001"}) // 插入一个点,属性 '_id' 为 'CU001'
insert().into(@customer).nodes({cust_name: "Jason", _id: "CU001"}) // 插入一个点,同时指定 'cust_name' 和 '_id'
...
这些 UQL 的特征:
- 以命令
insert()
为开头 - 在参数
into()
中声明 schema,在nodes()
中将每个点的属性值组织为对象格式 - 未提供的属性将由系统指定为默认值,具体值因数据类型而异
- 未提供的
_id
和_uuid
将自动生成
插入边
边只有 _uuid
一种唯一标识符,但另有 4 个系统属性,即边起点和终点的唯一标识符 _from
&_to
、_from_uuid
&_to_uuid
,其中至少一对必须在插入边时提供。
以下 UQL 均向 schema transfer 中插入了边(由 'CU001' 或 'CU002' 指向 'MC001'):
// 插入一条边,属性使用默认值
insert().into(@transfer).edges({_from: "CU001", _to: "MC001"})
// 插入两条边,属性使用默认值
insert().into(@transfer).edges([{_from: "CU001", _to: "MC001"},{_from: "CU002", _to: "MC001"}])
// 插入一条边,属性 'tran_amount' 为 '1000'
insert().into(@transfer).edges({_from: "CU001", _to: "MC001", tran_amount: "1000"})
// 插入一条边,属性 '_uuid' 为 '1'
insert().into(@transfer).edges({_from: "CU001", _to: "MC001", _uuid: 1})
// 插入一条边,同时指定 'tran_amount' 和 '_uuid'
insert().into(@transfer).edges({_from: "CU001", _to: "MC001", tran_amount: "Jason", _uuid: "CU001"})
...
与插点类似,这些插边的 UQL 具有以下特征:
- 以命令
insert()
为开头 - 在参数
into()
中声明 schema,在edges()
中将每个边的属性值组织为对象格式 - 未提供的属性将由系统指定为默认值,具体值因数据类型而异
- 未提供的
_uuid
将自动生成 - 必须提供
_from
&_to
(或_from_uuid
&_to_uuid
),并且这些点必须已经存在
点击阅读更多关于插入元数据的介绍 插入、插入覆盖、插入更新。
关于从 CSV 文件批量导入数据至图集,请阅读 数据导入。
更新元数据
更多关于更新点、边的 UQL 示例:
update().nodes().set({type: "IV"}) // 将所有点的属性 'type' 改为 'IV'
update().nodes({_id == "CU001"}).set({type: "IV"}) // 将 '_id' 为 'CU001' 的点的属性 'type' 改为 'IV'
update().nodes({merchant_name contains "Beijing"}).set({type: "IV"}) // 将 'merchant_name' 中包含 'Beijing' 的点的属性 'type' 改为 'IV'
update().edges().set({result: "success"}) // 将所有边的属性 'result' 改为 'success'
update().edges({_uuid == 1}).set({result: "success"}) // 将 '_uuid' 为 '1' 的边的属性 'result' 改为 'success'
update().edges({@transfer._from == "CU001"}).set({result: "success"}) // 将 '_from' 为 'CU001' 的边的属性 'result' 改为 'success'
...
这些 UQL 语句的特征为:
- 以命令
update()
为开头 - 在参数
nodes()
或edges()
中描述需要更新的点、边(描述方法详见 图数据) - 在参数
set()
中将点、边的新属性值组织为对象格式 _id
和_uuid
的值不支持更新
update()
命令用来更新元数据的属性值,而非对属性进行重命名。对属性、schema、图集进行重命名属于修改图模型的操作,详见 UQL 文档中的 图集、Schema、属性。