本节介绍用于管理数据库中图的方法。
showGraph()
获取数据库中全部的图。
参数
config: RequestConfig
(可选):请求配置。
返回值
List<GraphSet>
:获取的图列表
// Retrieves all graphs and prints the names of those with over 2000 edges
List<GraphSet> graphSetList = driver.showGraph();
for (GraphSet graphSet : graphSetList) {
if (graphSet.getTotalEdges() > 2000) {
System.out.println(graphSet.getName());
}
}
Display_Ad_Click
ERP_DATA2
wikiKG
getGraph()
获取数据库中一个指定的图。
参数
graphName: String
:图名称。config: RequestConfig
(可选):请求配置。
返回值
GraphSet
:获取的图。
// Retrieves the graph named 'miniCircle'
GraphSet graph = driver.getGraph("miniCircle");
System.out.println(toJson(graph));
{"id": "444", "name": "miniCircle", "totalNodes": 304, "totalEdges": 1961, "shards": ["1"], "partitionBy": "CityHash64", "status": "NORMAL", "description": "", "slotNum": 256}
hasGraph()
检查数据库中是否存在一个指定的图。
参数
graphName: String
:图名称。config: RequestConfig
(可选):请求配置。
返回值
bool
:检查结果。
// Checks the existence of a graph named 'miniCircle'
boolean response = driver.hasGraph("miniCircle");
System.out.println(response);
true
createGraph()
在数据库中创建一个图。
参数
graphSet: GraphSet
:待创建的图;name
属性必填,shards
、partitionBy
和description
可选。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Creates a graph
GraphSet graph = new GraphSet();
graph.setName("testJavaSDK");
graph.setShards(Lists.newArrayList("1"));
graph.setPartitionBy("Crc32");
graph.setDescription("testJavaSDK desc");
Response response = driver.createGraph(graph);
System.out.println(response.getStatus().getCode());
SUCCESS
createGraphIfNotExist()
在数据库中创建一个图,并返回是否数据库中已有同名图存在。
参数
graphSet: GraphSet
:待创建的图;name
属性必填,shards
、partitionBy
和description
可选。config: RequestConfig
(可选):请求配置。
返回值
ResponseWithExistCheck
:请求结果。
GraphSet graph = new GraphSet();
graph.setName("testJavaSDK");
graph.setShards(Lists.newArrayList("1"));
graph.setPartitionBy("Crc32");
graph.setDescription("testJavaSDK desc");
ResponseWithExistCheck result = driver.createGraphIfNotExist(graph);
System.out.println("Does the graph already exist? " + result.getExist());
if(result.getResponse() == null) {
System.out.println("Graph creation status: No response");
} else {
System.out.println("Graph creation status: " + result.getResponse().getStatus().getCode());
}
Thread.sleep(3000);
System.out.println("----- Creates the graph again -----");
ResponseWithExistCheck result1 = driver.createGraphIfNotExist(graph);
System.out.println("Does the graph already exist? " + result1.getExist());
if(result1.getResponse() == null) {
System.out.println("Graph creation status: No response");
} else {
System.out.println("Graph creation status: " + result1.getResponse().getStatus().getCode());
}
Does the graph already exist? false
Graph creation status: SUCCESS
----- Creates the graph again -----
Does the graph already exist? true
Graph creation status: No response
alterGraph()
修改数据库中一个图的名称和描述。
参数
graphName: String
:图名称。alterGraphset: GraphSet
:用于设置新的图name
和/或description
的GraphSet
对象。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Alters the name and description of the graph 'testPythonSDK'
GraphSet newGraphInfo = new GraphSet();
newGraphInfo.setName("newGraph");
newGraphInfo.setDescription("a new graph");
Response response = driver.alterGraph("testJavaSDK", newGraphInfo);
System.out.println(response.getStatus().getCode());
SUCCESS
dropGraph()
从数据库中删除一个指定的图。
参数
graphName: String
:图名称。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Drops the graph 'testPythonSDK'
Response response = driver.dropGraph("testJavaSDK");
System.out.println(response.getStatus().getCode());
SUCCESS
truncate()
清空(删除)图中的指定点或边,或清空整个图。请注意,清空点的同时会删除与点相连的所有边。清空操作仅删除点边,图的schema和属性定义仍保留。
参数
request: TruncateParams
:清空操作的参数;graphName
属性必填,schemaName
和dbType
可选。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Truncates User nodes in 'myGraph'
TruncateParams param1 = new TruncateParams();
param1.setGraphName("myGraph");
param1.setSchemaName("User");
param1.setDbType(Ultipa.DBType.DBNODE);
Response response1 = driver.truncate(param1);
System.out.println(response1.getStatus().getCode());
// Truncates all edges in the 'myGraph'
TruncateParams param2 = new TruncateParams();
param2.setGraphName("myGraph");
param2.setSchemaName("*");
param2.setDbType(Ultipa.DBType.DBEDGE);
Response response2 = driver.truncate(param2);
System.out.println(response2.getStatus().getCode());
// Truncates 'myGraph'
TruncateParams param3 = new TruncateParams();
param3.setGraphName("myGraph");
Response response3 = driver.truncate(param3);
System.out.println(response3.getStatus().getCode());
SUCCESS
SUCCESS
SUCCESS
compact()
清除图中的无效及冗余数据。有效数据不会受到影响。
参数
graphName: String
:图名称。config: RequestConfig
(可选):请求配置。
返回值
JobResponse
:请求结果。
// Compacts the graph 'miniCircle'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("miniCircle");
JobResponse response = driver.compact("miniCircle");
String jobId = response.getJobId();
Thread.sleep(3000);
List<Job> jobs = driver.showJob(jobId, requestConfig);
for (Job job : jobs) {
System.out.println(job.getId() + " - " + job.getStatus());
}
45 - FINISHED
45_1 - FINISHED
45_2 - FINISHED
45_3 - FINISHED
完整示例
package com.ultipa.www.sdk.api;
import com.ultipa.sdk.UltipaDriver;
import com.ultipa.sdk.connect.conf.UltipaConfig;
import com.ultipa.sdk.operate.entity.GraphSet;
import com.ultipa.sdk.operate.response.Response;
import org.assertj.core.util.Lists;
public class Main {
public static void main(String[] args) {
UltipaConfig ultipaConfig = UltipaConfig.config()
// URI example: .hosts(Lists.newArrayList("d3026ac361964633986849ec43b84877s.eu-south-1.cloud.ultipa.com:8443"))
.hosts(Lists.newArrayList("192.168.1.85:60061","192.168.1.88:60061","192.168.1.87:60061"))
.username("<username>")
.password("<password>");
UltipaDriver driver = null;
try {
driver = new UltipaDriver(ultipaConfig);
// Creates a graph
GraphSet graph = new GraphSet();
graph.setName("testJavaSDK");
graph.setShards(Lists.newArrayList("1"));
graph.setPartitionBy("Crc32");
graph.setDescription("testJavaSDK desc");
Response response = driver.createGraph(graph);
System.out.println(response.getStatus().getCode());
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
if (driver != null) {
driver.close();
}
}
}
}