当前位置: 首页 > news >正文

HugeGraph 【图数据库】JAVA调用SDK

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.效果展示

http://www.dtcms.com/a/283363.html

相关文章:

  • 助力品牌从系统碎片化走向IT一体化建设,实现全渠道业务协同!——商派“数智化IT轻咨询”
  • MH32F103A单片机 可兼容替代STMCCT6/RCT6/RBT6,增强型
  • Kotlin重写函数中的命名参数
  • 【论文阅读】A Survey on Knowledge-Oriented Retrieval-Augmented Generation(4)
  • 【Android】CheckBox实现和监听
  • 归一化与激活函数:深度学习的双引擎
  • CentOS网络配置与LAMP环境搭建指南
  • Product Hunt 每日热榜 | 2025-07-16
  • 计算机网络——数据链路层(25王道最新版)
  • Oracle 关于一些连接故障的总结
  • xss1-8
  • Traefik 中实现流量治理3种方式和场景选择
  • AI Agent 入门与概览
  • VLAN实验(2)
  • 需求分析方法论
  • unity中利用Curvy Spline插件实现简单的可视化路径
  • 猎板分享:印制线路板制造工艺的创新与质量管控新策略
  • STM32 GPIO的八种工作模式
  • Python暑期学习笔记一
  • Swift基础 -- 3、协议和扩展、错误处理、范型
  • 宇树 G1 部署(七)——系统重装与镜像还原
  • 第13章 AB实验平台的建设
  • Redis原理和应用以及整合SpringBoot+Vue
  • RAG优化秘籍:基于Tablestore的知识库答疑系统架构设计
  • 智能体架构深度解构:一次用户请求的完整旅程
  • 多维动态规划题解——最小路径和【LeetCode】空间优化一维数组
  • Java设计模式之-组合模式
  • Fiddler 中文版 API 调试与性能优化实践 官方中文网全程支持
  • 怎么删除 wps 的右键菜单
  • Android-EDLA【CTS】CtsMediaRecorderTestCases存在fail