HugeGraph-Client Quick Start

1 概述

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

2 环境要求

  • jdk1.8
  • maven-3.3.9

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>com.baidu.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 com.baidu.hugegraph.driver.GraphManager;
  5. import com.baidu.hugegraph.driver.GremlinManager;
  6. import com.baidu.hugegraph.driver.HugeClient;
  7. import com.baidu.hugegraph.driver.SchemaManager;
  8. import com.baidu.hugegraph.structure.constant.T;
  9. import com.baidu.hugegraph.structure.graph.Edge;
  10. import com.baidu.hugegraph.structure.graph.Path;
  11. import com.baidu.hugegraph.structure.graph.Vertex;
  12. import com.baidu.hugegraph.structure.gremlin.Result;
  13. import com.baidu.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 = new HugeClient("http://localhost:8080", "hugegraph");
  18. SchemaManager schema = hugeClient.schema();
  19. schema.propertyKey("name").asText().ifNotExist().create();
  20. schema.propertyKey("age").asInt().ifNotExist().create();
  21. schema.propertyKey("city").asText().ifNotExist().create();
  22. schema.propertyKey("weight").asDouble().ifNotExist().create();
  23. schema.propertyKey("lang").asText().ifNotExist().create();
  24. schema.propertyKey("date").asText().ifNotExist().create();
  25. schema.propertyKey("price").asInt().ifNotExist().create();
  26. schema.vertexLabel("person")
  27. .properties("name", "age", "city")
  28. .primaryKeys("name")
  29. .ifNotExist()
  30. .create();
  31. schema.vertexLabel("software")
  32. .properties("name", "lang", "price")
  33. .primaryKeys("name")
  34. .ifNotExist()
  35. .create();
  36. schema.indexLabel("personByCity")
  37. .onV("person")
  38. .by("city")
  39. .secondary()
  40. .ifNotExist()
  41. .create();
  42. schema.indexLabel("personByAgeAndCity")
  43. .onV("person")
  44. .by("age", "city")
  45. .secondary()
  46. .ifNotExist()
  47. .create();
  48. schema.indexLabel("softwareByPrice")
  49. .onV("software")
  50. .by("price")
  51. .range()
  52. .ifNotExist()
  53. .create();
  54. schema.edgeLabel("knows")
  55. .sourceLabel("person")
  56. .targetLabel("person")
  57. .properties("date", "weight")
  58. .ifNotExist()
  59. .create();
  60. schema.edgeLabel("created")
  61. .sourceLabel("person").targetLabel("software")
  62. .properties("date", "weight")
  63. .ifNotExist()
  64. .create();
  65. schema.indexLabel("createdByDate")
  66. .onE("created")
  67. .by("date")
  68. .secondary()
  69. .ifNotExist()
  70. .create();
  71. schema.indexLabel("createdByWeight")
  72. .onE("created")
  73. .by("weight")
  74. .range()
  75. .ifNotExist()
  76. .create();
  77. schema.indexLabel("knowsByWeight")
  78. .onE("knows")
  79. .by("weight")
  80. .range()
  81. .ifNotExist()
  82. .create();
  83. GraphManager graph = hugeClient.graph();
  84. Vertex marko = graph.addVertex(T.label, "person", "name", "marko",
  85. "age", 29, "city", "Beijing");
  86. Vertex vadas = graph.addVertex(T.label, "person", "name", "vadas",
  87. "age", 27, "city", "Hongkong");
  88. Vertex lop = graph.addVertex(T.label, "software", "name", "lop",
  89. "lang", "java", "price", 328);
  90. Vertex josh = graph.addVertex(T.label, "person", "name", "josh",
  91. "age", 32, "city", "Beijing");
  92. Vertex ripple = graph.addVertex(T.label, "software", "name", "ripple",
  93. "lang", "java", "price", 199);
  94. Vertex peter = graph.addVertex(T.label, "person", "name", "peter",
  95. "age", 35, "city", "Shanghai");
  96. marko.addEdge("knows", vadas, "date", "20160110", "weight", 0.5);
  97. marko.addEdge("knows", josh, "date", "20130220", "weight", 1.0);
  98. marko.addEdge("created", lop, "date", "20171210", "weight", 0.4);
  99. josh.addEdge("created", lop, "date", "20091111", "weight", 0.4);
  100. josh.addEdge("created", ripple, "date", "20171210", "weight", 1.0);
  101. peter.addEdge("created", lop, "date", "20170324", "weight", 0.2);
  102. GremlinManager gremlin = hugeClient.gremlin();
  103. ResultSet resultSet = gremlin.gremlin("g.V().outE().path()").execute();
  104. Iterator<Result> results = resultSet.iterator();
  105. results.forEachRemaining(result -> {
  106. System.out.println(result.getObject().getClass());
  107. Object object = result.getObject();
  108. if (object instanceof Vertex) {
  109. System.out.println(((Vertex) object).id());
  110. } else if (object instanceof Edge) {
  111. System.out.println(((Edge) object).id());
  112. } else if (object instanceof Path) {
  113. List<Object> elements = ((Path) object).objects();
  114. elements.forEach(element -> {
  115. System.out.println(element.getClass());
  116. System.out.println(element);
  117. });
  118. } else {
  119. System.out.println(object);
  120. }
  121. });
  122. }
  123. }
4.3.2 BatchExample
  1. import java.util.LinkedList;
  2. import java.util.List;
  3. import com.baidu.hugegraph.driver.GraphManager;
  4. import com.baidu.hugegraph.driver.HugeClient;
  5. import com.baidu.hugegraph.driver.SchemaManager;
  6. import com.baidu.hugegraph.structure.graph.Edge;
  7. import com.baidu.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 = new HugeClient("http://localhost:8080", "hugegraph");
  12. SchemaManager schema = hugeClient.schema();
  13. schema.propertyKey("name").asText().ifNotExist().create();
  14. schema.propertyKey("age").asInt().ifNotExist().create();
  15. schema.propertyKey("lang").asText().ifNotExist().create();
  16. schema.propertyKey("date").asText().ifNotExist().create();
  17. schema.propertyKey("price").asInt().ifNotExist().create();
  18. schema.vertexLabel("person")
  19. .properties("name", "age")
  20. .primaryKeys("name")
  21. .ifNotExist()
  22. .create();
  23. schema.vertexLabel("person")
  24. .properties("price")
  25. .nullableKeys("price")
  26. .append();
  27. schema.vertexLabel("software")
  28. .properties("name", "lang", "price")
  29. .primaryKeys("name")
  30. .ifNotExist()
  31. .create();
  32. schema.indexLabel("softwareByPrice")
  33. .onV("software").by("price")
  34. .range()
  35. .ifNotExist()
  36. .create();
  37. schema.edgeLabel("knows")
  38. .link("person", "person")
  39. .properties("date")
  40. .ifNotExist()
  41. .create();
  42. schema.edgeLabel("created")
  43. .link("person", "software")
  44. .properties("date")
  45. .ifNotExist()
  46. .create();
  47. schema.indexLabel("createdByDate")
  48. .onE("created").by("date")
  49. .secondary()
  50. .ifNotExist()
  51. .create();
  52. GraphManager graph = hugeClient.graph();
  53. Vertex marko = new Vertex("person").property("name", "marko")
  54. .property("age", 29);
  55. Vertex vadas = new Vertex("person").property("name", "vadas")
  56. .property("age", 27);
  57. Vertex lop = new Vertex("software").property("name", "lop")
  58. .property("lang", "java")
  59. .property("price", 328);
  60. Vertex josh = new Vertex("person").property("name", "josh")
  61. .property("age", 32);
  62. Vertex ripple = new Vertex("software").property("name", "ripple")
  63. .property("lang", "java")
  64. .property("price", 199);
  65. Vertex peter = new Vertex("person").property("name", "peter")
  66. .property("age", 35);
  67. // Create a list to put vertex(Default max size is 500)
  68. List<Vertex> vertices = new LinkedList<>();
  69. vertices.add(marko);
  70. vertices.add(vadas);
  71. vertices.add(lop);
  72. vertices.add(josh);
  73. vertices.add(ripple);
  74. vertices.add(peter);
  75. // Post a vertex list to server
  76. vertices = graph.addVertices(vertices);
  77. vertices.forEach(vertex -> System.out.println(vertex));
  78. Edge markoKnowsVadas = new Edge("knows").source(marko).target(vadas)
  79. .property("date", "20160110");
  80. Edge markoKnowsJosh = new Edge("knows").source(marko).target(josh)
  81. .property("date", "20130220");
  82. Edge markoCreateLop = new Edge("created").source(marko).target(lop)
  83. .property("date", "20171210");
  84. Edge joshCreateRipple = new Edge("created").source(josh).target(ripple)
  85. .property("date", "20171210");
  86. Edge joshCreateLop = new Edge("created").source(josh).target(lop)
  87. .property("date", "20091111");
  88. Edge peterCreateLop = new Edge("created").source(peter).target(lop)
  89. .property("date", "20170324");
  90. // Create a list to put edge(Default max size is 500)
  91. List<Edge> edges = new LinkedList<>();
  92. edges.add(markoKnowsVadas);
  93. edges.add(markoKnowsJosh);
  94. edges.add(markoCreateLop);
  95. edges.add(joshCreateRipple);
  96. edges.add(joshCreateLop);
  97. edges.add(peterCreateLop);
  98. // Post a edge list to server
  99. edges = graph.addEdges(edges, false);
  100. edges.forEach(edge -> System.out.println(edge));
  101. }
  102. }

4.4 运行Example

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

4.5 Example示例说明

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