本节为您介绍如何使用Connection
对象的方法管理数据库中的图集。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
showGraph()
从数据库获取全部图集。
参数:
RequestConfig
(可选):配置请求。
返回值:
List<GraphSet>
:数据库中全部图集列表。
// 获取全部图集并打印边数超过2000条的图集名称
List<GraphSet> graphSetList = client.showGraph();
for (GraphSet graphSet : graphSetList) {
if (graphSet.getTotalEdges() > 2000) {
System.out.println(graphSet.getName());
}
}
Display_Ad_Click
ERP_DATA2
wikiKG
getGraph()
根据图集名称从数据库获取图集信息。
参数:
String
:图集名称。RequestConfig
(可选):配置请求。
返回值:
GraphSet
:目标图集。
// 获取名为wikiKG的图集并打印其全部信息
GraphSet graph = client.getGraph("wikiKG");
Assert.assertEquals("wikiKG",graph.getName());
System.out.println(new Gson().toJson(graph));
{"id":615,"name":"wikiKG","totalNodes":3546,"totalEdges":2179,"status":"MOUNTED","description":""}
createGraph()
在数据库中新建图集。
参数:
GraphSet
:待创建的图集;必须设置name
字段,description
字段可选。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
// 创建图集并打印错误代码
GraphSet graph = new GraphSet();
graph.setName("testJavaSDK");
graph.setDescription("testJavaSDK Desc");
Response response = client.createGraph(graph);
System.out.println(response.getStatus().getErrorCode());
数据库中已创建名为testJavaSDK
的新图集,驱动程序打印结果如下:
SUCCEED
createGraphIfNotExist()
在数据库中新建图集,若同名图集已存在,忽略报错。
参数:
GraphSet
:待创建的图集;必须设置name
字段,description
字段可选。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
// 创建图集并打印错误代码
GraphSet graph = new GraphSet();
graph.setName("testJavaSDK");
graph.setDescription("testJavaSDK Desc");
Response response1 = client.createGraphIfNotExist(graph);
System.out.println("First Creation: " + response1.getStatus().getErrorCode());
// 尝试再次创建图集并打印错误代码
Response response2 = client.createGraphIfNotExist(graph);
System.out.println("Second Creation: " + response2.getStatus().getErrorCode());
数据库中已创建名为testJavaSDK
的图集,驱动程序打印结果如下:
First Creation: SUCCESS
Second Creation: SUCCESS
dropGraph()
根据图集名称从数据库中删除图集。
参数:
String
:图集名称。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
// 创建图集后将其删除并打印结果
GraphSet graph = new GraphSet();
graph.setName("newGraph");
Response response = client.createGraph(graph);
Assert.assertEquals(Ultipa.ErrorCode.SUCCESS,response.getStatus().getErrorCode());
Thread.sleep(2000);
Response response1 = client.dropGraph("newGraph");
Assert.assertEquals(Ultipa.ErrorCode.SUCCESS,response1.getStatus().getErrorCode());
System.out.println(new Gson().toJson(response1));
{"host":"192.168.1.85:60611","statistic":{"rowAffected":0,"totalTimeCost":0,"engineTimeCost":0,"nodeAffected":0,"edgeAffected":0,"totalCost":8,"engineCost":0},"status":{"errorCode":"SUCCESS","msg":"","clusterInfo":{"redirect":"","leaderAddress":"","followers":[]}},"aliases":[],"items":{},"explainPlan":{"planNodes":[]}}
alterGraph()
根据图集名称,修改数据库中已有图集的名称和描述信息。
参数:
GraphSet
:待修改的图集;必须设置name
字段。GraphSet
:更新已有图集的配置;必须设置name
字段和description
字段。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
// 将图集testJavaSDK重命名为newGraph,添加图集描述并打印结果
GraphSet oldGraph = new GraphSet();
oldGraph.setName("testJavaSDK");
GraphSet newGraph = new GraphSet();
newGraph.setName("newGraph");
newGraph.setDescription("The graphset is altered");
Response response = client.alterGraph(oldGraph, newGraph);
System.out.println(new Gson().toJson(response));
{"host":"192.168.1.85:60611","statistic":{"rowAffected":0,"totalTimeCost":0,"engineTimeCost":0,"nodeAffected":0,"edgeAffected":0,"totalCost":1,"engineCost":0},"status":{"errorCode":"SUCCESS","msg":"","clusterInfo":{"redirect":"","leaderAddress":"","followers":[]}},"aliases":[],"items":{},"explainPlan":{"planNodes":[]}}
truncate()
清空(删除)给定图集中的指定点或边,或清空全部图集。请注意,清空点数据的同时会删除与点相连的所有边。清空操作仅删除点边数据,不会删除schema和属性信息。
参数:
Truncate
:待清空的对象;必须设置graphName
字段,可选择设置schemaDbType
字段和schemaName
字段,但设置schemaName
字段时,必须设置schemaDbType
字段。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
// 清空图集myGraph_1中@user点数据并打印错误代码
Truncate truncate1 = new Truncate();
truncate1.setGraphName("myGraph_1");
truncate1.setSchemaDbType(Ultipa.DBType.DBNODE);
truncate1.setSchemaName("user");
Response response1 = client.truncate(truncate1);
System.out.println(response1.getStatus().getErrorCode());
// 清空图集myGraph_2中全部边数据并打印错误代码
Truncate truncate2 = new Truncate();
truncate2.setGraphName("myGraph_2");
truncate2.setSchemaDbType(Ultipa.DBType.DBEDGE);
Response response2 = client.truncate(truncate2);
System.out.println(response2.getStatus().getErrorCode());
// 清空图集myGraph_3并打印错误代码
Truncate truncate3 = new Truncate();
truncate3.setGraphName("myGraph_3");
Response response3 = client.truncate(truncate3);
System.out.println(response3.getStatus().getErrorCode());
SUCCESS
SUCCESS
SUCCESS
compact()
清除服务器磁盘上的无效及冗余数据以压缩图集。有效数据不会受到影响。
参数:
String
:图集名称。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
// 压缩图集miniCircle并打印错误代码
Response response = client.compact("miniCircle");
System.out.println(response.getStatus().getErrorCode());
SUCCESS
hasGraph()
根据图集名称查看数据库中是否存在该图集。
参数:
String
:图集名称。RequestConfig
(可选):配置请求。
返回值:
boolean
:请求的结果。
// 查看图集miniCircle是否存在并打印结果
boolean has = client.hasGraph("miniCircle");
System.out.println("has = " + has);
has = true
unmountGraph()
卸载图集以节省数据库内存。
参数:
String
:图集名称。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
// 卸载图集miniCircle并打印图集状态
client.unmountGraph("miniCircle");
Thread.sleep(3000);
GraphSet graphSet = client.getGraph("miniCircle");
System.out.println(graphSet.getStatus());
UNMOUNTED
mountGraph()
将图集挂载至数据库内存。
参数:
String
:图集名称。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
// 挂载图集miniCircle并打印图集状态
client.mountGraph("miniCircle");
Thread.sleep(3000);
GraphSet graphSet = client.getGraph("miniCircle");
System.out.println(graphSet.getStatus());
MOUNTED
完整示例
package com.ultipa.www.sdk.api;
import com.google.gson.Gson;
import com.ultipa.Ultipa;
import com.ultipa.sdk.connect.Connection;
import com.ultipa.sdk.connect.conf.UltipaConfiguration;
import com.ultipa.sdk.connect.driver.UltipaClientDriver;
import com.ultipa.sdk.connect.conf.RequestConfig;
import com.ultipa.sdk.operate.entity.*;
import com.ultipa.sdk.operate.response.Response;
import org.junit.Assert;
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);
// 配置请求
RequestConfig requestConfig = new RequestConfig();
requestConfig.setUseMaster(true);
// 创建图集newGraph
GraphSet graph = new GraphSet();
graph.setName("newGraph");
Response response = client.createGraph(graph, requestConfig);
Assert.assertEquals(Ultipa.ErrorCode.SUCCESS,response.getStatus().getErrorCode());
Thread.sleep(2000);
// 删除刚刚创建的图集newGraph
Response response1 = client.dropGraph("newGraph");
Assert.assertEquals(Ultipa.ErrorCode.SUCCESS,response1.getStatus().getErrorCode());
System.out.println(new Gson().toJson(response1));
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
if (driver != null) {
driver.close();
}
}
}
}