全部产品
云市场

图模型管理

更新时间:2019-08-30 18:10:39

通过SDK操作图模型的步骤:

1、初始化SchemaClient

  1. SchemaClient schemaClient = maxGraph.schemaClient();
  2. SchemaClient schemaClient = maxGraph.schemaClient();

2、构造SchemaBuilder(可以跳过)

SDK支持两种方式来构建SchemaBuilder

  • 手动构建TypeDef,new SchemaBuilder
  • 通过json文件构造SchemaBuilder

代码示例如下:

  1. //手动构建TypeDef
  2. String schemaJson = IOUtils.toString(Thread.currentThread().getContextClassLoader().getResource("schema/" + graphName + ".schema"), "utf-8");
  3. //json文件
  4. SchemaBuilder schemaBuilder = SchemaBuilder.fromMemSchema(JSON.fromJson(schemaJson, MemSchema.class));

3、调用SchemaClient提供的接口操作Schema

接口 说明 备注
createVertexType 新增点
createEdgeType 新增边
updateVertexType 修改点
updateEdgeType 修改边
dropType 删除点/边 删除点/边之前要先把关系删了
addRelationShip 增加边上的关系
dropRelationShip 删除边上的关系
addTypeProperties 增加属性
updateTypeProperties 修改属性
dropTypeProperties 删除属性
addTypeIndex 增加索引 目前只支持主键
dropTypeIndex 删除索引 目前只支持主键
getSchema 查询schema

图模型操作示例:

  1. public class SchemaDefineExample extends Example{
  2. private final static boolean CLEAN_BEFORE_CREATE = true;
  3. public static void main(String[] args) {
  4. MaxGraph maxGraph = null;
  5. try {
  6. //Create MaxGraph Connection
  7. maxGraph = MaxGraph.newConnector(graphName, endpoint).setUserName(username)
  8. .setPassword(password).connect();
  9. //SchemaClient
  10. SchemaClient schemaClient = maxGraph.schemaClient();
  11. //clean exists schema
  12. if(CLEAN_BEFORE_CREATE){
  13. MemSchema preCommitSchema = schemaClient.getSchema();
  14. SchemaBuilder schemaBuilder = SchemaBuilder.fromMemSchema(preCommitSchema);
  15. //drop relation
  16. for (TypeDef typeDef : schemaBuilder.getTypeDefList()) {
  17. if (typeDef.type() == Type.EDGE && !CollectionUtils.isEmpty(typeDef.getRawRelationShips())) {
  18. for (RawRelationShip rawRelationShip : typeDef.getRawRelationShips()) {
  19. schemaClient.dropRelationShip(rawRelationShip.edgeLabel,rawRelationShip.srcVertexLabel,rawRelationShip.dstVertexLabel);
  20. }
  21. }
  22. }
  23. //drop type
  24. for (TypeDef typeDef : schemaBuilder.getTypeDefList()) {
  25. schemaClient.dropType(typeDef.label());
  26. }
  27. }
  28. //create schema builder
  29. SchemaBuilder schemaBuilder = createSchemaBuilder();
  30. for (TypeDef typeDef : schemaBuilder.getTypeDefList()) {
  31. if (typeDef.type() == TypeIdProto.Type.VERTEX) {
  32. //create vertex
  33. schemaClient.createVertexType(typeDef.label(),
  34. typeDef.getPropertyDefList(), Lists.newArrayList(typeDef.getIndexMap().values()), typeDef
  35. .getComment(), typeDef.isDimensionType(), new TypeOption(StorageEngine.MEMORY));
  36. } else {
  37. //create edge
  38. Set<RawRelationShip> rawRelationShipSet = new HashSet<>(typeDef.getRawRelationShips());
  39. schemaClient.createEdgeType(typeDef.label(), typeDef.getPropertyDefList(), typeDef.getComment(),
  40. rawRelationShipSet, typeDef.isDimensionType(), new TypeOption(StorageEngine.ROCKSDB), null);
  41. }
  42. }
  43. }catch (Exception ex) {
  44. ex.printStackTrace();
  45. }finally {
  46. //close connection
  47. if(maxGraph != null){
  48. maxGraph.close();
  49. }
  50. }
  51. }
  52. public static SchemaBuilder createSchemaBuilder() {
  53. //define property
  54. PropertyDef _domainName = new PropertyDef("domain_name", DataType.STRING, "domain_name");
  55. PropertyDef _ip = new PropertyDef("ip", DataType.STRING, "ip");
  56. PropertyDef _nsName = new PropertyDef("ns_name", DataType.STRING, "ns_name");
  57. PropertyDef _internalIp = new PropertyDef("internal_ip", DataType.STRING, "internal_ip");
  58. PropertyDef _internetIp = new PropertyDef("internet_ip", DataType.STRING, "internet_ip");
  59. PropertyDef _instanceId = new PropertyDef("instance_id", DataType.STRING, "instance_id");
  60. PropertyDef _userId = new PropertyDef("user_id", DataType.STRING, "user_id");
  61. PropertyDef _regionId = new PropertyDef("region_id", DataType.STRING, "region_id");
  62. PropertyDef _feedType = new PropertyDef("feed_type", DataType.STRING, "feed_type");
  63. PropertyDef _feedName = new PropertyDef("feed_name", DataType.STRING, "feed_name");
  64. PropertyDef _queryTime = new PropertyDef("query_time", DataType.STRING, "query_time");
  65. //define vertex
  66. IndexDef idPrimaryKey = IndexDef.createPrimaryKey(Lists.newArrayList("domain_name"));
  67. TypeDef domain = new TypeDef("domain", TypeIdProto.Type.VERTEX,
  68. false, Lists.newArrayList(_domainName), Lists.newArrayList(idPrimaryKey), "domain", 0);
  69. idPrimaryKey = IndexDef.createPrimaryKey(Lists.newArrayList("ip"));
  70. TypeDef ip = new TypeDef("ip", TypeIdProto.Type.VERTEX,
  71. false, Lists.newArrayList(_ip), Lists.newArrayList(idPrimaryKey), "ip", 0);
  72. idPrimaryKey = IndexDef.createPrimaryKey(Lists.newArrayList("ns_name"));
  73. TypeDef nameServer = new TypeDef("nameserver", TypeIdProto.Type.VERTEX,
  74. false, Lists.newArrayList(_nsName), Lists.newArrayList(idPrimaryKey), "nameserver", 0);
  75. idPrimaryKey = IndexDef.createPrimaryKey(Lists.newArrayList("instance_id"));
  76. TypeDef ecs = new TypeDef("ecs", TypeIdProto.Type.VERTEX,
  77. false, Lists.newArrayList(_internalIp,_internetIp,_instanceId,_userId,_regionId), Lists.newArrayList(idPrimaryKey), "ecs", 0);
  78. idPrimaryKey = IndexDef.createPrimaryKey(Lists.newArrayList("feed_name"));
  79. TypeDef feed = new TypeDef("feed", TypeIdProto.Type.VERTEX,
  80. false, Lists.newArrayList(_feedType,_feedName), Lists.newArrayList(idPrimaryKey), "feed", 0);
  81. //define edge
  82. TypeDef resolvedTo = new TypeDef("resolved_to", TypeIdProto.Type.EDGE, false, Lists.newArrayList(_queryTime), Lists.newArrayList(), "resolved_to", 0);
  83. resolvedTo.addRawRelationShip(new RawRelationShip("domain", "ip","resolved_to"));
  84. TypeDef nsTo = new TypeDef("ns_to", TypeIdProto.Type.EDGE, false, Lists.newArrayList(), Lists.newArrayList(), "ns_to", 0);
  85. nsTo.addRawRelationShip(new RawRelationShip("domain", "nameserver", "ns_to"));
  86. TypeDef cnameTo = new TypeDef("cname_to", TypeIdProto.Type.EDGE, false, Lists.newArrayList(), Lists.newArrayList(), "cname_to", 0);
  87. cnameTo.addRawRelationShip(new RawRelationShip("domain", "domain", "cname_to"));
  88. TypeDef listedIn = new TypeDef("listed_in", TypeIdProto.Type.EDGE, false, Lists.newArrayList(), Lists.newArrayList(), "listed_in", 0);
  89. listedIn.addRawRelationShip(new RawRelationShip("feed", "domain", "listed_in"));
  90. TypeDef query = new TypeDef("query", TypeIdProto.Type.EDGE, false, Lists.newArrayList(_queryTime), Lists.newArrayList(), "query", 0);
  91. query.addRawRelationShip(new RawRelationShip("ecs", "domain", "query"));
  92. return new SchemaBuilder(Lists.newArrayList(domain, ip, nameServer, ecs, feed, resolvedTo, nsTo, cnameTo, listedIn, query ), 64);
  93. }
  94. }