本节介绍用于管理图中schema与属性的方法。
Schema
showSchema()
获取图中全部schema。
参数
config: RequestConfig
(可选):请求配置。
返回值
List<Schema>
:获取的schema列表。
// Retrieves all schemas in the graph 'miniCircle'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("miniCircle");
List<Schema> schemas = driver.showSchema(requestConfig);
for (Schema schema : schemas) {
System.out.println(schema.getName() + ", " + schema.getDbType());
}
default, DBNODE
account, DBNODE
celebrity, DBNODE
country, DBNODE
movie, DBNODE
default, DBEDGE
direct, DBEDGE
disagree, DBEDGE
filmedIn, DBEDGE
follow, DBEDGE
wishlist, DBEDGE
response, DBEDGE
review, DBEDGE
showNodeSchema()
获取图中全部点schema。
参数
config: RequestConfig
(可选):请求配置。
返回值
List<Schema>
:获取的schema列表。
// Retrieves all node schemas in the graph 'miniCircle'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("miniCircle");
List<Schema> schemas = driver.showNodeSchema(requestConfig);
for (Schema schema : schemas) {
System.out.println(schema.getName() + ", " + schema.getDbType());
}
default, DBNODE
account, DBNODE
celebrity, DBNODE
country, DBNODE
movie, DBNODE
showEdgeSchema()
获取图中全部边schema。
参数
config: RequestConfig
(可选):请求配置。
返回值
List<Schema>
:获取的schema列表。
// Retrieves all edge schemas in the graph 'miniCircle'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("miniCircle");
List<Schema> schemas = driver.showEdgeSchema(requestConfig);
for (Schema schema : schemas) {
System.out.println(schema.getName() + ", " + schema.getDbType());
}
default, DBEDGE
direct, DBEDGE
disagree, DBEDGE
filmedIn, DBEDGE
follow, DBEDGE
wishlist, DBEDGE
response, DBEDGE
review, DBEDGE
getSchema()
获取图中一个指定的schema。
参数
schemaName: String
:schema名称。dbType: DBtype
:schema类型(点或边)。config: RequestConfig
(可选):请求配置。
返回值
Schema
:获取的schema。
// Retrieves the node schema named 'account'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("miniCircle");
Schema schema = driver.getSchema("account", Ultipa.DBType.DBNODE, requestConfig);
System.out.println(schema.getTotal());
111
getNodeSchema()
获取图中一个指定的点schema。
参数
schemaName: String
:schema名称。config: RequestConfig
(可选):请求配置。
返回值
Schema
:获取的schema。
// Retrieves the node schema named 'account'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("miniCircle");
Schema schema = driver.getNodeSchema("account", requestConfig);
if (schema != null) {
List<Property> properties = schema.getProperties();
if (properties != null) {
for (Property property : properties) {
System.out.println(property.getName());
}
}
} else {
System.out.println("Not found");
}
gender
year
industry
name
getEdgeSchema()
获取图中一个指定的边schema。
参数
schemaName: String
:schema名称。config: RequestConfig
(可选):请求配置。
返回值
Schema
:获取的schema。
// Retrieves the edge schema named 'disagree'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("miniCircle");
Schema schema = driver.getEdgeSchema("disagree", requestConfig);
if (schema != null) {
List<Property> properties = schema.getProperties();
if (properties != null) {
for (Property property : properties) {
System.out.println(property.getName());
}
}
} else {
System.out.println("Not found");
}
datetime
timestamp
targetPost
createSchema()
在图中创建一个schema。
参数
schema: Schema
:待创建的schema;name
和dbType
属性必填,description
和properties
可选。isCreateProperties: boolean
(可选):是否创建属性,默认为false
。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("miniCircle");
// Creates node schema 'utility' (with properties)
Schema utility = new Schema();
utility.setName("utility");
utility.setDbType(Ultipa.DBType.DBNODE);
utility.setProperties(new ArrayList<Property>() {{
add(new Property() {{
setName("name");
setType(Ultipa.PropertyType.STRING);
}});
add(new Property() {{
setName("type");
setType(Ultipa.PropertyType.UINT32);
}});
}});
Response response1 = driver.createSchema(utility, true, requestConfig);
System.out.println(response1.getStatus().getCode());
Thread.sleep(3000);
// Creates edge schema 'vote' (without properties)
Schema vote = new Schema();
vote.setName("vote");
vote.setDbType(Ultipa.DBType.DBEDGE);
Response response2 = driver.createSchema(vote, false, requestConfig);
System.out.println(response2.getStatus().getCode());
SUCCESS
SUCCESS
createSchemaIfNotExist()
在图中创建一个schema,并返回是否图中已有同名点或边schema存在。
参数
schema: Schema
:待创建的schema;name
和dbType
属性必填,description
和properties
可选。isCreateProperties: boolean
(可选):是否创建属性,默认为false
。config: RequestConfig
(可选):请求配置。
返回值
ResponseWithExistCheck
:请求结果。
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("miniCircle");
Schema schema = new Schema();
schema.setName("utility");
schema.setDbType(Ultipa.DBType.DBNODE);
schema.setProperties(new ArrayList<Property>() {{
add(new Property() {{
setName("name");
setType(Ultipa.PropertyType.STRING);
}});
add(new Property() {{
setName("type");
setType(Ultipa.PropertyType.UINT32);
}});
}});
ResponseWithExistCheck result = driver.createSchemaIfNotExist(schema, true, requestConfig);
System.out.println("Does the schema already exist? " + result.getExist());
if(result.getResponse() == null) {
System.out.println("Schema creation status: No response");
} else {
System.out.println("Schema creation status: " + result.getResponse().getStatus().getCode());
}
System.out.println("----- Creates the schema again -----");
ResponseWithExistCheck result_1 = driver.createSchemaIfNotExist(schema, true, requestConfig);
System.out.println("Does the schema already exist? " + result_1.getExist());
if(result_1.getResponse() == null) {
System.out.println("Schema creation status: No response");
} else {
System.out.println("Schema creation status: " + result_1.getResponse().getStatus().getCode());
}
Does the schema already exist? false
----- Creates the schema again -----
Does the schema already exist? true
Schema creation status: No response
alterSchema()
修改图中一个schema的名称和描述。
参数
originalSchema: Schema
:待修改的schema;name
和dbType
属性必填。newSchema:Schema
:用于设置新的schemaname
和/或description
的Schema
对象。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Renames the node schema 'utility' to 'securityUtility' in the graph 'miniCircle'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("miniCircle");
Schema oldSchema = new Schema();
oldSchema.setName("utility");
oldSchema.setDbType(Ultipa.DBType.DBNODE);
Schema newSchema = new Schema();
newSchema.setName("securityUtility");
Response response = driver.alterSchema(oldSchema, newSchema, requestConfig);
System.out.println(response.getStatus().getCode());
SUCCESS
dropSchema()
从图中删除一个指定的schema。
参数
schema: Schema
:待删除的schema;name
和dbType
属性必填。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Drops the edge schema 'vote' from the graph 'miniCircle'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("miniCircle");
Schema schema = new Schema();
schema.setName("vote");
schema.setDbType(Ultipa.DBType.DBEDGE);
Response response = driver.dropSchema(schema, requestConfig);
System.out.println(response.getStatus().getCode());
SUCCESS
Property
showProperty()
获取图中全部属性。
参数
dbType: DBType
(可选):属性类型(点或边)。schemaName: String
(可选):schema名称。config: RequestConfig
(可选):请求配置。
返回值
AllProperties
:一个包含了两个Property
对象列表的类,分别为nodeProperties
和edgeProperties
。
// Retrieves all properties in the graph 'citation'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("citation");
AllProperties properties = driver.showProperty(requestConfig);
List<Property> nodeProperties = properties.getNodeProperties();
System.out.println("Node Properties:");
for (Property property : nodeProperties) {
System.out.println(property.getName() + " is associated with schema " + property.getSchema());
}
List<Property> edgeProperties = properties.getEdgeProperties();
System.out.println("Edge Properties:");
for (Property property : edgeProperties) {
System.out.println(property.getName() + " is associated with schema " + property.getSchema());
}
Node Properties:
_id is associated with schema default
_id is associated with schema Paper
title is associated with schema Paper
score is associated with schema Paper
author is associated with schema Paper
Edge Properties:
weight is associated with schema Cites
showNodeProperty()
获取图中全部点属性。
参数
schemaName: String
(可选):schema名称。config: RequestConfig
(可选):请求配置。
返回值
List<Property>
:获取的属性列表。
// Retrieves properties associated with the node schema 'Paper' in the graph 'citation'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("citation");
List<Property> properties = driver.showNodeProperty("Paper", requestConfig);
for (Property property : properties) {
System.out.println(property.getName() + " - " + property.getType());
}
_id - STRING
title - STRING
score - INT32
author - STRING
showEdgeProperty()
获取图中全部边属性。
参数
schemaName: String
(可选):schema名称。config: RequestConfig
(可选):请求配置。
返回值
List<Property>
:获取的属性列表。
// Retrieves properties associated with the edge schema 'Cites' in the graph 'citation'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("citation");
List<Property> properties = driver.showEdgeProperty("Cites", requestConfig);
for (Property property : properties) {
System.out.println(property.getName() + " - " + property.getType());
}
weight - INT32
getProperty()
获取图中一个指定的属性。
参数
dbType: DBType
:属性类型(点或边)。schemaName: String
:Schema名称。propertyName: String
:属性名称。config: RequestConfig
(可选):请求配置。
返回值
Property
:获取的属性。
// Retrieves node property 'title' associated with the node schema 'Paper' in the graph 'citation'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("citation");
Property property = driver.getProperty(Ultipa.DBType.DBNODE, "Paper", "title", requestConfig);
System.out.println(property);
Property(name=title, type=STRING, subType=null, typeString=string, lte=false, read=true, write=true, schema=Paper, description=, ignored=false, extra=null, encrypt=, encrypted=false)
getNodeProperty()
获取图中一个指定的点属性。
参数
schemaName: String
:Schema名称。propertyName: String
:属性名称。config: RequestConfig
(可选):请求配置。
返回值
Property
:获取的属性。
// Retrieves node property 'title' associated with the node schema 'Paper' in the graph 'citation'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("citation");
Property property = driver.getNodeProperty("Paper", "title", requestConfig);
System.out.println(property);
Property(name=title, type=STRING, subType=null, typeString=string, lte=false, read=true, write=true, schema=Paper, description=, ignored=false, extra=null, encrypt=, encrypted=false)
getEdgeProperty()
获取图中一个指定的边属性。
参数
schemaName: String
:Schema名称。propertyName: String
:属性名称。config: RequestConfig
(可选):请求配置。
返回值
Property
:获取的属性。
// Retrieves edge property 'weight' associated with the edge schema 'Cites' in the graph 'citation'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("citation");
Property property = driver.getEdgeProperty("Cites", "weight", requestConfig);
System.out.println(property);
Property(name=weight, type=INT32, subType=null, typeString=int32, lte=false, read=true, write=true, schema=Cites, description=, ignored=false, extra=null, encrypt=, encrypted=false)
createProperty()
在图中创建一个属性。
参数
dbType: DBType
:属性类型(点或边)。property: Property
:待创建的属性;name
、type
(以及subType
,如果type
为SET
或LIST
)和schema
(用*
指定全部schema)属性必填,encrypt
和description
可选。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Creates a property 'year' for all node schemas, creates a property 'tags' for the node schema 'Paper'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("citation");
Property property1 = new Property();
property1.setName("year");
property1.setType(Ultipa.PropertyType.UINT32);
property1.setEncrypt("AES128");
property1.setSchema("*");
Property property2 = new Property();
property2.setName("tags");
property2.setType(Ultipa.PropertyType.STRING);
property2.setEncrypt("AES128");
property2.setSchema("*");
Response response1 = driver.createProperty(Ultipa.DBType.DBNODE, property1, requestConfig);
System.out.println(response1.getStatus().getCode());
Response response2 = driver.createProperty(Ultipa.DBType.DBNODE, property2, requestConfig);
System.out.println(response2.getStatus().getCode());
SUCCESS
SUCCESS
createPropertyIfNotExist()
在图中创建一个属性,并返回是否图中指定点或边schema下已有同名属性存在。
参数
dbType: DBType
:属性类型(点或边)。property: Property
:待创建的属性;name
、type
(以及subType
,如果type
为SET
或LIST
)和schema
(用*
指定全部schema)属性必填,encrypt
和description
可选。config: RequestConfig
(可选):请求配置。
返回值
ResponseWithExistCheck
:请求结果。
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("citation");
Property property = new Property();
property.setName("tags");
property.setType(Ultipa.PropertyType.SET);
property.setSubType(Lists.newArrayList(Ultipa.PropertyType.STRING));
property.setEncrypt("AES128");
property.setSchema("Paper");
ResponseWithExistCheck result = driver.createPropertyIfNotExist(Ultipa.DBType.DBNODE, property, requestConfig);
System.out.println("Does the property already exist? " + result.getExist());
if(result.getResponse() == null) {
System.out.println("Property creation status: No response");
} else {
System.out.println("Property creation status: " + result.getResponse().getStatus().getCode());
}
System.out.println("----- Creates the property again -----");
ResponseWithExistCheck result_1 = driver.createPropertyIfNotExist(Ultipa.DBType.DBNODE, property, requestConfig);
System.out.println("Does the property already exist? " + result_1.getExist());
if(result_1.getResponse() == null) {
System.out.println("Property creation status: No response");
} else {
System.out.println("Property creation status: " + result_1.getResponse().getStatus().getCode());
}
Does the property already exist? false
Property creation status: SUCCESS
----- Creates the property again -----
Does the property already exist? true
Property creation status: No response
alterProperty()
修改图中指定属性的名称和描述。
参数
dbType: DBType
:属性类型(点或边)。originProp: Property
:待修改的属性;name
和schema
属性必填(用*
可指定全部schema)。newProp: Property
:用于设置新的属性name
和/或description
的Property
对象。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Renames the property 'tags' of the node schema 'Paper' to 'keywords' in the graph 'citation'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("citation");
Property oldProperty = new Property();
oldProperty.setName("tags");
oldProperty.setSchema("Paper");
Property newProperty = new Property();
newProperty.setName("keywords");
Response response = driver.alterProperty(Ultipa.DBType.DBNODE, oldProperty, newProperty, requestConfig);
System.out.println(response.getStatus().getCode());
SUCCESS
dropProperty()
从图中删除指定的属性。
参数
dbType: DBType
:属性类型(点或边)。property: Property
:待删除的属性;name
和schema
属性必填(用*
可指定全部schema)。config: RequestConfig
(可选):请求配置。
返回值
Response
:请求结果。
// Drops the property 'tags' of the node schema in the graph 'citation'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("citation");
Property property = new Property();
property.setName("tags");
property.setSchema("Paper");
Response response = driver.dropProperty(Ultipa.DBType.DBNODE, property, requestConfig);
System.out.println(response.getStatus().getCode());
SUCCESS
完整示例
package com.ultipa.www.sdk.api;
import com.ultipa.Ultipa;
import com.ultipa.sdk.UltipaDriver;
import com.ultipa.sdk.connect.conf.RequestConfig;
import com.ultipa.sdk.connect.conf.UltipaConfig;
import com.ultipa.sdk.operate.entity.*;
import com.ultipa.sdk.operate.response.Response;
import org.assertj.core.util.Lists;
import java.util.ArrayList;
import java.util.List;
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 schemas and properties in the graph 'social'
RequestConfig requestConfig = new RequestConfig();
requestConfig.setGraph("social");
Schema user = new Schema();
user.setName("user");
user.setDbType(Ultipa.DBType.DBNODE);
user.setProperties(new ArrayList<Property>() {{
add(new Property() {{
setName("name");
setType(Ultipa.PropertyType.STRING);
}});
add(new Property() {{
setName("age");
setType(Ultipa.PropertyType.INT32);
}});
add(new Property() {{
setName("score");
setType(Ultipa.PropertyType.DECIMAL);
setDecimalExtra(25, 10);
}});
add(new Property() {{
setName("birthday");
setType(Ultipa.PropertyType.DATETIME);
}});
add(new Property() {{
setName("active");
setType(Ultipa.PropertyType.BOOL);
}});
add(new Property() {{
setName("location");
setType(Ultipa.PropertyType.POINT);
}});
add(new Property() {{
setName("profile");
setType(Ultipa.PropertyType.BLOB);
}});
add(new Property() {{
setName("interests");
setType(Ultipa.PropertyType.LIST);
setSubType(Lists.newArrayList(Ultipa.PropertyType.STRING));
}});
add(new Property() {{
setName("permissionCodes");
setType(Ultipa.PropertyType.SET);
setSubType(Lists.newArrayList(Ultipa.PropertyType.INT32));
}});
}});
Schema product = new Schema();
product.setName("product");
product.setDbType(Ultipa.DBType.DBNODE);
product.setProperties(new ArrayList<Property>() {{
add(new Property() {{
setName("name");
setType(Ultipa.PropertyType.STRING);
}});
add(new Property() {{
setName("price");
setType(Ultipa.PropertyType.FLOAT);
}});
}});
Schema follows = new Schema();
follows.setName("follows");
follows.setDbType(Ultipa.DBType.DBEDGE);
follows.setProperties(new ArrayList<Property>() {{
add(new Property() {{
setName("createdOn");
setType(Ultipa.PropertyType.TIMESTAMP);
}});
add(new Property() {{
setName("weight");
setType(Ultipa.PropertyType.FLOAT);
}});
}});
Schema purchased = new Schema();
purchased.setName("purchased");
purchased.setDbType(Ultipa.DBType.DBEDGE);
List<Schema> schemas = Lists.newArrayList(user, product, follows, purchased);
for (Schema schema : schemas) {
Response response = driver.createSchema(schema, true, requestConfig);
System.out.println(response.getStatus().getCode());
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
if (driver != null) {
driver.close();
}
}
}
}