本节为您介绍如何使用Connection
对象的方法向图集插入或从图集删除点和边。
每个示例主要展示如何使用所列方法。点击完整示例.
图数据模型示例
以下各示例为您展示如何在具有以下schema和属性的图集里插入或删除点和边:
数据属性类型映射关系
插入点或边时,需指定不同属性类型。嬴图属性类型和Python/驱动程序数据类型的映射关系如下:
嬴图支持的属性类型 | Python/驱动程序支持的数据类型 |
---|---|
int32 | int |
uint32 | int |
int64 | int |
uint64 | int |
float | float |
double | float |
decimal | Decimal ,支持多种数值类型(int ,float 等)和str |
string | str |
text | str |
datetime | str [1] |
timestamp | str [1], int |
point | str |
blob | bytes |
list | list |
set | set |
[1] 支持批量插入的日期字符串格式包括[YY]YY-MM-DD HH:MM:SS
, [YY]YY-MM-DD HH:MM:SSZ
, [YY]YY-MM-DDTHH:MM:SSZ
, [YY]YY-MM-DDTHH:MM:SSXX
, [YY]YY-MM-DDTHH:MM:SSXXX
, [YY]YY-MM-DD HH:MM:SS.SSS
及其变体。
插入
insertNodes()
向当前图集的某个schema插入新的点。
参数:
List[Node]
:待插入的Node
列表。str
:schema名称。InsertRequestConfig
(可选):配置请求。
返回值:
ResponseInsertNode
:请求的结果。若将InsertRequestConfig.silent
设定为false,ResponseInsertNode
对象会包含一个nodes
别名,所有插入的点均在其中。
# 向图集lcc名为user的schema插入两个点,打印错误代码和插入点的信息
insertRequestConfig = InsertRequestConfig(
insertType=InsertType.NORMAL,
graphName="lcc",
silent=False
)
nodes = [
Node(uuid=1, id="U001", values={
"name": "Alice",
"age": 18,
"score": 65.32,
"birthday": "1993-5-4",
"location": "point({latitude: 132.1, longitude: -1.5})",
"profile": "castToRaw(abc)",
"interests": ["tennis", "violin"],
"permissionCodes": [2004, 3025, 1025]
}),
Node(uuid=2, id="U002", values={
"name": "Bob"
})
]
response = Conn.insertNodes(nodes, "user", insertRequestConfig)
print(response.status.code)
# 如果InsertRequestConfig.silent设定为true,Response不返回别名
insertedNodes = response.alias("nodes").asNodes()
for insertedNode in insertedNodes:
print(insertedNode.toJSON())
0
{"id": "U001", "schema": "user", "uuid": 1, "values": {"age": 18, "birthday": "1993-05-04 00:00:00", "interests": ["tennis", "violin"], "location": "POINT(132.100000 -1.500000)", "name": "Alice", "permissionCodes": [3025, 2004, 1025], "profile": [99, 97, 115, 116, 84, 111, 82, 97, 119, 40, 97, 98, 99, 41], "score": "65.3200000000"}}
{"id": "U002", "schema": "user", "uuid": 2, "values": {"age": null, "birthday": null, "interests": null, "location": null, "name": "Bob", "permissionCodes": null, "profile": null, "score": null}}
insertEdges()
向当前图集的某个schema插入新的边。
参数:
List[Edge]
:待插入的Edge
列表。str
:schema名称。InsertRequestConfig
(可选):配置请求。
返回值:
ResponseInsertEdge
:请求的结果。若将InsertRequestConfig.silent
设定为false,ResponseInsertEdge
对象会包含一个edges
别名,所有插入的边均在其中。
# 向图集lcc名为follows的schema插入两条边,打印错误代码和插入边的信息
insertRequestConfig = InsertRequestConfig(
insertType=InsertType.NORMAL,
graphName="lcc",
silent=False
)
edges = [
Edge(uuid=1, from_id="U001", to_id="U002", values={"createdOn": "2024-5-6"}),
Edge(uuid=2, from_id="U002", to_id="U001", values={"createdOn": 1715169600})
]
response = Conn.insertEdges(edges, "follows", insertRequestConfig)
print(response.status.code)
# There is no alias in ResponseInsertEdge if InsertRequestConfig.silent is true
insertedEdges = response.alias("edges").asEdges()
for insertedEdge in insertedEdges:
print(insertedEdge.toJSON())
0
{"from_id": "U001", "from_uuid": 1, "schema": "follows", "to_id": "U002", "to_uuid": 2, "uuid": 1, "values": {"createdOn": 1714924800}}
{"from_id": "U002", "from_uuid": 2, "schema": "follows", "to_id": "U001", "to_uuid": 1, "uuid": 2, "values": {"createdOn": 1715169600}}
insertNodesBatchBySchema()
通过gRPC向当前图集的某个schema插入新点。插入的点,其属性必须和schema结构中的声明保持一致。
参数:
Schema
:目标schema。List[Node]
:待插入的Node
列表。InsertRequestConfig
(可选):配置请求。
返回值:
InsertResponse
:请求的结果。当InsertRequestConfig.silent
设置为false时,InsertResponse.data.uuids
中包含了插入节点的UUID。
# 向图集lcc名为user的schema插入两个点,打印错误代码和插入结果
insertRequestConfig = InsertRequestConfig(
insertType=InsertType.NORMAL,
graphName="lcc",
silent=False
)
schema = Schema(
name="user",
dbType=DBType.DBNODE,
properties=[
Property(name="name", type=PropertyType.PROPERTY_STRING),
Property(name="age", type=PropertyType.PROPERTY_INT32),
Property(name="score", type=PropertyType.PROPERTY_DECIMAL),
Property(name="birthday", type=PropertyType.PROPERTY_DATETIME),
Property(name="location", type=PropertyType.PROPERTY_POINT),
Property(name="profile", type=PropertyType.PROPERTY_BLOB),
Property(name="interests", type=PropertyType.PROPERTY_LIST, subTypes=[PropertyType.PROPERTY_STRING]),
Property(name="permissionCodes", type=PropertyType.PROPERTY_SET, subTypes=[PropertyType.PROPERTY_INT32])
]
)
nodes = [
Node(uuid=1, id="U001", values={
"name": "Alice",
"age": 18,
"score": 65.32,
"birthday": "1993-5-4",
"location": "point({latitude: 132.1, longitude: -1.5})",
"profile": b"abc",
"interests": ["tennis", "violin"],
"permissionCodes": {2004, 3025, 1025}
}),
Node(uuid=2, id="U002", values={
"name": "Bob",
"age": None,
"score": None,
"birthday": None,
"location": None,
"profile": None,
"interests": None,
"permissionCodes": None
})
]
response = Conn.insertNodesBatchBySchema(schema, nodes, insertRequestConfig)
print(response.status.code)
# InsertResponse.data.uuids is empty if InsertRequestConfig.silent is true
print(response.data.uuids)
0
[1, 2]
insertEdgesBatchBySchema()
通过gRPC向当前图集的某个schema插入新边。插入的边,其属性必须和schema结构中的声明保持一致。
参数:
Schema
:目标schema。List[Edge]
:待插入的Edge
列表。InsertRequestConfig
(可选):配置请求。
返回值:
InsertResponse
:请求的结果。当InsertRequestConfig.silent
设置为false 时,InsertResponse.data.uuids
中包含了插入边的UUID。
# 向图集lcc名为follows的schema插入两条边,打印错误代码和插入结果
insertRequestConfig = InsertRequestConfig(
insertType=InsertType.NORMAL,
graphName="lcc",
silent=False
)
schema = Schema(
name="follows",
dbType=DBType.DBEDGE,
properties=[
Property(name="createdOn", type=PropertyType.PROPERTY_TIMESTAMP)
]
)
edges = [
Edge(uuid=1, from_id="U001", to_id="U002", values={"createdOn": "2024-05-06"}),
Edge(uuid=2, from_id="U002", to_id="U001", values={"createdOn": None}),
]
response = Conn.insertEdgesBatchBySchema(schema, edges, insertRequestConfig)
print(response.status.code)
# 如果InsertRequestConfig.silent设定为true,InsertResponse.data.uuids为空
print(response.data.uuids)
0
[1,2]
insertNodesBatchAuto()
通过gRPC向当前图集的一个或多个schema插入新点。插入的点,其属性必须和schema结构中的声明保持一致。
参数:
List[Node]
:待插入的Node
列表。InsertRequestConfig
(可选):配置请求。
返回值:
ResponseBatchAutoInsert
:请求的结果。当InsertRequestConfig.silent
设置为 false时,ResponseBatchAutoInsert.data.uuids
中包含了插入点的UUID。
# 向图集lcc名为user和product的两个schema各插入一个点,打印错误代码和插入结果
insertRequestConfig = InsertRequestConfig(
insertType=InsertType.NORMAL,
graphName="lcc",
silent=False
)
nodes = [
Node(schema="user", uuid=1, id="U001", values={
"name": "Alice",
"age": 18,
"score": 65.32,
"birthday": "1993-5-4",
"location": "point({latitude: 132.1, longitude: -1.5})",
"profile": b"abc",
"interests": ["tennis", "violin"],
"permissionCodes": {2004, 3025, 1025}
}),
Node(schema="user", uuid=2, id="U002", values={
"name": "Bob",
"age": None,
"score": None,
"birthday": None,
"location": None,
"profile": None,
"interests": None,
"permissionCodes": None
}),
Node(schema="product", uuid=3, id="P001", values={
"name": "Wireless Earbud",
"price": 93.2
})
]
response = Conn.insertNodesBatchAuto(nodes, insertRequestConfig)
print(response.status.code)
# 如果InsertRequestConfig.silent设定为true,Response.data.uuids为空
print(response.data.uuids)
0
[1, 2, 3]
insertEdgesBatchAuto()
通过gRPC向当前图集的一个或多个schema插入新边。插入的边,其属性必须和schema结构中的声明保持一致。
参数:
List[Edge]
:待插入的Edge
列表。InsertRequestConfig
(可选):配置请求。
返回值:
ResponseBatchAutoInsert
:请求的结果。当InsertRequestConfig.silent
设置为 false 时,ResponseBatchAutoInsert.data.uuids
中包含了插入边的UUID。
# 向图集lcc名为follows的schema插入两条边,向名为purchased的schema插入一条边,打印错误代码和插入结果
insertRequestConfig = InsertRequestConfig(
insertType=InsertType.NORMAL,
graphName="lcc",
silent=False
)
edges = [
Edge(schema="follows", uuid=1, from_id="U001", to_id="U002", values={"createdOn": "2024-05-06"}),
Edge(schema="follows", uuid=2, from_id="U002", to_id="U001", values={"createdOn": 1715169600}),
Edge(schema="purchased", uuid=3, from_id="U002", to_id="P001", values={"qty": 1})
]
response = Conn.insertEdgesBatchAuto(edges, insertRequestConfig)
print(response.status.code)
# 如果InsertRequestConfig.silent设定为true,Response.data.uuids为空
print(response.data.uuids)
0
[1, 2, 3]
删除
deleteNodes()
从图集删除符合指定条件的点。需特别注意,删除点的同时也会删除与点相连的边。
参数:
str
:待删除的点的条件。InsertRequestConfig
(可选):配置请求。
返回值:
ResponseDeleteNode
:请求的结果。若将InsertRequestConfig.silent
设定为false,ResponseDeleteNode
对象会包含一个nodes
别名,所有删除的点均在其中。
# 删除图集lcc里@user下名为Alice的点,打印错误代码和已删除的点的信息
# 与删除的点相连的边也会全部删除
insertRequestConfig = InsertRequestConfig(
insertType=InsertType.NORMAL,
graphName="lcc",
silent=False
)
response = Conn.deleteNodes("{@user.name == 'Alice'}", insertRequestConfig)
print(response.status.code)
# 如果InsertRequestConfig.silent设定为true,那么ResponseDeleteNode不返回别名
deletedNodes = response.alias("nodes").asNodes()
for deletedNode in deletedNodes:
print(deletedNode.toJSON())
0
{"id": "U001", "schema": "user", "uuid": 1, "values": {"name": "Alice"}}
deleteEdges()
从图集删除符合指定条件的边。
参数:
str
:待删除的边的条件。InsertRequestConfig
(可选):配置请求。
返回值:
ResponseDeleteEdge
:请求的结果。若将InsertRequestConfig.silent
设定为false,ResponseDeleteEdge
对象会包含一个edges
别名,所有删除的边均在其中。
# 从图集lcc删除@purchased中的全部边,打印错误代码和已删除的边的信息
insertRequestConfig = InsertRequestConfig(
insertType=InsertType.NORMAL,
graphName="lcc",
silent=False
)
response = Conn.deleteEdges("{@purchased}", insertRequestConfig)
print(response.status.code)
# 如果InsertRequestConfig.silent设定为true,ResponseDeleteEdge不返回别名
deletedEdges = response.alias("edges").asEdges()
for deletedEdge in deletedEdges:
print(deletedEdge.toJSON())
0
{"from_id": "U002", "from_uuid": 2, "schema": "purchased", "to_id": "P001", "to_uuid": 3, "uuid": 3, "values": {}}
完整示例
from ultipa.configuration.InsertRequestConfig import InsertRequestConfig
from ultipa import Connection, UltipaConfig, Node
from ultipa.structs import InsertType
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)
# 设置连接
insertRequestConfig = InsertRequestConfig(
insertType=InsertType.NORMAL,
graphName="lcc",
silent=False
)
# 向图集lcc名为user的schema插入两个点,向名为product的schema插入一个点,打印错误代码和插入报告
nodes = [
Node(schema="user", uuid=1, id="U001", values={
"name": "Alice",
"age": 18,
"score": 65.32,
"birthday": "1993-5-4",
"location": "point({latitude: 132.1, longitude: -1.5})",
"profile": b"abc",
"interests": ["tennis", "violin"],
"permissionCodes": {2004, 3025, 1025}
}),
Node(schema="user", uuid=2, id="U002", values={
"name": "Bob",
"age": None,
"score": None,
"birthday": None,
"location": None,
"profile": None,
"interests": None,
"permissionCodes": None
}),
Node(schema="product", uuid=3, id="P001", values={
"name": "Wireless Earbud",
"price": 93.2
})
]
response = Conn.insertNodesBatchAuto(nodes, insertRequestConfig)
print(response.status.code)
# Response.data.uuids is empty if InsertRequestConfig.silent is true
print(response.data.uuids)