本节为您介绍如何使用Connection
对象的方法向图集插入或从图集删除点和边。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
图数据模型示例
以下各示例为您展示如何在具有以下schema和属性的图集里插入或删除点和边:
数据属性类型映射关系
插入点或边时,需指定不同属性类型。嬴图属性类型和Java/驱动程序数据类型的映射关系如下:
嬴图支持的属性类型 | Java/驱动程序支持的数据类型 |
---|---|
int32 | int |
uint32 | long |
int64 | long |
uint64 | long |
float | float |
double | double |
decimal | BigDecimal ,支持多种数值类型(Integer ,Float ,Double ,Long 等)和String |
string | String |
text | String |
datetime | String [1],另外支持批量插入java.util.Date 和LocalDateTime |
timestamp | String [1],另外支持批量插入java.util.Date |
point | Point (驱动类型) |
blob | byte[] ,String |
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插入新的点。
参数:
String
:schema名称。List<Node>
:待插入的Node
列表。InsertRequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。若将InsertRequestConfig.silent
设定为false,Response
对象会包含一个nodes
别名,所有插入的点均在其中。
// 向图集lcc名为user的schema插入两个点,打印错误代码和插入点的信息
InsertRequestConfig insertRequestConfig = new InsertRequestConfig();
insertRequestConfig.setInsertType(Ultipa.InsertType.NORMAL);
insertRequestConfig.setGraphName("lcc");
insertRequestConfig.setSilent(false);
List<Node> nodeList = new ArrayList<>();
Node node1 = new Node();
node1.setUUID(1l);
node1.setID("U001");
Value value1 = Value.newBuilder()
.add("name", "Alice")
.add("age", 18)
.add("score", 65.32)
.add("birthday", "1993-5-4")
.add("location", new Point(23.63, 104.25))
.add("profile", "abc")
.add("interests", Arrays.asList("tennis", "violin"))
.add("permissionCodes", new HashSet<>(Arrays.asList(2004, 3025, 1025)))
.build();
node1.setValues(value1);
nodeList.add(node1);
Node node2 = new Node();
node2.setUUID(2l);
node2.setID("U002");
Value value2 = Value.newBuilder().add("name", "Bob").build();
node2.setValues(value2);
nodeList.add(node2);
Response response = client.insertNodes("user", nodeList, insertRequestConfig);
System.out.println(response.getStatus().getErrorCode());
// 如果InsertRequestConfig.silent设定为true,Response不返回别名
List<Node> insertedNodes = response.alias("nodes").asNodes();
for (Node node : insertedNodes) {
System.out.println(node.toString());
}
SUCCESS
Node(uuid=1, id=U001, schema=user, values={name=Alice, age=18, score=65.3200000000, birthday=1993-05-04T00:00, location=POINT(23.63 104.25), profile=[B@1e66f1f5, interests=[tennis, violin], permissionCodes=[3025, 1025, 2004]})
Node(uuid=2, id=U002, schema=user, values={name=Bob, age=null, score=null, birthday=null, location=null, profile=null, interests=null, permissionCodes=null})
insertEdges()
向当前图集的某个schema插入新的边。
参数:
String
:schema名称。List<Node>
:待插入的Edge
列表。InsertRequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。若将InsertRequestConfig.silent
设定为false,Response
对象会包含一个edges
别名,所有插入的边均在其中。
// 向图集lcc名为follows的schema插入两条边,打印错误代码和插入边的信息
InsertRequestConfig insertRequestConfig = new InsertRequestConfig();
insertRequestConfig.setInsertType(Ultipa.InsertType.NORMAL);
insertRequestConfig.setGraphName("lcc");
insertRequestConfig.setSilent(false);
List<Edge> edgeList = new ArrayList<>();
Edge edge1 = new Edge();
edge1.setUUID(1l);
edge1.setFrom("U001");
edge1.setTo("U002");
Value value1 = Value.newBuilder().add("createdOn", "2024-5-6").build();
edge1.setValues(value1);
edgeList.add(edge1);
Edge edge2 = new Edge();
edge2.setUUID(2l);
edge2.setFrom("U002");
edge2.setTo("U001");
Value value2 = Value.newBuilder().add("createdOn", "2024-5-8").build();
edge2.setValues(value2);
edgeList.add(edge2);
Response response = client.insertEdges("follows", edgeList, insertRequestConfig);
System.out.println(response.getStatus().getErrorCode());
// 如果InsertRequestConfig.silent设定为true,Response不返回别名
List<Edge> insertedEdges = response.alias("edges").asEdges();
for (Edge edge : insertedEdges) {
System.out.println(edge.toString());
}
SUCCESS
Edge(uuid=1, fromUuid=1, toUuid=2, from=U001, to=U002, schema=follows, values={createdOn=Mon May 06 00:00:00 CST 2024})
Edge(uuid=2, fromUuid=2, toUuid=1, from=U002, to=U001, schema=follows, values={createdOn=Wed May 08 00:00:00 CST 2024})
insertNodesBatchBySchema()
通过gRPC向当前图集的某个schema插入新点。插入的点,其属性必须和schema结构中的声明保持一致。
参数:
Schema
:目标schema。List<Node>
:待插入的Node
列表。InsertRequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。若将InsertRequestConfig.silent
设定为false,Response.InsertNodesReply
将包含插入报告。
// 向图集lcc名为user的schema插入两个点,打印错误代码和插入结果
InsertRequestConfig insertRequestConfig = new InsertRequestConfig();
insertRequestConfig.setInsertType(Ultipa.InsertType.NORMAL);
insertRequestConfig.setGraphName("lcc");
insertRequestConfig.setSilent(false);
ArrayList<Property> properties = new ArrayList<>();
Property property1 = new Property(), property2 = new Property(), property3 = new Property(), property4 = new Property(), property5 = new Property(), property6 = new Property(), property7 = new Property(), property8 = new Property();
property1.setName("name");
property1.setType("string");
property2.setName("age");
property2.setType("int32");
property3.setName("score");
property3.setType("decimal");
property4.setName("birthday");
property4.setType("datetime");
property5.setName("location");
property5.setType("point");
property6.setName("profile");
property6.setType("blob");
property7.setName("interests");
property7.setType("string[]");
property8.setName("permissionCodes");
property8.setType("set(int32)");
properties.add(property1);
properties.add(property2);
properties.add(property3);
properties.add(property4);
properties.add(property5);
properties.add(property6);
properties.add(property7);
properties.add(property8);
Schema schema = new Schema();
schema.setName("user");
schema.setProperties(properties);
List<Node> nodeList = new ArrayList<>();
Node node1 = new Node();
node1.setUUID(1l);
node1.setID("U001");
Value value1 = Value.newBuilder()
.add("name", "Alice")
.add("age", 18)
.add("score", 65.32)
//.add("birthday", "1993-05-04")
//.add("birthday", new Date(736473600000l)) // 时间戳以毫秒计
.add("birthday", LocalDateTime.of(1993,5,4,00,00))
.add("location", new Point(23.63, 104.25))
.add("profile", "abc")
.add("interests", Arrays.asList("tennis", "violin"))
.add("permissionCodes", new HashSet<>(Arrays.asList(2004, 3025, 1025)))
.build();
node1.setValues(value1);
nodeList.add(node1);
Node node2 = new Node();
node2.setUUID(2l);
node2.setID("U002");
Value value2 = Value.newBuilder()
.add("name", "Bob")
.add("age", null)
.add("score", null)
.add("birthday", null)
.add("location", null)
.add("profile", null)
.add("interests", null)
.add("permissionCodes", null)
.build();
node2.setValues(value2);
nodeList.add(node2);
Response response = client.insertNodesBatchBySchema(schema, nodeList, insertRequestConfig);
System.out.println(response.getStatus().getErrorCode());
// 如果InsertRequestConfig.silent设定为true,Response.InsertNodesReply为空
System.out.println(response.getInsertNodesReply());
SUCCESS
InsertResponse(idList=[], uuidList=[1, 2], errorItems={})
insertEdgesBatchBySchema()
通过gRPC向当前图集的某个schema插入新边。插入的边,其属性必须和schema结构中的声明保持一致。
参数:
Schema
:目标schema。List<Edge>
:待插入的Edge
列表。InsertRequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。若将InsertRequestConfig.silent
设定为false,Response.InsertNodesReply
将包含插入报告。
// 向图集lcc名为follows的schema插入两条边,打印错误代码和插入结果
InsertRequestConfig insertRequestConfig = new InsertRequestConfig();
insertRequestConfig.setInsertType(Ultipa.InsertType.NORMAL);
insertRequestConfig.setGraphName("lcc");
insertRequestConfig.setSilent(false);
ArrayList<Property> properties = new ArrayList<>();
Property property = new Property();
property.setName("createdOn");
property.setType("timestamp");
properties.add(property);
Schema schema = new Schema();
schema.setName("follows");
schema.setProperties(properties);
List<Edge> edgeList = new ArrayList<>();
Edge edge1 = new Edge();
edge1.setUUID(1l);
edge1.setFrom("U001");
edge1.setTo("U002");
Value value1 = Value.newBuilder().add("createdOn", "2024-05-06").build();
edge1.setValues(value1);
edgeList.add(edge1);
Edge edge2 = new Edge();
edge2.setUUID(2l);
edge2.setFrom("U002");
edge2.setTo("U001");
Value value2 = Value.newBuilder().add("createdOn", new Date(1715169600l)).build();
edge2.setValues(value2);
edgeList.add(edge2);
Response response = client.insertEdgesBatchBySchema(schema, edgeList, insertRequestConfig);
System.out.println(response.getStatus().getErrorCode());
// 如果InsertRequestConfig.silent设定为true,Response.InsertEdgesReply为空
System.out.println(response.getInsertEdgesReply());
SUCCESS
InsertResponse(idList=null, uuidList=[1, 2], errorItems={})
insertNodesBatchAuto()
通过gRPC向当前图集的一个或多个schema插入新点。插入的点,其属性必须和schema结构中的声明保持一致。
参数:
List<Node>
:待插入的Node
列表。InsertRequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。若将InsertRequestConfig.silent
设定为false,Response.InsertNodesReply
将包含插入报告。
// 向图集lcc名为user和product的两个schema各插入一个点,打印错误代码和插入结果
InsertRequestConfig insertRequestConfig = new InsertRequestConfig();
insertRequestConfig.setInsertType(Ultipa.InsertType.NORMAL);
insertRequestConfig.setGraphName("lcc");
insertRequestConfig.setSilent(false);
List<Node> nodeList = new ArrayList<>();
Node node1 = new Node();
node1.setSchema("user");
node1.setUUID(1l);
node1.setID("U001");
Value value1 = Value.newBuilder()
.add("name", "Alice")
.add("age", 18)
.add("score", 65.32)
//.add("birthday", "1993-05-04")
//.add("birthday", new Date(736473600000l)) // 时间戳以毫秒计
.add("birthday", LocalDateTime.of(1993,5,4,00,00))
.add("location", new Point(23.63, 104.25))
.add("profile", "abc")
.add("interests", Arrays.asList("tennis", "violin"))
.add("permissionCodes", new HashSet<>(Arrays.asList(2004, 3025, 1025)))
.build();
node1.setValues(value1);
nodeList.add(node1);
Node node2 = new Node();
node2.setSchema("user");
node2.setUUID(2l);
node2.setID("U002");
Value value2 = Value.newBuilder()
.add("name", "Bob")
.add("age", null)
.add("score", null)
.add("birthday", null)
.add("location", null)
.add("profile", null)
.add("interests", null)
.add("permissionCodes", null)
.build();
node2.setValues(value2);
nodeList.add(node2);
Node node3 = new Node();
node3.setSchema("product");
node3.setUUID(3l);
node3.setID("P001");
Value value3 = Value.newBuilder()
.add("name", "Wireless Earbud")
.add("price", 93.2f)
.build();
node3.setValues(value3);
nodeList.add(node3);
Response response = client.insertNodesBatchAuto(nodeList, insertRequestConfig);
System.out.println(response.getStatus().getErrorCode());
// 如果InsertRequestConfig.silent设定为true,Response.InsertNodesReply为空
System.out.println(response.getInsertNodesReply());
SUCCESS
InsertResponse(idList=[], uuidList=[3, 1, 2], errorItems={})
insertEdgesBatchAuto()
通过gRPC向当前图集的一个或多个schema插入新边。插入的边,其属性必须和schema结构中的声明保持一致。
参数:
List<Edge>
:待插入的Edge
列表。InsertRequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。若将InsertRequestConfig.silent
设定为false,Response.InsertNodesReply
将包含插入报告。
// 向图集lcc名为follows的schema插入两条边,向名为purchased的schema插入一条边,打印错误代码和插入结果
InsertRequestConfig insertRequestConfig = new InsertRequestConfig();
insertRequestConfig.setInsertType(Ultipa.InsertType.NORMAL);
insertRequestConfig.setGraphName("lcc");
insertRequestConfig.setSilent(false);
List<Edge> edgeList = new ArrayList<>();
Edge edge1 = new Edge();
edge1.setSchema("follows");
edge1.setUUID(1l);
edge1.setFrom("U001");
edge1.setTo("U002");
Value value1 = Value.newBuilder().add("createdOn", "2024-05-06").build();
edge1.setValues(value1);
edgeList.add(edge1);
Edge edge2 = new Edge();
edge2.setSchema("follows");
edge2.setUUID(2l);
edge2.setFrom("U002");
edge2.setTo("U001");
Value value2 = Value.newBuilder().add("createdOn", new Date(1715169600000l)).build();
edge2.setValues(value2);
edgeList.add(edge2);
Edge edge3 = new Edge();
edge3.setSchema("purchased");
edge3.setUUID(3l);
edge3.setFrom("U002");
edge3.setTo("P001");
Value value3 = Value.newBuilder().add("qty", 1l).build();
edge3.setValues(value3);
edgeList.add(edge3);
Response response = client.insertEdgesBatchAuto(edgeList, insertRequestConfig);
System.out.println(response.getStatus().getErrorCode());
// 如果InsertRequestConfig.silent设定为true,Response.InsertEdgesReply为空
System.out.println(response.getInsertEdgesReply());
SUCCESS
InsertResponse(idList=[], uuidList=[3, 1, 2], errorItems={})
删除
deleteNodes()
从图集删除符合指定条件的点。需特别注意,删除点的同时也会删除与点相连的边。
参数:
String
:待删除的点的条件。Integer
(可选):最多删除点的个数;忽略此参数或将其设定为-1
将删除所有点。InsertRequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。若将InsertRequestConfig.silent
设定为false,Response
对象会包含一个nodes
别名,所有删除的点均在其中。
// 删除图集lcc里@user下名为Alice的点,打印错误代码和已删除的点的信息
// 与删除的点相连的边也会全部删除
InsertRequestConfig insertRequestConfig = new InsertRequestConfig();
insertRequestConfig.setInsertType(Ultipa.InsertType.NORMAL);
insertRequestConfig.setGraphName("lcc");
insertRequestConfig.setSilent(false);
Response response = client.deleteNodes("@user.name == 'Alice'", 1, insertRequestConfig);
System.out.println(response.getStatus().getErrorCode());
// 如果InsertRequestConfig.silent设定为true,Response不返回别名
for (Node node : deletedNodes) {
System.out.println(node.toString());
}
SUCCESS
Node(uuid=1, id=U001, schema=user, values={name=Alice})
deleteEdges()
从图集删除符合指定条件的边。
参数:
String
:待删除的边的条件。Integer
(可选):最多删除边的条数;忽略此参数或将其设定为-1
将删除所有边。InsertRequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。若将InsertRequestConfig.silent
设定为false,Response
对象会包含一个edges
别名,所有删除的边均在其中。
// 从图集lcc删除@purchased中的全部边,打印错误代码和已删除的边的信息
InsertRequestConfig insertRequestConfig = new InsertRequestConfig();
insertRequestConfig.setInsertType(Ultipa.InsertType.NORMAL);
insertRequestConfig.setGraphName("lcc");
insertRequestConfig.setSilent(false);
Response response = client.deleteEdges("@purchased", insertRequestConfig);
System.out.println(response.getStatus().getErrorCode());
// 如果InsertRequestConfig.silent设定为true,Response不返回别名
List<Edge> deletedEdges = response.alias("edges").asEdges();
for (Edge edge : deletedEdges) {
System.out.println(edge.toString());
}
SUCCESS
Edge(uuid=3, fromUuid=2, toUuid=3, from=U002, to=P001, schema=purchased, values={})
完整示例
package com.ultipa.www.sdk.api;
import com.ultipa.Ultipa;
import com.ultipa.sdk.connect.Connection;
import com.ultipa.sdk.connect.conf.InsertRequestConfig;
import com.ultipa.sdk.connect.conf.UltipaConfiguration;
import com.ultipa.sdk.connect.driver.UltipaClientDriver;
import com.ultipa.sdk.operate.entity.*;
import com.ultipa.sdk.operate.response.Response;
import com.ultipa.sdk.data.Point;
import java.time.LocalDateTime;
import java.util.*;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 设置连接
UltipaConfiguration myConfig = UltipaConfiguration.config()
// URI 示例: .hosts("mqj4zouys.us-east-1.cloud.ultipa.com:60010")
.hosts("192.168.1.85:60611,192.168.1.87:60611,192.168.1.88:60611")
.username("<username>")
.password("<password>");
UltipaClientDriver driver = null;
try {
// 建立与数据库的连接
driver = new UltipaClientDriver(myConfig);
Connection client = driver.getConnection();
Thread.sleep(3000);
// 配置插入请求
InsertRequestConfig insertRequestConfig = new InsertRequestConfig();
insertRequestConfig.setInsertType(Ultipa.InsertType.NORMAL);
insertRequestConfig.setGraphName("lcc");
insertRequestConfig.setSilent(false);
// 向图集lcc名为user的schema插入两个点,向名为product的schema插入一个点,打印错误代码和插入报告
List<Node> nodeList = new ArrayList<>();
Node node1 = new Node();
node1.setSchema("user");
node1.setUUID(1l);
node1.setID("U001");
Value value1 = Value.newBuilder()
.add("name", "Alice")
.add("age", 18)
.add("score", 65.32)
//.add("birthday", "1993-05-04")
//.add("birthday", new Date(736473600000l)) // 时间戳以毫秒计
.add("birthday", LocalDateTime.of(1993,5,4,00,00))
.add("location", new Point(23.63, 104.25))
.add("profile", "abc")
.add("interests", Arrays.asList("tennis", "violin"))
.add("permissionCodes", new HashSet<>(Arrays.asList(2004, 3025, 1025)))
.build();
node1.setValues(value1);
nodeList.add(node1);
Node node2 = new Node();
node2.setSchema("user");
node2.setUUID(2l);
node2.setID("U002");
Value value2 = Value.newBuilder()
.add("name", "Bob")
.add("age", null)
.add("score", null)
.add("birthday", null)
.add("location", null)
.add("profile", null)
.add("interests", null)
.add("permissionCodes", null)
.build();
node2.setValues(value2);
nodeList.add(node2);
Node node3 = new Node();
node3.setSchema("product");
node3.setUUID(3l);
node3.setID("P001");
Value value3 = Value.newBuilder()
.add("name", "Wireless Earbud")
.add("price", 93.2f)
.build();
node3.setValues(value3);
nodeList.add(node3);
Response response1 = client.insertNodesBatchAuto(nodeList, insertRequestConfig);
System.out.println("Node insertion status: " + response1.getStatus().getErrorCode());
// 如果InsertRequestConfig.silent设定为true,Response.InsertNodesReply为空
System.out.println("Node inserted: " + response1.getInsertNodesReply());
// 向图集lcc名为follows的schema插入两条边,向名为purchased的schema插入一条边,打印错误代码和插入结果
List<Edge> edgeList = new ArrayList<>();
Edge edge1 = new Edge();
edge1.setSchema("follows");
edge1.setUUID(1l);
edge1.setFrom("U001");
edge1.setTo("U002");
Value value4 = Value.newBuilder().add("createdOn", "2024-05-06").build();
edge1.setValues(value4);
edgeList.add(edge1);
Edge edge2 = new Edge();
edge2.setSchema("follows");
edge2.setUUID(2l);
edge2.setFrom("U002");
edge2.setTo("U001");
Value value5 = Value.newBuilder().add("createdOn", new Date(1715169600000l)).build();
edge2.setValues(value5);
edgeList.add(edge2);
Edge edge3 = new Edge();
edge3.setSchema("purchased");
edge3.setUUID(3l);
edge3.setFrom("U002");
edge3.setTo("P001");
Value value6 = Value.newBuilder().add("qty", 1l).build();
edge3.setValues(value6);
edgeList.add(edge3);
Response response2 = client.insertEdgesBatchAuto(edgeList, insertRequestConfig);
System.out.println("Edge insertion status: " + response2.getStatus().getErrorCode());
// 如果InsertRequestConfig.silent设定为true,Response.InsertEdgesReply为空
System.out.println("Edge inserted: " + response2.getInsertEdgesReply());
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
if (driver != null) {
driver.close();
}
}
}
}