HugeGraph-Client Quick Start
1 HugeGraph-Client 概述
HugeGraph-Client 向 HugeGraph-Server 发出 HTTP 请求,获取并解析 Server 的执行结果。 提供了 Java/Go/Python 版, 用户可以使用 Client-API 编写代码操作 HugeGraph,比如元数据和图数据的增删改查,或者执行 gremlin 语句等。 后文主要是 Java 使用示例 (其他语言 SDK 可参考对应 READEME 页面)
现在已经支持基于 Go 语言的 HugeGraph Client SDK (version >=1.2.0)
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 依赖
2. <dependencies>
3. <dependency>
4. <groupId>org.apache.hugegraph</groupId>
5. <artifactId>hugegraph-client</artifactId>
6. <!-- Update to the latest release version -->
7. <version>1.5.0</version>
8. </dependency>
9. </dependencies>
注:Graph 所有组件版本号均保持一致
4.3 Example
4.3.1 SingleExample
1. import java.io.IOException;
2. import java.util.Iterator;
3. import java.util.List;
5. import org.apache.hugegraph.driver.GraphManager;
6. import org.apache.hugegraph.driver.GremlinManager;
7. import org.apache.hugegraph.driver.HugeClient;
8. import org.apache.hugegraph.driver.SchemaManager;
9. import org.apache.hugegraph.structure.constant.T;
10. import org.apache.hugegraph.structure.graph.Edge;
11. import org.apache.hugegraph.structure.graph.Path;
12. import org.apache.hugegraph.structure.graph.Vertex;
13. import org.apache.hugegraph.structure.gremlin.Result;
14. import org.apache.hugegraph.structure.gremlin.ResultSet;
16. public class SingleExample {
18. public static void main(String[] args) throws IOException {
19. // If connect failed will throw a exception.
20. HugeClient hugeClient = HugeClient.builder("http://localhost:8080",
21. "hugegraph")
22. .build();
24. SchemaManager schema = hugeClient.schema();
26. schema.propertyKey("name").asText().ifNotExist().create();
27. schema.propertyKey("age").asInt().ifNotExist().create();
28. schema.propertyKey("city").asText().ifNotExist().create();
29. schema.propertyKey("weight").asDouble().ifNotExist().create();
30. schema.propertyKey("lang").asText().ifNotExist().create();
31. schema.propertyKey("date").asDate().ifNotExist().create();
32. schema.propertyKey("price").asInt().ifNotExist().create();
34. schema.vertexLabel("person")
35. .properties("name", "age", "city")
36. .primaryKeys("name")
37. .ifNotExist()
38. .create();
40. schema.vertexLabel("software")
41. .properties("name", "lang", "price")
42. .primaryKeys("name")
43. .ifNotExist()
44. .create();
46. schema.indexLabel("personByCity")
47. .onV("person")
48. .by("city")
49. .secondary()
50. .ifNotExist()
51. .create();
53. schema.indexLabel("personByAgeAndCity")
54. .onV("person")
55. .by("age", "city")
56. .secondary()
57. .ifNotExist()
58. .create();
60. schema.indexLabel("softwareByPrice")
61. .onV("software")
62. .by("price")
63. .range()
64. .ifNotExist()
65. .create();
67. schema.edgeLabel("knows")
68. .sourceLabel("person")
69. .targetLabel("person")
70. .properties("date", "weight")
71. .ifNotExist()
72. .create();
74. schema.edgeLabel("created")
75. .sourceLabel("person").targetLabel("software")
76. .properties("date", "weight")
77. .ifNotExist()
78. .create();
80. schema.indexLabel("createdByDate")
81. .onE("created")
82. .by("date")
83. .secondary()
84. .ifNotExist()
85. .create();
87. schema.indexLabel("createdByWeight")
88. .onE("created")
89. .by("weight")
90. .range()
91. .ifNotExist()
92. .create();
94. schema.indexLabel("knowsByWeight")
95. .onE("knows")
96. .by("weight")
97. .range()
98. .ifNotExist()
99. .create();
101. GraphManager graph = hugeClient.graph();
102. Vertex marko = graph.addVertex(T.LABEL, "person", "name", "marko",
103. "age", 29, "city", "Beijing");
104. Vertex vadas = graph.addVertex(T.LABEL, "person", "name", "vadas",
105. "age", 27, "city", "Hongkong");
106. Vertex lop = graph.addVertex(T.LABEL, "software", "name", "lop",
107. "lang", "java", "price", 328);
108. Vertex josh = graph.addVertex(T.LABEL, "person", "name", "josh",
109. "age", 32, "city", "Beijing");
110. Vertex ripple = graph.addVertex(T.LABEL, "software", "name", "ripple",
111. "lang", "java", "price", 199);
112. Vertex peter = graph.addVertex(T.LABEL, "person", "name", "peter",
113. "age", 35, "city", "Shanghai");
115. marko.addEdge("knows", vadas, "date", "2016-01-10", "weight", 0.5);
116. marko.addEdge("knows", josh, "date", "2013-02-20", "weight", 1.0);
117. marko.addEdge("created", lop, "date", "2017-12-10", "weight", 0.4);
118. josh.addEdge("created", lop, "date", "2009-11-11", "weight", 0.4);
119. josh.addEdge("created", ripple, "date", "2017-12-10", "weight", 1.0);
120. peter.addEdge("created", lop, "date", "2017-03-24", "weight", 0.2);
122. GremlinManager gremlin = hugeClient.gremlin();
123. System.out.println("==== Path ====");
124. ResultSet resultSet = gremlin.gremlin("g.V().outE().path()").execute();
125. Iterator<Result> results = resultSet.iterator();
126. results.forEachRemaining(result -> {
127. System.out.println(result.getObject().getClass());
128. Object object = result.getObject();
129. if (object instanceof Vertex) {
130. System.out.println(((Vertex) object).id());
131. } else if (object instanceof Edge) {
132. System.out.println(((Edge) object).id());
133. } else if (object instanceof Path) {
134. List<Object> elements = ((Path) object).objects();
135. elements.forEach(element -> {
136. System.out.println(element.getClass());
137. System.out.println(element);
138. });
139. } else {
140. System.out.println(object);
141. }
142. });
144. hugeClient.close();
145. }
146. }
4.3.2 BatchExample
1. import java.util.ArrayList;
2. import java.util.List;
4. import org.apache.hugegraph.driver.GraphManager;
5. import org.apache.hugegraph.driver.HugeClient;
6. import org.apache.hugegraph.driver.SchemaManager;
7. import org.apache.hugegraph.structure.graph.Edge;
8. import org.apache.hugegraph.structure.graph.Vertex;
10. public class BatchExample {
12. public static void main(String[] args) {
13. // If connect failed will throw a exception.
14. HugeClient hugeClient = HugeClient.builder("http://localhost:8080",
15. "hugegraph")
16. .build();
18. SchemaManager schema = hugeClient.schema();
20. schema.propertyKey("name").asText().ifNotExist().create();
21. schema.propertyKey("age").asInt().ifNotExist().create();
22. schema.propertyKey("lang").asText().ifNotExist().create();
23. schema.propertyKey("date").asDate().ifNotExist().create();
24. schema.propertyKey("price").asInt().ifNotExist().create();
26. schema.vertexLabel("person")
27. .properties("name", "age")
28. .primaryKeys("name")
29. .ifNotExist()
30. .create();
32. schema.vertexLabel("person")
33. .properties("price")
34. .nullableKeys("price")
35. .append();
37. schema.vertexLabel("software")
38. .properties("name", "lang", "price")
39. .primaryKeys("name")
40. .ifNotExist()
41. .create();
43. schema.indexLabel("softwareByPrice")
44. .onV("software").by("price")
45. .range()
46. .ifNotExist()
47. .create();
49. schema.edgeLabel("knows")
50. .link("person", "person")
51. .properties("date")
52. .ifNotExist()
53. .create();
55. schema.edgeLabel("created")
56. .link("person", "software")
57. .properties("date")
58. .ifNotExist()
59. .create();
61. schema.indexLabel("createdByDate")
62. .onE("created").by("date")
63. .secondary()
64. .ifNotExist()
65. .create();
67. // get schema object by name
68. System.out.println(schema.getPropertyKey("name"));
69. System.out.println(schema.getVertexLabel("person"));
70. System.out.println(schema.getEdgeLabel("knows"));
71. System.out.println(schema.getIndexLabel("createdByDate"));
73. // list all schema objects
74. System.out.println(schema.getPropertyKeys());
75. System.out.println(schema.getVertexLabels());
76. System.out.println(schema.getEdgeLabels());
77. System.out.println(schema.getIndexLabels());
79. GraphManager graph = hugeClient.graph();
81. Vertex marko = new Vertex("person").property("name", "marko")
82. .property("age", 29);
83. Vertex vadas = new Vertex("person").property("name", "vadas")
84. .property("age", 27);
85. Vertex lop = new Vertex("software").property("name", "lop")
86. .property("lang", "java")
87. .property("price", 328);
88. Vertex josh = new Vertex("person").property("name", "josh")
89. .property("age", 32);
90. Vertex ripple = new Vertex("software").property("name", "ripple")
91. .property("lang", "java")
92. .property("price", 199);
93. Vertex peter = new Vertex("person").property("name", "peter")
94. .property("age", 35);
96. Edge markoKnowsVadas = new Edge("knows").source(marko).target(vadas)
97. .property("date", "2016-01-10");
98. Edge markoKnowsJosh = new Edge("knows").source(marko).target(josh)
99. .property("date", "2013-02-20");
100. Edge markoCreateLop = new Edge("created").source(marko).target(lop)
101. .property("date",
102. "2017-12-10");
103. Edge joshCreateRipple = new Edge("created").source(josh).target(ripple)
104. .property("date",
105. "2017-12-10");
106. Edge joshCreateLop = new Edge("created").source(josh).target(lop)
107. .property("date", "2009-11-11");
108. Edge peterCreateLop = new Edge("created").source(peter).target(lop)
109. .property("date",
110. "2017-03-24");
112. List<Vertex> vertices = new ArrayList<>();
113. vertices.add(marko);
114. vertices.add(vadas);
115. vertices.add(lop);
116. vertices.add(josh);
117. vertices.add(ripple);
118. vertices.add(peter);
120. List<Edge> edges = new ArrayList<>();
121. edges.add(markoKnowsVadas);
122. edges.add(markoKnowsJosh);
123. edges.add(markoCreateLop);
124. edges.add(joshCreateRipple);
125. edges.add(joshCreateLop);
126. edges.add(peterCreateLop);
128. vertices = graph.addVertices(vertices);
129. vertices.forEach(vertex -> System.out.println(vertex));
131. edges = graph.addEdges(edges, false);
132. edges.forEach(edge -> System.out.println(edge));
134. hugeClient.close();
135. }
136. }
4.4 运行 Example
运行 Example 之前需要启动 Server, 启动过程见HugeGraph-Server Quick Start
4.5 详细 API 说明
示例说明见HugeGraph-Client 基本 API 介绍
Last modified December 13, 2024: chore: update version to 1.5.0 (#385) (5e7803ce)
