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