本节介绍用于插入点、边的方法。
方法 | 机制 | 使用场景 | 备注 |
---|---|---|---|
insertNodes() insertEdges() |
使用UQL | 插入少量点或边 | |
insertNodesBatchBySchema() insertEdgesBatchBySchema() |
使用gRPC将数据直接发送到服务器 | 插入大量相同Schema的点或边 | 需使用与嬴图属性类型对应的Node.js数据类型来给属性赋值(参见属性类型映射) |
insertNodesBatchAuto() insertEdgesBatchAuto() |
插入大量不同Schema的点或边 |
属性类型映射
嬴图属性类型和Node.js数据类型的映射关系如下:
嬴图属性类型 |
Node.js数据类型 |
示例 |
---|---|---|
INT32 , UINT32 |
number |
18 |
INT64 , UINT64 |
string |
1715169600 |
FLOAT , DOUBLE , DECIMAL |
string |
65.32 |
STRING , TEXT |
string |
"John Doe" |
DATETIME |
string [1] |
"1993-05-06" |
TIMESTAMP |
string [1], number |
"1993-05-06" , 1715169600 |
BOOL |
boolean |
true , false ,0 ,1 |
POINT |
string |
"POINT(132.1 -1.5)" |
LIST |
Array<> |
["tennis", "violin"] |
SET |
Set<> |
[2004, 3025, 1025] |
[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
以及它们的变体。
示例图结构
本节示例的点、边插入和删除基于如下的Schema和属性定义进行:

参考此示例创建这个图结构。
insertNodes()
向图中某个Schema插入点。
参数
schemaName: string
:Schema名称。nodes: Node[]
:待插入的点列表。config: InsertRequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Inserts two 'user' nodes into the graph 'social'
const insertRequestConfig: InsertRequestConfig = {
graph: "social",
insertType: InsertType.NORMAL,
silent: true,
};
const node1 = {
id: "U1",
values: {
name: "Alice",
age: 18,
score: "65.32",
birthday: "1993-5-4",
"active": 0,
location: `POINT(132.1 -1.5)`,
interests: ["tennis", "violin"],
permissionCodes: [2004, 3025, 1025],
},
};
const node2 = { id: "U2", values: { name: "Bob" } };
const nodeList = [node1,node2]
const response = await conn.insertNodes(
"user",
nodeList,
insertRequestConfig
);
console.log(response.status?.message);
SUCCESS
insertEdges()
向图中某个Schema插入边。
参数
schemaName: string
:Schema名称。edges: Edge[]
:待插入的边列表;每个Edge
的from
和to
属性必填。config: InsertRequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Inserts two 'follows' edges to the graph 'social'
const insertRequestConfig: InsertRequestConfig = {
graph: "social",
insertType: InsertType.NORMAL,
silent: true,
};
const edge1 = {
from: "U1",
to: "U2",
values: {
createdOn: "2024-5-6",
weight: "3.2",
},
};
const edge2 = {
from: "U2",
to: "U1",
values: { createdOn: "2024-5-8" },
};
const edgeList = [edge1, edge2];
const response = await conn.insertEdges(
"follows",
edgeList,
insertRequestConfig
);
console.log(response.status?.message);
SUCCESS
insertNodesBatchBySchema()
通过gRPC向图中某个Schema插入点。此方法针对批量插入进行了优化。
参数
schema: Schema
:目标Schema;属性name
必填,properties
包含图中对应Schema关联的部分或所有属性。nodes: Node[]
:待插入的点列表;每个Node
的values
属性必须与Schema.properties
结构相同。config: InsertRequestConfig
(可选):请求配置。
返回值
InsertResponse
:插入请求结果。
// Inserts two 'user' nodes into the graph 'social'
const insertRequestConfig: InsertRequestConfig = {
graph: "social",
insertType: InsertType.NORMAL,
silent: true,
};
const schema: Schema = {
name: "user",
dbType: DBType.DBNODE,
properties: [
{ name: "name", type: UltipaPropertyType.STRING, schema: "user" },
//{name:"age", type: UltipaPropertyType.INT32,schema:"user"},
{
name: "score",
type: UltipaPropertyType.DECIMAL,
decimalExtra: { precision: 25, scale: 10 },
schema: "user",
},
{ name: "birthday", type: UltipaPropertyType.DATETIME, schema: "user" },
{ name: "active", type: UltipaPropertyType.BOOL, schema: "user" },
{ name: "location", type: UltipaPropertyType.POINT, schema: "user" },
{
name: "interests",
type: UltipaPropertyType.LIST,
subType: [UltipaPropertyType.STRING],
schema: "user",
},
{
name: "permissionCodes",
type: UltipaPropertyType.SET,
subType: [UltipaPropertyType.INT32],
schema: "user",
},
],
};
const node1 = {
id: "U1",
values: {
name: "Alice",
//age: 18,
score: "65.32",
birthday: "1993-5-4",
active: 0,
location: `POINT(132.1 -1.5)`,
interests: ["tennis", "violin"],
permissionCodes: [2004, 3025, 1025],
},
};
const node2 = { id: "U2", values: { name: "Bob" } };
const nodeList = [node1, node2];
const insertResponse = await conn.insertNodesBatchBySchema(
schema,
nodeList,
insertRequestConfig
);
if (insertResponse.errorItems?.size == 0) {
console.log("All nodes inserted successfully");
} else {
console.log("Error items:", insertResponse.errorItems);
}
All nodes inserted successfully
insertEdgesBatchBySchema()
通过gRPC向图中某个Schema插入边。此方法针对批量插入进行了优化。
参数
schema: Schema
:目标Schema;属性name
必填,properties
包含图中对应Schema关联的部分或所有属性。edges: Edge[]
:待插入的边列表;每个Edge
的from
和to
属性必填,values
必须与Schema.properties
结构相同。config: InsertRequestConfig
(可选):请求配置。
返回值
config: InsertResponse
:插入请求结果。
// Inserts two 'follows' edges into the graph 'social'
const insertRequestConfig: InsertRequestConfig = {
graph: "social",
insertType: InsertType.NORMAL,
silent: true,
};
const schema: Schema = {
name: "follows",
dbType: DBType.DBEDGE,
properties: [
{ name: "createdOn", type: UltipaPropertyType.TIMESTAMP, schema: "follows" },
{ name: "weight", type: UltipaPropertyType.FLOAT, schema: "follows" },
],
};
const edge1 = {
from: "U1",
to: "U2",
values: {
// createdOn: 1714953600,
weight: "3.2",
},
};
const edge2 = { from: "U2", to: "U1", values: { createdOn: 1715169600 } };
const edgeList = [edge1, edge2];
const insertResponse = await conn.insertEdgesBatchBySchema(
schema,
edgeList,
insertRequestConfig
);
if (insertResponse.errorItems?.size == 0) {
console.log("All edges inserted successfully");
} else {
console.log("Error items:", insertResponse.errorItems);
}
All edges inserted successfully
insertNodesBatchAuto()
通过gRPC向图中一个或多个Schema插入点。此方法针对批量插入进行了优化。
参数
nodes: Node[]
:待插入的点列表;每个Node
的schema
必填,所有点的values
结构相同并且包含图中对应Schema关联的部分或所有属性。config: InsertRequestConfig
(可选):请求配置。
返回值
Map<string, InsertResponse>
:Schema名称,插入请求结果。
// Inserts two 'user' nodes and a 'product' node into the graph 'social'
const insertRequestConfig: InsertRequestConfig = {
graph: "social",
insertType: InsertType.NORMAL,
silent: true,
};
const node1 = {
id: "U1",
schema: "user",
values: {
name: "Alice",
// age: 18,
score: "65.32",
birthday: "1993-5-4",
active: false,
location: `POINT(132.1 -1.5)`,
interests: ["tennis", "violin"],
permissionCodes: [2004, 3025, 1025],
},
};
const node2 = { id: "U2", schema: "user", values: { name: "Bob" } };
const node3 = {
schema: "product",
values: { name: "Wireless Earbud", price: "93.2" },
};
const nodeList = [node1, node2,node3];
const result = await conn.insertNodesBatchAuto(nodeList, insertRequestConfig);
for (let [schemaName, insertResponse] of result.entries()) {
if (!isEmpty(insertResponse.errorItems)) {
console.log(
"Error items of " +
schemaName +
" nodes: " +
JSON.stringify(insertResponse.errorItems)
);
} else {
console.log("All " + schemaName + " nodes inserted successfully");
}
}
All user nodes inserted successfully
All product nodes inserted successfully
insertEdgesBatchAuto()
通过gRPC向图中一个或多个Schema插入边。此方法针对批量插入进行了优化。
参数
edges: Edge[]
:待插入的边列表;每个Edge
的schema
、from
和to
必填,所有边的values
结构相同并且包含图中对应Schema关联的部分或所有属性。config: InsertRequestConfig
(可选):请求配置。
返回值
Map<string, InsertResponse>
:Schema名称,插入请求结果。
// Inserts two 'follows' edges and a 'purchased' edge into the graph 'social'
const insertRequestConfig: InsertRequestConfig = {
graph: "social",
insertType: InsertType.NORMAL,
silent: true,
};
const edge1 = {
from: "U1",
to: "U2",
schema: "follows",
values: {
createdOn: "2024-05-06",
weight: "3.2"
},
};
const edge2 = {
from: "U2",
to: "U1",
schema: "follows",
values: {
createdOn: 1714953600,
} };
const edge3 = {
from:"U2",
to: "68382e950000010021000000",
schema: "purchased"
};
const edgeList = [edge1, edge2,edge3];
const result = await conn.insertEdgesBatchAuto(edgeList, insertRequestConfig);
for (let [schemaName, insertResponse] of result.entries()) {
if (!isEmpty(insertResponse.errorItems)) {
console.log(
"Error items of " +
schemaName +
" edges: " +
JSON.stringify(insertResponse.errorItems)
);
} else {
console.log("All " + schemaName + " edges inserted successfully");
}
}
All follows edges inserted successfully
All purchased edges inserted successfully
完整示例
import { UltipaDriver } from "@ultipa-graph/ultipa-driver";
import { ULTIPA } from "@ultipa-graph/ultipa-driver/dist/types";
import { InsertRequestConfig } from "@ultipa-graph/ultipa-driver/dist/types/types";
import { InsertType } from "@ultipa-graph/ultipa-driver/src/proto/ultipa_pb";
import { isEmpty } from "lodash";
let sdkUsage = async () => {
// URI example: ultipaConfig.hosts: ["mqj4zouys.us-east-1.cloud.ultipa.com:60010"]
const ultipaConfig: ULTIPA.UltipaConfig = {
hosts: ["192.168.1.85:60061", "192.168.1.87:60061", "192.168.1.88:60061"],
username: "<username>",
password: "<password>"
};
const conn = new UltipaDriver(ultipaConfig);
const isSuccess = await conn.test();
console.log(`Connection succeeds: ${isSuccess}`);
// Inserts two 'user' nodes, a 'product' node, two 'follows' edges, and a 'purchased' edge into the graph 'social'
const insertRequestConfig: InsertRequestConfig = {
graph: "social",
insertType: InsertType.NORMAL,
silent: true,
};
const node1 = {
id: "U1",
schema: "user",
values: {
name: "Alice",
age: 18,
score: "65.32",
birthday: "1993-5-4",
active: 0,
location: `POINT(132.1 -1.5)`,
interests: ["tennis", "violin"],
permissionCodes: [2004, 3025, 1025],
},
};
const node2 = { id: "U2", schema: "user", values: { name: "Bob" } };
const node3 = {
id: "P1",
schema: "product",
values: { name: "Wireless Earbud", price: "93.2" },
};
const nodeList = [node1, node2, node3];
const edge1 = {
from: "U1",
to: "U2",
schema: "follows",
values: {
createdOn: "2024-05-06",
weight: "3.2",
},
};
const edge2 = {
from: "U2",
to: "U1",
schema: "follows",
values: {
createdOn: 1714953600,
},
};
const edge3 = {
from: "U2",
to: "P1",
schema: "purchased",
};
const edgeList = [edge1, edge2, edge3];
const result_n = await conn.insertNodesBatchAuto(nodeList, insertRequestConfig);
for (let [schemaName, insertResponse] of result_n.entries()) {
if (!isEmpty(insertResponse.errorItems)) {
console.log(
"Error items of " +
schemaName +
" nodes: " +
JSON.stringify(insertResponse.errorItems)
);
} else {
console.log("All " + schemaName + " nodes inserted successfully");
}
}
const result_e = await conn.insertEdgesBatchAuto(edgeList, insertRequestConfig);
for (let [schemaName, insertResponse] of result_e.entries()) {
if (!isEmpty(insertResponse.errorItems)) {
console.log(
"Error items of " +
schemaName +
" edges: " +
JSON.stringify(insertResponse.errorItems)
);
} else {
console.log("All " + schemaName + " edges inserted successfully");
}
}
};
sdkUsage().catch(console.error);