本节介绍用于插入点、边的方法。
| 方法 | 机制 | 使用场景 | 备注 | 
|---|---|---|---|
| insertNodes()insertEdges() | 使用UQL | 插入少量点或边 | |
| insertNodesBatchBySchema()insertEdgesBatchBySchema() | 使用gRPC将数据直接发送到服务器 | 插入大量相同schema的点或边 | 需使用与嬴图属性类型对应的Python数据类型来给属性赋值(参见属性类型映射) | 
| insertNodesBatchAuto()insertEdgesBatchAuto() | 插入大量不同schema的点或边 | 
属性类型映射
嬴图属性类型和Python数据类型的映射关系如下:
| 嬴图属性类型 | Python数据类型 | 示例 | 
|---|---|---|
| INT32,UINT32,INT64,UINT64 | int | 18 | 
| FLOAT,DOUBLE | float | 170.5 | 
| DECIMAL | Decimal | 65.32 | 
| STRING,TEXT | str | "John Doe" | 
| LOCAL_DATETIME | str[1] | "1993-05-06 09:11:02" | 
| ZONED_DATETIME | str[1] | "1993-05-06 09:11:02-0800" | 
| DATE | str[1] | "1993-05-06" | 
| LOCAL_TIME | str[1] | "09:11:02" | 
| ZONED_TIME | str[1] | "09:11:02-0800" | 
| DATETIME | str[2] | "1993-05-06" | 
| TIMESTAMP | str[2],int | "1993-05-06",1715169600 | 
| YEAR_TO_MONTH | str | "P2Y5M","-P1Y5M" | 
| DAY_TO_SECOND | str | "P3DT4H","-P1DT2H3M4.12S" | 
| BOOL | bool | True,False | 
| POINT | str | "point({latitude: 132.1, longitude: -1.5})" | 
| LIST | list | ["tennis", "violin"] | 
| SET | set | [2004, 3025, 1025] | 
[1] 支持的日期格式包括YYYY-MM-DD和YYYYMMDD,支持的时间格式包括HH:MM:SS[.fraction]和HHMMSS[.fraction]。日期和时间用空格或字母T连接。支持的时区格式包括±HH:MM和±HHMM。
[2] 支持的日期字符串格式包括[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以及它们的变体。
示例图结构
本节示例的点、边插入和删除基于如下的Schema和属性定义进行:

参考此示例创建这个图结构。
insertNodes()
向图中某个Schema插入点。
参数
- nodes:List[Node]:待插入的点列表。
- schemaName: str:Schema名称。
- config: InsertRequestConfig(可选):请求配置。
返回值
- Response:请求结果。
# Inserts two 'user' nodes into the graph 'social'
insertRequestConfig = InsertRequestConfig(graph="social")
nodes = [
    Node(id="U1", values={
        "name": "Alice",
        "age": 18,
        "score": 65.32,
        "birthday": "1993-05-04",
        "active": 0,
        "location": "point({latitude: 132.1, longitude: -1.5})",
        "interests": ["tennis", "violin"],
        "permissionCodes": [2004, 3025, 1025]
    }),
    Node(id="U2", values={
        "name": "Bob"
    })
]
response = Conn.insertNodes(nodes, "user", insertRequestConfig)
if response.status.code.name == "SUCCESS":
    print(response.status.code.name)
else:
    print(response.status.message)
SUCCESS
insertEdges()
向图中某个Schema插入边。
参数
- edges:List[Edge]:待插入的边列表;每个- Edge的- fromId和- toId属性必填。
- schemaName: str:Schema名称。
- config: InsertRequestConfig(可选):请求配置。
返回值
- Response:请求结果。
# Inserts two 'follows' edges to the graph 'social'
insertRequestConfig = InsertRequestConfig(graph="social")
edges = [
    Edge(fromId="U1", toId="U2", values={"createdOn": "2024-5-6", "weight": 3.2}),
    Edge(fromId="U2", toId="U1", values={"createdOn": 1715169600})
]
response = Conn.insertEdges(edges, "follows", insertRequestConfig)
if response.status.code.name == "SUCCESS":
    print(response.status.code.name)
else:
    print(response.status.message)
SUCCESS
insertNodesBatchBySchema()
通过gRPC向图中某个Schema插入点。此方法针对批量插入进行了优化。
参数
- schema: Schema:目标Schema;属性- name必填,- properties包含图中对应Schema关联的部分或所有属性。
- nodes:List[Node]:待插入的点列表。
- config: InsertRequestConfig(可选):请求配置。
返回值
- InsertResponse:插入请求结果。
# Inserts two 'user' nodes into the graph 'social'
insertRequestConfig = InsertRequestConfig(graph="social")
schema = Schema(
    name="user",
    properties=[
        Property(name="name", type=UltipaPropertyType.STRING),
        Property(name="age", type=UltipaPropertyType.INT32),
        Property(name="score", type=UltipaPropertyType.DECIMAL, decimalExtra=DecimalExtra(25, 10)),
        Property(name="birthday", type=UltipaPropertyType.DATE),
        Property(name="active", type=UltipaPropertyType.BOOL),
        Property(name="location", type=UltipaPropertyType.POINT),
        Property(name="interests", type=UltipaPropertyType.LIST, subType=[UltipaPropertyType.STRING]),
        Property(name="permissionCodes", type=UltipaPropertyType.SET, subType=[UltipaPropertyType.INT32])
    ]
)
nodes = [
    Node(id="U1", values={
        "name": "Alice",
        "age": 18,
        "score": 65.32,
        "birthday": "1993-05-04",
        "active": 0,
        "location": "point({latitude: 132.1, longitude: -1.5})",
        "interests": ["tennis", "violin"],
        "permissionCodes": [2004, 3025, 1025]
    }),
    Node(id="U2", values={
        "name": "Bob",
    })
]
insertResponse = Conn.insertNodesBatchBySchema(schema, nodes, insertRequestConfig)
if insertResponse.errorItems:
    print("Error items:", insertResponse.errorItems)
else:
    print("All nodes inserted successfully")
All nodes inserted successfully
insertEdgesBatchBySchema()
通过gRPC向图中某个Schema插入边。此方法针对批量插入进行了优化。
参数
- schema: Schema:目标Schema;属性- name必填,- properties包含图中对应Schema关联的部分或所有属性。
- edges:List[Edge]:待插入的边列表;每个- Edge的- fromId和- toId属性必填。
- config: InsertRequestConfig(可选):请求配置。
返回值
- InsertResponse:插入请求结果。
# Inserts two 'follows' edges into the graph 'social'
insertRequestConfig = InsertRequestConfig(graph="social")
schema = Schema(
    name="follows",
    properties=[
        Property(name="createdOn", type=UltipaPropertyType.TIMESTAMP),
        Property(name="weight", type=UltipaPropertyType.FLOAT)
    ]
)
edges = [
    Edge(fromId="U1", toId="U2", values={
        "createdOn": "2024-5-6",
        "weight": 3.2
    }),
    Edge(fromId="U2", toId="U1", values={
        "createdOn": 1715169600
    })
]
insertResponse = Conn.insertEdgesBatchBySchema(schema, edges, insertRequestConfig)
if insertResponse.errorItems:
    print("Error items:", insertResponse.errorItems)
else:
    print("All edges inserted successfully")
All edges inserted successfully
insertNodesBatchAuto()
通过gRPC向图中一个或多个Schema插入点。此方法针对批量插入进行了优化。
参数
- nodes: List[Node]:待插入的点列表;每个- Node的- schema必填,- values包含图中对应Schema关联的部分或所有属性。
- config: InsertRequestConfig(可选):请求配置。
返回值
- Dict[str,InsertResponse]:Schema名称,插入请求结果。
# Inserts two 'user' nodes and a 'product' node into the graph 'social'
insertRequestConfig = InsertRequestConfig(graph="social")
nodes = [
    Node(id="U1", schema="user", values={
        "name": "Alice",
        "age": 18,
        "score": 65.32,
        "birthday": "1993-05-04",
        "active": True,
        "location": "point({latitude: 132.1, longitude: -1.5})",
        "interests": ["tennis", "violin"],
        "permissionCodes": [2004, 3025, 1025]
    }),
    Node(id="U2", schema="user", values={
        "name": "Bob"
    }),
    Node(schema="product", values={
        "name": "Wireless Earbud",
        "price": 93.2
    })
]
result = Conn.insertNodesBatchAuto(nodes, insertRequestConfig)
for schemaName, insertResponse in result.items():
    if insertResponse.errorItems:
        print("Error items of", schemaName, "nodes:", insertResponse.errorItems)
    else:
        print("All", schemaName, "nodes inserted successfully")
All user nodes inserted successfully
All product nodes inserted successfully
insertEdgesBatchAuto()
通过gRPC向图中一个或多个Schema插入边。此方法针对批量插入进行了优化。
参数
- edges: List[Edge]:待插入的边列表;每个- Edge的- schema、- fromId和- toId必填,- values包含图中对应Schema关联的部分或所有属性。
- config: InsertRequestConfig(可选):请求配置。
返回值
- Dict[str,InsertResponse]:Schema名称,插入请求结果。
# Inserts two 'follows' edges and a 'purchased' edge into the graph 'social'
insertRequestConfig = InsertRequestConfig(graph="social")
edges = [
    Edge(schema="follows", fromId="U1", toId="U2", values={"createdOn": "2024-05-06", "weight": 3.2}),
    Edge(schema="follows", fromId="U2", toId="U1", values={"createdOn": 1715169600}),
    Edge(schema="purchased", fromId="U2", toId="689da1080000030022000005", values={})
]
result = Conn.insertEdgesBatchAuto(edges, insertRequestConfig)
for schemaName, insertResponse in result.items():
    if insertResponse.errorItems:
        print("Error items of", schemaName, "edges:", insertResponse.errorItems)
    else:
        print("All", schemaName, "edges inserted successfully")
All follows edges inserted successfully
All purchased edges inserted successfully
完整示例
from ultipa import UltipaConfig, Connection, InsertRequestConfig, Node
ultipaConfig = UltipaConfig()
# URI example: 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)
# Inserts two 'user' nodes, a 'product' node, two 'follows' edges, and a 'purchased' edge into the graph 'social'
insertRequestConfig = InsertRequestConfig(graph="social")
nodes = [
    Node(id="U1", schema="user", values={
        "name": "Alice",
        "age": 18,
        "score": 65.32,
        "birthday": "1993-05-04",
        "active": True,
        "location": "point({latitude: 132.1, longitude: -1.5})",
        "interests": ["tennis", "violin"],
        "permissionCodes": [2004, 3025, 1025]
    }),
    Node(id="U2", schema="user", values={
        "name": "Bob"
    }),
    Node(id="P1", schema="product", values={
        "name": "Wireless Earbud",
        "price": 93.2
    })
]
edges = [
    Edge(schema="follows", fromId="U1", toId="U2", values={"createdOn": "2024-05-06", "weight": 3.2}),
    Edge(schema="follows", fromId="U2", toId="U1", values={"createdOn": 1715169600}),
    Edge(schema="purchased", fromId="U2", toId="P1", values={})
]
result_n = Conn.insertNodesBatchAuto(nodes, insertRequestConfig)
for schemaName, insertResponse in result_n.items():
    if insertResponse.errorItems:
        print("Error items of", schemaName, "nodes:", insertResponse.errorItems)
    else:
        print("All", schemaName, "nodes inserted successfully")
result_e = Conn.insertEdgesBatchAuto(edges, insertRequestConfig)
for schemaName, insertResponse in result_e.items():
    if insertResponse.errorItems:
        print("Error items of", schemaName, "edges:", insertResponse.errorItems)
    else:
        print("All", schemaName, "edges inserted successfully")
