1.引入依赖
<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>28.0-jre</version> </dependency><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>4.9.3</version> </dependency><dependency><groupId>org.apache.hugegraph</groupId><artifactId>hugegraph-client</artifactId><version>1.5.0</version></dependency>
2.引入依赖
import com.sws.link.common.api.dto.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.hugegraph.driver.GraphManager;
import org.apache.hugegraph.driver.GremlinManager;
import org.apache.hugegraph.driver.HugeClient;
import org.apache.hugegraph.driver.SchemaManager;
import org.apache.hugegraph.structure.constant.T;
import org.apache.hugegraph.structure.graph.Edge;
import org.apache.hugegraph.structure.graph.Path;
import org.apache.hugegraph.structure.graph.Vertex;
import org.apache.hugegraph.structure.gremlin.Result;
import org.apache.hugegraph.structure.gremlin.ResultSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate; // 使用Spring的JdbcTemplate
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;@RestController
@RequestMapping("/api")
@Slf4j
public class TestController {// public static void main(String[] args) throws IOException {
// // 连接HugeGraph服务器
// HugeClient hugeClient = HugeClient.builder("http://192.168.44.135:8080",
// "hugegraph")
// .build();
//
// SchemaManager schema = hugeClient.schema();
//
//
// // 创建属性键
// schema.propertyKey("名称").asText().ifNotExist().create();
// schema.propertyKey("人口").asInt().ifNotExist().create();
// schema.propertyKey("面积").asDouble().ifNotExist().create();
// schema.propertyKey("建成年份").asInt().ifNotExist().create();
// schema.propertyKey("库容量").asDouble().ifNotExist().create();
// schema.propertyKey("级别").asText().ifNotExist().create(); // 省级、市级等
// schema.propertyKey("所属流域").asText().ifNotExist().create();
// schema.propertyKey("管辖关系").asText().ifNotExist().create(); // 完全管辖、部分管辖等
// schema.propertyKey("建设时间").asDate().ifNotExist().create();
// schema.propertyKey("边界长度").asDouble().ifNotExist().create(); // 新增这一行
//
// // 创建顶点标签:行政区划
// schema.vertexLabel("行政区划")
// .properties("名称", "人口", "面积", "级别")
// .primaryKeys("名称")
// .ifNotExist()
// .create();
//
// // 创建顶点标签:水库
// schema.vertexLabel("水库")
// .properties("名称", "建成年份", "库容量", "所属流域")
// .primaryKeys("名称")
// .ifNotExist()
// .create();
//
// // 创建索引
// schema.indexLabel("行政区划按级别")
// .onV("行政区划")
// .by("级别")
// .secondary()
// .ifNotExist()
// .create();
//
// schema.indexLabel("水库按流域")
// .onV("水库")
// .by("所属流域")
// .secondary()
// .ifNotExist()
// .create();
//
// schema.indexLabel("水库按库容量")
// .onV("水库")
// .by("库容量")
// .range()
// .ifNotExist()
// .create();
//
// // 创建边标签:包含(行政区划包含水库)
// schema.edgeLabel("包含")
// .sourceLabel("行政区划")
// .targetLabel("水库")
// .properties("管辖关系", "建设时间")
// .ifNotExist()
// .create();
//
// // 创建边标签:相邻(行政区划之间相邻)
// schema.edgeLabel("相邻")
// .sourceLabel("行政区划")
// .targetLabel("行政区划")
// .properties("边界长度")
// .ifNotExist()
// .create();
//
// // 创建边上的索引
// schema.indexLabel("包含关系按管辖类型")
// .onE("包含")
// .by("管辖关系")
// .secondary()
// .ifNotExist()
// .create();
//
// // 添加顶点数据
// GraphManager graph = hugeClient.graph();
//
// // 行政区划顶点
// Vertex 北京市 = graph.addVertex(T.LABEL, "行政区划", "名称", "北京市",
// "人口", 2154, "面积", 16410.54, "级别", "直辖市");
// Vertex 河北省 = graph.addVertex(T.LABEL, "行政区划", "名称", "河北省",
// "人口", 7556, "面积", 188800, "级别", "省级");
// Vertex 河南省 = graph.addVertex(T.LABEL, "行政区划", "名称", "河南省",
// "人口", 9605, "面积", 167000, "级别", "省级");
// Vertex 湖北省 = graph.addVertex(T.LABEL, "行政区划", "名称", "湖北省",
// "人口", 5917, "面积", 185900, "级别", "省级");
//
// // 水库顶点
// Vertex 密云水库 = graph.addVertex(T.LABEL, "水库", "名称", "密云水库",
// "建成年份", 1960, "库容量", 43.75, "所属流域", "潮白河");
// Vertex 官厅水库 = graph.addVertex(T.LABEL, "水库", "名称", "官厅水库",
// "建成年份", 1954, "库容量", 41.6, "所属流域", "永定河");
// Vertex 丹江口水库 = graph.addVertex(T.LABEL, "水库", "名称", "丹江口水库",
// "建成年份", 1973, "库容量", 290.5, "所属流域", "汉江");
//
// // 添加边关系
// 北京市.addEdge("包含", 密云水库, "管辖关系", "完全管辖", "建设时间", "1958-09-01");
// 北京市.addEdge("包含", 官厅水库, "管辖关系", "部分管辖", "建设时间", "1951-10-01");
// 河北省.addEdge("包含", 官厅水库, "管辖关系", "部分管辖", "建设时间", "1951-10-01");
// 河南省.addEdge("包含", 丹江口水库, "管辖关系", "部分管辖", "建设时间", "1958-09-01");
// 湖北省.addEdge("包含", 丹江口水库, "管辖关系", "部分管辖", "建设时间", "1958-09-01");
//
// 北京市.addEdge("相邻", 河北省, "边界长度", 1200.0);
// 河北省.addEdge("相邻", 河南省, "边界长度", 500.0);
// 河南省.addEdge("相邻", 湖北省, "边界长度", 600.0);
//
// // 执行Gremlin查询并输出结果
// GremlinManager gremlin = hugeClient.gremlin();
// System.out.println("==== 路径查询结果 ====");
// ResultSet resultSet = gremlin.gremlin("g.V().outE().path()").execute();
// Iterator<Result> results = resultSet.iterator();
//
// results.forEachRemaining(result -> {
// Object object = result.getObject();
// if (object instanceof Path) {
// List<Object> elements = ((Path) object).objects();
// elements.forEach(element -> {
// if (element instanceof Vertex) {
// Vertex v = (Vertex) element;
// System.out.println("顶点: " + v.property("名称"));
// } else if (element instanceof Edge) {
// Edge e = (Edge) element;
// System.out.println("边: " + e.label() + " - " + e.property("管辖关系"));
// }
// });
// System.out.println("-----");
// }
// });
//
// hugeClient.close();
// }public static void main(String[] args) {// 连接HugeGraph服务器HugeClient hugeClient = HugeClient.builder("http://192.168.44.135:8080","hugegraph").build();SchemaManager schema = hugeClient.schema();// 定义属性键schema.propertyKey("名称").asText().ifNotExist().create();schema.propertyKey("人口").asInt().ifNotExist().create();schema.propertyKey("面积").asDouble().ifNotExist().create();schema.propertyKey("级别").asText().ifNotExist().create();schema.propertyKey("建成年份").asInt().ifNotExist().create();schema.propertyKey("库容量").asDouble().ifNotExist().create();schema.propertyKey("所属流域").asText().ifNotExist().create();schema.propertyKey("管辖关系").asText().ifNotExist().create();schema.propertyKey("建设时间").asDate().ifNotExist().create();schema.propertyKey("边界长度").asDouble().ifNotExist().create();schema.propertyKey("邮政编码").asText().ifNotExist().create(); // 创建行政区划顶点标签schema.vertexLabel("行政区划").properties("名称", "人口", "面积", "级别").primaryKeys("名称").ifNotExist().create();// 为行政区划追加可选属性(示例)schema.vertexLabel("行政区划").properties("邮政编码").nullableKeys("邮政编码").append();// 创建水库顶点标签schema.vertexLabel("水库").properties("名称", "建成年份", "库容量", "所属流域").primaryKeys("名称").ifNotExist().create();// 创建索引schema.indexLabel("水库按库容量").onV("水库").by("库容量").range().ifNotExist().create();schema.indexLabel("行政区划按级别").onV("行政区划").by("级别").secondary().ifNotExist().create();// 创建边标签schema.edgeLabel("包含").link("行政区划", "水库").properties("管辖关系", "建设时间").ifNotExist().create();schema.edgeLabel("相邻").link("行政区划", "行政区划").properties("边界长度").ifNotExist().create();// 创建边上的索引schema.indexLabel("包含关系按建设时间").onE("包含").by("建设时间").secondary().ifNotExist().create();// 打印schema信息System.out.println("属性键: " + schema.getPropertyKey("名称"));System.out.println("顶点标签: " + schema.getVertexLabel("行政区划"));System.out.println("边标签: " + schema.getEdgeLabel("包含"));System.out.println("索引: " + schema.getIndexLabel("水库按库容量"));// 批量创建顶点GraphManager graph = hugeClient.graph();// 准备行政区划顶点Vertex bejing = new Vertex("行政区划").property("名称", "北京市").property("人口", 2154).property("面积", 16410.54).property("级别", "直辖市");Vertex hebei = new Vertex("行政区划").property("名称", "河北省").property("人口", 7556).property("面积", 188800).property("级别", "省级");Vertex henan = new Vertex("行政区划").property("名称", "河南省").property("人口", 9605).property("面积", 167000).property("级别", "省级");// 准备水库顶点Vertex mima = new Vertex("水库").property("名称", "密云水库").property("建成年份", 1960).property("库容量", 43.75).property("所属流域", "潮白河");Vertex guantingshuiku = new Vertex("水库").property("名称", "官厅水库").property("建成年份", 1954).property("库容量", 41.6).property("所属流域", "永定河");Vertex danjiangkoushuiku = new Vertex("水库").property("名称", "丹江口水库").property("建成年份", 1973).property("库容量", 290.5).property("所属流域", "汉江");// 批量添加顶点List<Vertex> vertices = new ArrayList<>();vertices.add(bejing);vertices.add(hebei);vertices.add(henan);vertices.add(mima);vertices.add(guantingshuiku);vertices.add(danjiangkoushuiku);vertices = graph.addVertices(vertices);System.out.println("已添加顶点:");vertices.forEach(vertex -> System.out.println(vertex.property("名称")));// 批量创建边Edge 北京包含密云 = new Edge("包含").source(bejing).target(mima).property("管辖关系", "完全管辖").property("建设时间", "1958-09-01");Edge 北京包含官厅 = new Edge("包含").source(bejing).target(guantingshuiku).property("管辖关系", "部分管辖").property("建设时间", "1951-10-01");Edge 河北包含官厅 = new Edge("包含").source(hebei).target(guantingshuiku).property("管辖关系", "部分管辖").property("建设时间", "1951-10-01");Edge 北京相邻河北 = new Edge("相邻").source(bejing).target(hebei).property("边界长度", 1200.0);Edge 河北相邻河南 = new Edge("相邻").source(hebei).target(henan).property("边界长度", 500.0);// 批量添加边List<Edge> edges = new ArrayList<>();edges.add(北京包含密云);edges.add(北京包含官厅);edges.add(河北包含官厅);edges.add(北京相邻河北);edges.add(河北相邻河南);edges = graph.addEdges(edges, false);System.out.println("已添加边:");hugeClient.close();}
}
3.效果展示
