本节为您介绍如何使用Connection
对象的方法管理图集中点和边的schema与属性。
每个示例主要展示如何使用所列方法。点击完整示例查看完整代码示例。
Schema
showSchema()
从当前图集获取全部点、边的schema信息。
参数:
RequestConfig
(可选):配置请求。
返回值:
List<Schema>
:当前图集全部schema列表。
// 获取图集UltipaTeam的所有schema并打印schema的类型和名称
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
List<Schema> schemas = client.showSchema(requestConfig);
Assert.assertTrue(schemas.size() > 0);
for (Schema schema : schemas) {
System.out.println(schema.getDbType() + ": " + schema.getName());
}
DBNODE: default
DBNODE: member
DBNODE: organization
DBEDGE: default
DBEDGE: reportsTo
DBEDGE: relatesTo
getSchema()
从当前图集获取一个点schema或一个边schema。
参数:
String
:schema名称。DBtype
:schema类型(点schema 或边schema)。RequestConfig
(可选):配置请求。
返回值:
Schema
:目标schema。
// 获取图集UltipaTeam中名为member的点schema和名为connectsTo的边schema,并打印其信息
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
Schema schema1 = client.getSchema("member", Ultipa.DBType.DBNODE, requestConfig);
System.out.println("schema1: " + new Gson().toJson(schema1));
Schema schema2 = client.getSchema("connectsTo", Ultipa.DBType.DBEDGE, requestConfig);
System.out.println("schema2: " + new Gson().toJson(schema2));
schema1: {"name":"member","description":"","properties":[{"name":"name","propertyType":"STRING","type":"string","lte":false,"description":"","ignored":false,"extra":"{}"},{"name":"title","propertyType":"STRING","type":"string","lte":false,"description":"","ignored":false,"extra":"{}"},{"name":"profile","propertyType":"STRING","type":"string","lte":false,"description":"","ignored":false,"extra":"{}"}],"dbType":"DBNODE","total":7}
schema2: null
showNodeSchema()
获取当前图集的全部点schema。
参数:
RequestConfig
(可选):配置请求。
返回值:
List<Schema>
:当前图集全部点schema列表。
// 获取图集UltipaTeam的全部点schema并打印其名称
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
List<Schema> schemas = client.showNodeSchema(requestConfig);
Assert.assertTrue(schemas.size() > 0);
for (Schema schema : schemas) {
System.out.println(schema.getName());
}
default
member
organization
showEdgeSchema()
获取当前图集的全部边schema。
参数:
RequestConfig
(可选):配置请求。
返回值:
List<Schema>
:当前图集的全部边schema列表。
// 获取图集UltipaTeam的全部边schema并打印其名称
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
List<Schema> schemas = client.showEdgeSchema(requestConfig);
Assert.assertTrue(schemas.size() > 0);
for (Schema schema : schemas) {
System.out.println(schema.getName());
}
default
reportsTo
relatesTo
getNodeSchema()
获取当前图集的一个点schema。
参数:
String
:schema名称。RequestConfig
(可选):配置请求。
返回值:
Schema
:目标点schema。
// 获取图集UltipaTeam中名为member的点schema并打印其属性信息
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
Schema schema = client.getNodeSchema("member", requestConfig);
System.out.println(schema.getProperties());
[Property(name=name, propertyType=STRING, subPropertyTypes=null, type=string, lte=false, read=null, write=null, schema=null, description=, ignored=false, extra={}, encrypt=null, encrypted=null), Property(name=title, propertyType=STRING, subPropertyTypes=null, type=string, lte=false, read=null, write=null, schema=null, description=, ignored=false, extra={}, encrypt=null, encrypted=null), Property(name=profile, propertyType=STRING, subPropertyTypes=null, type=string, lte=false, read=null, write=null, schema=null, description=, ignored=false, extra={}, encrypt=null, encrypted=null)]
getEdgeSchema()
获取当前图集的一个边schema。
参数:
String
:schema名称。RequestConfig
(可选):配置请求。
返回值:
Schema
:目标边schema。
// 获取图集UltipaTeam中名为relatesTo的边schema并打印其属性信息
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
Schema schema = client.getEdgeSchema("relatesTo", requestConfig);
System.out.println(schema.getProperties());
[Property(name=type, propertyType=STRING, subPropertyTypes=null, type=string, lte=false, read=null, write=null, schema=null, description=, ignored=false, extra={}, encrypt=null, encrypted=null)]
createSchema()
在当前图集新建一个schema。
参数:
Schema
:待创建的schema;必须设置name
字段和dbType
字段,description
字段和properties
字段可选。boolean
(可选):是否创建属性,默认为false
。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
// 在图集UltipaTeam中新建名为utility的点schema并打印其全部信息
Schema schema1 = new Schema();
schema1.setName("utility");
schema1.setDescription("Office utilities");
schema1.setDbType(Ultipa.DBType.DBNODE);
Property property1 = new Property();
property1.setName("name");
property1.setType("string");
Property property2 = new Property();
property2.setName("purchaseDate");
property2.setType("datetime");
ArrayList<Property> propertyArrayList = new ArrayList<>();
propertyArrayList.add(property1);
propertyArrayList.add(property2);
schema1.setProperties(propertyArrayList);
Response response1 = client.createSchema(schema1, true, requestConfig);
System.out.println(response1.getStatus().getErrorCode());
Thread.sleep(3000);
Schema newSchema1 = client.getNodeSchema("utility", requestConfig);
System.out.println(new Gson().toJson(newSchema1));
// 在图集UltipaTeam中新建名为managedBy的边schema并打印其全部信息
Schema schema2 = new Schema();
schema2.setName("managedBy");
schema2.setDbType(Ultipa.DBType.DBEDGE);
Response response2 = client.createSchema(schema2, false, requestConfig);
System.out.println(response2.getStatus().getErrorCode());
Thread.sleep(3000);
Schema newSchema2 = client.getEdgeSchema("managedBy", requestConfig);
System.out.println(new Gson().toJson(newSchema2));
SUCCESS
{"name":"utility","description":"Office utilities","properties":[{"name":"name","propertyType":"STRING","type":"string","lte":false,"description":"","ignored":false,"extra":"{}"},{"name":"purchaseDate","propertyType":"DATETIME","type":"datetime","lte":false,"description":"","ignored":false,"extra":"{}"}],"dbType":"DBNODE","total":0}
SUCCESS
{"name":"managedBy","description":"null,"properties":[],"dbType":"DBEDGE","total":0}
createSchemaIfNotExist()
在当前图集新建一个schema,若同名schema已存在,忽略报错。
参数:
Schema
:待创建的schema;必须设置name
字段和dbType
字段,description
字段和properties
字段可选。boolean
(可选):是否创建属性,默认为false
。RequestConfig
(可选):配置请求。
返回值:
boolean
:创建是否发生。
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
Schema schema = new Schema();
schema.setName("utility");
schema.setDescription("Office utilities");
schema.setDbType(Ultipa.DBType.DBNODE);
// 在图集UltipaTeam中新建一个schema并打印创建是否发生
boolean flag1 = client.createSchemaIfNotExist(schema, requestConfig);
System.out.println(flag1);
// 再次创建同个schema并打印创建是否发生
Thread.sleep(3000);
boolean flag2 = client.createSchemaIfNotExist(schema, requestConfig);
System.out.println(flag2);
true
false
alterSchema()
根据schema名称修改当前图集中某个schema的名称和描述。
参数:
Schema
:图集中待修改的schema;必须设置name
字段和DbType
字段。Schema
:更新已有schema的配置;对于name
字段和description
字段,必须至少设置其中一个字段。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
// 将图集UltipaTeam中的一个点schema由utility改为securityUtility并删除其描述
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
Schema schema = client.getSchema("utility", Ultipa.DBType.DBNODE, requestConfig);
Schema newSchema = new Schema();
newSchema.setName("securityUtility");
newSchema.setDescription("yyyy");
Response response = client.alterSchema(schema, newSchema, requestConfig);
System.out.println(response.getStatus().getErrorCode());
SUCCESS
dropSchema()
根据schema名称从当前图集删除某个schema。
参数:
Schema
:图集中待删除的schema;必须设置name
字段和DbType
字段。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
// 在图集UltipaTeam中删除名为utility的点schema
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
Schema schema = client.getSchema("utility", Ultipa.DBType.DBNODE, requestConfig);
Response response = client.dropSchema(schema, requestConfig);
System.out.println(new Gson().toJson(response));
{"host":"192.168.1.87: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":[]}}
Property
showProperty()
获取当前图集的自定义点属性信息或边属性信息。
参数:
DBType
:属性类型(点属性或边属性)。String
(可选):schema名称。RequestConfig
(可选):配置请求。
返回值:
List<Property>
:当前图集全部目标属性列表。
// 获取图集UltipaTeam的所有自定义点属性信息并打印其名称、类型和关联的schema
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
List<Property> propertyList = client.showProperty(Ultipa.DBType.DBNODE, requestConfig);
for (Property property : propertyList) {
System.out.println(property.getName() + "(" + property.getType() + ")" + " is associated with schema named " + property.getSchema());
}
name(string) is associated with schema named member
title(string) is associated with schema named member
profile(string) is associated with schema named member
name(string) is associated with schema named organization
logo(string) is associated with schema named organization
showNodeProperty()
获取当前图集的自定义点属性信息。
参数:
String
(可选):schema名称。RequestConfig
(可选):配置请求。
返回值:
List<Property>
:当前图集全部目标属性列表。
// 获取图集UltipaTeam中名为member的点schema其关联的所有自定义点属性信息并打印属性数量
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
List<Property> propertyList = client.showNodeProperty("member", requestConfig);
System.out.println(propertyList.size());
3
showEdgeProperty()
获取当前图集的自定义边属性信息。
参数:
String
(可选):schema名称。RequestConfig
(可选):配置请求。
返回值:
List<Property>
:当前图集全部目标属性列表。
// 获取图集UltipaTeam中名为relatesTo的边schema其关联的所有自定义边属性,并打印属性名称
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
List<Property> propertyList = client.showEdgeProperty("relatesTo", requestConfig);
for (Property property : propertyList) {
System.out.println(property.getName());
}
type
getProperty()
获取当前图集的一个自定义点属性或边属性。
参数:
String
:schema名称。DBType
属性类型(点属性或边属性)。String
:属性名称。RequestConfig
(可选):配置请求。
返回值:
Property
:目标属性。
// 获取图集UltipaTeam的点属性@member.title,并打印其全部信息
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
Property property = client.getProperty("member", Ultipa.DBType.DBNODE, "title", requestConfig);
System.out.println(new Gson().toJson(property));
{"name":"title","propertyType":"STRING","type":"string","lte":false,"read":true,"write":true,"schema":"member","description":"","ignored":false,"extra":"{}","encrypt":"","encrypted":false}
getNodeProperty()
获取当前图集的一个自定义点属性。
参数:
String
:schema名称。String
:属性名称。RequestConfig
(可选):配置请求。
返回值:
Property
:目标属性。
// 获取图集UltipaTeam的点属性@member.title并打印其全部信息
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
Property property = client.getNodeProperty("member", "title", requestConfig);
System.out.println(new Gson().toJson(property));
{"name":"title","propertyType":"STRING","type":"string","lte":false,"read":true,"write":true,"schema":"member","description":"","ignored":false,"extra":"{}","encrypt":"","encrypted":false}
getEdgeProperty()
获取当前图集的一个自定义边属性。
参数:
String
:schema名称。String
:属性名称。RequestConfig
(可选):配置请求。
返回值:
Property
:目标属性。
// 获取图集UltipaTeam的边属性@relatesTo.type并打印其全部信息
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
Property property = client.getEdgeProperty("relatesTo", "type", requestConfig);
System.out.println(new Gson().toJson(property));
{"name":"type","propertyType":"STRING","type":"string","lte":false,"read":true,"write":true,"schema":"relatesTo","description":"","ignored":false,"extra":"{}","encrypt":"","encrypted":false}
createProperty()
在当前图集为点schema或边schema创建一个属性。
参数:
DBType
:属性类型(边属性或点属性)。String
:schema名称,可用*
指定全部schema。Property
:待创建的属性;必须设置name
字段和type
字段,description
字段可选。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
// 在图集UltipaTeam中为名为member的点schema创建两个属性并打印错误代码
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
String schema = "member";
Property property1 = new Property();
property1.setName("startDate");
property1.setType("datetime");
Property property2 = new Property();
property2.setName("age");
property2.setType("int32");
Response response1 = client.createProperty(Ultipa.DBType.DBNODE, schema, property1, requestConfig);
System.out.println(response1.getStatus().getErrorCode());
Response response2 = client.createProperty(Ultipa.DBType.DBNODE, schema, property2, requestConfig);
System.out.println(response2.getStatus().getErrorCode());
SUCCESS
SUCCESS
createPropertyIfNotExist()
在当前图集为某个点schema或边schema新建一个属性,若同名属性已存在,忽略报错。
参数:
DBType
:属性类型(点属性或边属性)。String
:schema名称,可用*
指定全部schema。Property
:待创建的属性;必须设置name
字段和type
字段,description
字段可选。RequestConfig
(可选):配置请求。
返回值:
boolean
:创建是否发生。
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
Property property = new Property();
property.setName("startDate");
property.setType("datetime");
// 在图集UltipaTeam中为名为member的点schema新建一个属性并打印创建是否发生
boolean flag1 = client.createPropertyIfNotExist(Ultipa.DBType.DBNODE, "member", property, requestConfig);
System.out.println(flag1);
// 在图集UltipaTeam中再次创建同个属性并打印创建是否发生
Thread.sleep(3000);
boolean flag2 = client.createPropertyIfNotExist(Ultipa.DBType.DBNODE, "member", property, requestConfig);
System.out.println(flag2);
true
false
alterProperty()
根据属性名称修改当前图集中某个自定义属性的名称和描述。
参数:
DBType
:属性类型(点属性或边属性)。Property
:图集中待修改的属性;必须设置name
字段和schema
字段(可用*
指定全部schema)。Property
:更新已有属性的配置;对于name
字段和description
字段,必须至少设置其中一个字段。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
// 将图集UltipaTeam中全部点schema下的name属性改为Name
Property oldProperty = new Property();
oldProperty.setName("name");
oldProperty.setSchema("*");
Property newProperty = new Property();
newProperty.setName("Name");
Response response = client.alterProperty(Ultipa.DBType.DBNODE, oldProperty, newProperty, requestConfig);
System.out.println(response.getStatus().getErrorCode());
SUCCESS
dropProperty()
根据属性名称和关联schema从当前图集删除某自定义属性。
参数:
DBType
:属性类型(点属性或边属性)。String
(可选):schema名称;未设置时默认指定全部schema。String
:属性名称。RequestConfig
(可选):配置请求。
返回值:
Response
:请求的结果。
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraphName("UltipaTeam");
// 删除图集UltipaTeam中与全部点schema关联的startDate属性并打印错误代码
Response response1 = client.dropProperty(Ultipa.DBType.DBNODE, "startDate", requestConfig);
System.out.println(response1.getStatus().getErrorCode());
//删除图集UltipaTeam的点属性@member.name并打印错误代码
Response response2 = client.dropProperty(Ultipa.DBType.DBNODE, "member", "name", requestConfig);
System.out.println(response2.getStatus().getErrorCode());
SUCCESS
SUCCESS
完整示例
package com.ultipa.www.sdk.api;
import com.ultipa.Ultipa;
import com.ultipa.sdk.connect.Connection;
import com.ultipa.sdk.connect.conf.RequestConfig;
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 java.util.ArrayList;
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.setGraphName("UltipaTeam");
// 在图集UltipaTeam中新建名为utility的点schema(同时创建属性)并打印错误代码
Schema schema = new Schema();
schema.setName("utility");
schema.setDescription("Office utilities");
schema.setDbType(Ultipa.DBType.DBNODE);
Property property1 = new Property();
property1.setName("name");
property1.setType("string");
Property property2 = new Property();
property2.setName("purchaseDate");
property2.setType("datetime");
ArrayList<Property> propertyArrayList = new ArrayList<>();
propertyArrayList.add(property1);
propertyArrayList.add(property2);
schema.setProperties(propertyArrayList);
Response response = client.createSchema(schema, true, requestConfig);
System.out.println(response.getStatus().getErrorCode());
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
if (driver != null) {
driver.close();
}
}
}
}