概述
插入操作向当前图集单一或批量地插入新的点、边数据。insert()
、insert().overwrite()
和upsert()
这三个命令均可以触发插入操作。本节介绍由insert()
命令触发的插入操作。
插入数据时,需首先指定schema,然后指定全部或部分属性的值,也可以不指定任何属性的值:
- 指定的UID属性值不能与图集中已有的重复;没有指定UID属性值时,系统会自动生成。
- 未携带的自定义属性会被赋值为null。
- 若指定的属性值与其数据类型不符,系统会使用该数据类型的默认值给该属性赋值。各数据类型的默认值如下:
- ""(string,text)
- 0(int32,int64,uint32,uint64,float,double,decimal)
- "1970-01-01 08:00:00 +08:00"(timestamp)
- "0000-00-00 00:00:00"(datetime)
- [](list)
- POINT(0.00 0.00)(point)
常见的失败原因:
- 指定的UID属性值在图集中已存在。
- 插入边时,未指定起点和终点。
- 插入边时,指定的起点或终点在图集中不存在。
不建议在流式返回的算法语句之后组装插入语句。
语法
- 子句别名:支持,数据类型为NODE或EDGE
- 参数别名:所有参数均不支持定义别名
// 向当前图集的某个schema中插入点数据
insert().into(@<schema>)
.nodes([ //只插入一个点时可省略中括号
{<property1>:<value1>, <property2>:<value2>, ...},
{<property1>:<value1>, <property2>:<value2>, ...},
...
])
// 向当前图集的某个schema中插入边数据,必须携带_from和_to(或_from_uuid和_to_uuid)
insert().into(@<schema>)
.edges([ //只插入一个边时可省略中括号
{<property1>:<value1>, <property2>:<value2>, ...},
{<property1>:<value1>, <property2>:<value2>, ...},
...
])
示例
指定point和blob类型属性的值
设置piont和blob类型的属性值时,分别需要使用坐标对象构建函数point()
和二进制转换函数castToRaw()
。
本例插入一个@default点,并携带属性coordinate(point类型)和blob_value(blob类型)的值:
insert().into(@default).nodes({
coordinate: point({latitude: 132.1, longitude: -1.5}),
blob_value: castToRaw("32")
})
示例图集
在一个空图集中,依次运行以下各行语句创建示例图集:
create().node_schema("account").edge_schema("follow")
create().node_property(@account, "name").edge_property(@follow, "time", datetime)
insert().into(@account).nodes([{_id:"U001", _uuid:1, name:"Jason"}, {_id:"U002", _uuid:2, name:"Tim"}, {_id:"U003", _uuid:3, name:"Grace"}, {_id:"U004", _uuid:4, name:"Ted"}])
insert().into(@follow).edges([{_uuid:1, _from_uuid:4, _to_uuid:1, time:"2021-09-10"}, {_uuid:2, _from_uuid:3, _to_uuid:2, time:"2020-03-12"}, {_uuid:3, _from_uuid:4, _to_uuid:2, time:"2023-07-30"}])
属性赋值
本例插入两个@account点。第一个不携带任何属性,该点的_id
和_uuid
由系统生成,其余属性值为null;第二个点未携带的_uuid
由系统生成:
insert().into(@account).nodes([{}, {_id: "U005", name: "Alice"}]) as nodes
return nodes{*}
| _id | _uuid | name |
|------------------------|-------|--------|
| ULTIPA8000000000000001 | 5 | null |
| U005 | 6 | Alice |
UID重复
本例指定的点UID(_id
)U001在图集中已存在,因此插入失败:
insert().into(@account).nodes({_id: "U001"}) as nodes
return nodes{*}
Duplicated ids!
边的起/终点不存在
本例指定的终点(_to
)U010在图集中不存在,因此插入失败:
insert().into(@follow).edges({_from: "U001", _to: "U010"}) as edges
return edges{*}
The _to: U010 does not exist!