HugeGraph-Client Quick Start

1 HugeGraph-Client 概述

HugeGraph-Client 向 HugeGraph-Server 发出 HTTP 请求,获取并解析 Server 的执行结果。目前仅提供了 Java 版,用户可以使用 HugeGraph-Client 编写 Java 代码操作 HugeGraph,比如元数据和图数据的增删改查,或者执行 gremlin 语句。

2 环境要求

  • java 11 (兼容 java 8)
  • maven 3.5+

3 使用流程

使用 HugeGraph-Client 的基本步骤如下:

  • 新建Eclipse/ IDEA Maven 项目;
  • 在 pom 文件中添加 HugeGraph-Client 依赖;
  • 创建类,调用 HugeGraph-Client 接口;

详细使用过程见下节完整示例。

4 完整示例

4.1 新建 Maven 工程

可以选择 Eclipse 或者 Intellij Idea 创建工程:

4.2 添加 hugegraph-client 依赖

添加 hugegraph-client 依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.hugegraph</groupId>
  4. <artifactId>hugegraph-client</artifactId>
  5. <version>${version}</version>
  6. </dependency>
  7. </dependencies>

4.3 Example

4.3.1 SingleExample
  1. import java.io.IOException;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import org.apache.hugegraph.driver.GraphManager;
  5. import org.apache.hugegraph.driver.GremlinManager;
  6. import org.apache.hugegraph.driver.HugeClient;
  7. import org.apache.hugegraph.driver.SchemaManager;
  8. import org.apache.hugegraph.structure.constant.T;
  9. import org.apache.hugegraph.structure.graph.Edge;
  10. import org.apache.hugegraph.structure.graph.Path;
  11. import org.apache.hugegraph.structure.graph.Vertex;
  12. import org.apache.hugegraph.structure.gremlin.Result;
  13. import org.apache.hugegraph.structure.gremlin.ResultSet;
  14. public class SingleExample {
  15. public static void main(String[] args) throws IOException {
  16. // If connect failed will throw a exception.
  17. HugeClient hugeClient = HugeClient.builder("http://localhost:8080",
  18. "hugegraph")
  19. .build();
  20. SchemaManager schema = hugeClient.schema();
  21. schema.propertyKey("name").asText().ifNotExist().create();
  22. schema.propertyKey("age").asInt().ifNotExist().create();
  23. schema.propertyKey("city").asText().ifNotExist().create();
  24. schema.propertyKey("weight").asDouble().ifNotExist().create();
  25. schema.propertyKey("lang").asText().ifNotExist().create();
  26. schema.propertyKey("date").asDate().ifNotExist().create();
  27. schema.propertyKey("price").asInt().ifNotExist().create();
  28. schema.vertexLabel("person")
  29. .properties("name", "age", "city")
  30. .primaryKeys("name")
  31. .ifNotExist()
  32. .create();
  33. schema.vertexLabel("software")
  34. .properties("name", "lang", "price")
  35. .primaryKeys("name")
  36. .ifNotExist()
  37. .create();
  38. schema.indexLabel("personByCity")
  39. .onV("person")
  40. .by("city")
  41. .secondary()
  42. .ifNotExist()
  43. .create();
  44. schema.indexLabel("personByAgeAndCity")
  45. .onV("person")
  46. .by("age", "city")
  47. .secondary()
  48. .ifNotExist()
  49. .create();
  50. schema.indexLabel("softwareByPrice")
  51. .onV("software")
  52. .by("price")
  53. .range()
  54. .ifNotExist()
  55. .create();
  56. schema.edgeLabel("knows")
  57. .sourceLabel("person")
  58. .targetLabel("person")
  59. .properties("date", "weight")
  60. .ifNotExist()
  61. .create();
  62. schema.edgeLabel("created")
  63. .sourceLabel("person").targetLabel("software")
  64. .properties("date", "weight")
  65. .ifNotExist()
  66. .create();
  67. schema.indexLabel("createdByDate")
  68. .onE("created")
  69. .by("date")
  70. .secondary()
  71. .ifNotExist()
  72. .create();
  73. schema.indexLabel("createdByWeight")
  74. .onE("created")
  75. .by("weight")
  76. .range()
  77. .ifNotExist()
  78. .create();
  79. schema.indexLabel("knowsByWeight")
  80. .onE("knows")
  81. .by("weight")
  82. .range()
  83. .ifNotExist()
  84. .create();
  85. GraphManager graph = hugeClient.graph();
  86. Vertex marko = graph.addVertex(T.LABEL, "person", "name", "marko",
  87. "age", 29, "city", "Beijing");
  88. Vertex vadas = graph.addVertex(T.LABEL, "person", "name", "vadas",
  89. "age", 27, "city", "Hongkong");
  90. Vertex lop = graph.addVertex(T.LABEL, "software", "name", "lop",
  91. "lang", "java", "price", 328);
  92. Vertex josh = graph.addVertex(T.LABEL, "person", "name", "josh",
  93. "age", 32, "city", "Beijing");
  94. Vertex ripple = graph.addVertex(T.LABEL, "software", "name", "ripple",
  95. "lang", "java", "price", 199);
  96. Vertex peter = graph.addVertex(T.LABEL, "person", "name", "peter",
  97. "age", 35, "city", "Shanghai");
  98. marko.addEdge("knows", vadas, "date", "2016-01-10", "weight", 0.5);
  99. marko.addEdge("knows", josh, "date", "2013-02-20", "weight", 1.0);
  100. marko.addEdge("created", lop, "date", "2017-12-10", "weight", 0.4);
  101. josh.addEdge("created", lop, "date", "2009-11-11", "weight", 0.4);
  102. josh.addEdge("created", ripple, "date", "2017-12-10", "weight", 1.0);
  103. peter.addEdge("created", lop, "date", "2017-03-24", "weight", 0.2);
  104. GremlinManager gremlin = hugeClient.gremlin();
  105. System.out.println("==== Path ====");
  106. ResultSet resultSet = gremlin.gremlin("g.V().outE().path()").execute();
  107. Iterator<Result> results = resultSet.iterator();
  108. results.forEachRemaining(result -> {
  109. System.out.println(result.getObject().getClass());
  110. Object object = result.getObject();
  111. if (object instanceof Vertex) {
  112. System.out.println(((Vertex) object).id());
  113. } else if (object instanceof Edge) {
  114. System.out.println(((Edge) object).id());
  115. } else if (object instanceof Path) {
  116. List<Object> elements = ((Path) object).objects();
  117. elements.forEach(element -> {
  118. System.out.println(element.getClass());
  119. System.out.println(element);
  120. });
  121. } else {
  122. System.out.println(object);
  123. }
  124. });
  125. hugeClient.close();
  126. }
  127. }
4.3.2 BatchExample
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import org.apache.hugegraph.driver.GraphManager;
  4. import org.apache.hugegraph.driver.HugeClient;
  5. import org.apache.hugegraph.driver.SchemaManager;
  6. import org.apache.hugegraph.structure.graph.Edge;
  7. import org.apache.hugegraph.structure.graph.Vertex;
  8. public class BatchExample {
  9. public static void main(String[] args) {
  10. // If connect failed will throw a exception.
  11. HugeClient hugeClient = HugeClient.builder("http://localhost:8080",
  12. "hugegraph")
  13. .build();
  14. SchemaManager schema = hugeClient.schema();
  15. schema.propertyKey("name").asText().ifNotExist().create();
  16. schema.propertyKey("age").asInt().ifNotExist().create();
  17. schema.propertyKey("lang").asText().ifNotExist().create();
  18. schema.propertyKey("date").asDate().ifNotExist().create();
  19. schema.propertyKey("price").asInt().ifNotExist().create();
  20. schema.vertexLabel("person")
  21. .properties("name", "age")
  22. .primaryKeys("name")
  23. .ifNotExist()
  24. .create();
  25. schema.vertexLabel("person")
  26. .properties("price")
  27. .nullableKeys("price")
  28. .append();
  29. schema.vertexLabel("software")
  30. .properties("name", "lang", "price")
  31. .primaryKeys("name")
  32. .ifNotExist()
  33. .create();
  34. schema.indexLabel("softwareByPrice")
  35. .onV("software").by("price")
  36. .range()
  37. .ifNotExist()
  38. .create();
  39. schema.edgeLabel("knows")
  40. .link("person", "person")
  41. .properties("date")
  42. .ifNotExist()
  43. .create();
  44. schema.edgeLabel("created")
  45. .link("person", "software")
  46. .properties("date")
  47. .ifNotExist()
  48. .create();
  49. schema.indexLabel("createdByDate")
  50. .onE("created").by("date")
  51. .secondary()
  52. .ifNotExist()
  53. .create();
  54. // get schema object by name
  55. System.out.println(schema.getPropertyKey("name"));
  56. System.out.println(schema.getVertexLabel("person"));
  57. System.out.println(schema.getEdgeLabel("knows"));
  58. System.out.println(schema.getIndexLabel("createdByDate"));
  59. // list all schema objects
  60. System.out.println(schema.getPropertyKeys());
  61. System.out.println(schema.getVertexLabels());
  62. System.out.println(schema.getEdgeLabels());
  63. System.out.println(schema.getIndexLabels());
  64. GraphManager graph = hugeClient.graph();
  65. Vertex marko = new Vertex("person").property("name", "marko")
  66. .property("age", 29);
  67. Vertex vadas = new Vertex("person").property("name", "vadas")
  68. .property("age", 27);
  69. Vertex lop = new Vertex("software").property("name", "lop")
  70. .property("lang", "java")
  71. .property("price", 328);
  72. Vertex josh = new Vertex("person").property("name", "josh")
  73. .property("age", 32);
  74. Vertex ripple = new Vertex("software").property("name", "ripple")
  75. .property("lang", "java")
  76. .property("price", 199);
  77. Vertex peter = new Vertex("person").property("name", "peter")
  78. .property("age", 35);
  79. Edge markoKnowsVadas = new Edge("knows").source(marko).target(vadas)
  80. .property("date", "2016-01-10");
  81. Edge markoKnowsJosh = new Edge("knows").source(marko).target(josh)
  82. .property("date", "2013-02-20");
  83. Edge markoCreateLop = new Edge("created").source(marko).target(lop)
  84. .property("date",
  85. "2017-12-10");
  86. Edge joshCreateRipple = new Edge("created").source(josh).target(ripple)
  87. .property("date",
  88. "2017-12-10");
  89. Edge joshCreateLop = new Edge("created").source(josh).target(lop)
  90. .property("date", "2009-11-11");
  91. Edge peterCreateLop = new Edge("created").source(peter).target(lop)
  92. .property("date",
  93. "2017-03-24");
  94. List<Vertex> vertices = new ArrayList<>();
  95. vertices.add(marko);
  96. vertices.add(vadas);
  97. vertices.add(lop);
  98. vertices.add(josh);
  99. vertices.add(ripple);
  100. vertices.add(peter);
  101. List<Edge> edges = new ArrayList<>();
  102. edges.add(markoKnowsVadas);
  103. edges.add(markoKnowsJosh);
  104. edges.add(markoCreateLop);
  105. edges.add(joshCreateRipple);
  106. edges.add(joshCreateLop);
  107. edges.add(peterCreateLop);
  108. vertices = graph.addVertices(vertices);
  109. vertices.forEach(vertex -> System.out.println(vertex));
  110. edges = graph.addEdges(edges, false);
  111. edges.forEach(edge -> System.out.println(edge));
  112. hugeClient.close();
  113. }
  114. }

4.4 运行 Example

运行 Example 之前需要启动 Server, 启动过程见HugeGraph-Server Quick Start

4.5 Example 示例说明

示例说明见HugeGraph-Client 基本 API 介绍

Last modified October 9, 2023: doc: enhance a string of problems to improve UE (#288) (69c20091)