# kg-graph **Repository Path**: chengjunjie/kg-graph ## Basic Information - **Project Name**: kg-graph - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-09-02 - **Last Updated**: 2025-09-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Knowledge Graph System 一个基于 Spring Boot + JPA + MySQL + TinkerPop + TinkerGraph 的知识图谱系统,提供完整的节点和关系管理功能,支持 G6 图可视化框架。 ## 技术栈 - **Java**: 1.8 - **Spring Boot**: 2.7.14 - **数据库**: MySQL 8.0 + JPA/Hibernate - **图计算**: Apache TinkerPop 3.6.4 + TinkerGraph - **前端框架**: 支持 G6 可视化 - **文档**: Swagger/OpenAPI 3 - **测试**: JUnit 5 + Mockito + H2 ## 项目结构 ``` knowledge-graph/ ├── src/ │ ├── main/ │ │ ├── java/com/knowledgegraph/ │ │ │ ├── entity/ # 实体类 │ │ │ ├── repository/ # JPA 存储库 │ │ │ ├── service/ # 业务逻辑服务 │ │ │ ├── controller/ # REST API 控制器 │ │ │ ├── dto/ # 数据传输对象 │ │ │ ├── config/ # 配置类 │ │ │ └── KnowledgeGraphApplication.java │ │ └── resources/ │ │ └── application.properties │ └── test/ # 测试代码 └── pom.xml ``` ## 核心功能 ### 1. 实体管理 - **节点 (Node)**: 知识图谱中的实体,支持类型、属性、位置等信息 - **资源 (Resource)**: 文档、文件等资源管理 - **节点关系 (NodeRelation)**: 节点之间的有向/无向关系 - **节点资源关联 (NodeResource)**: 节点与资源的多对多关联 ### 2. 图数据处理 - **TinkerPop 集成**: 使用 Gremlin 进行图查询和分析 - **路径查找**: 最短路径、邻居查找等算法 - **图统计**: 节点数量、连通分量、中心性计算等 - **环路检测**: 图中环路结构分析 ### 3. G6 数据转换 - **完整图数据**: 转换为 G6 格式的完整知识图谱 - **子图提取**: 根据节点 ID 列表生成子图 - **树状结构**: 以指定节点为根的树状图数据 - **样式配置**: 支持节点和边的自定义样式 ## API 接口 ### 节点管理 ``` POST /api/knowledge-graph/nodes # 创建节点 GET /api/knowledge-graph/nodes/{id} # 获取节点详情 GET /api/knowledge-graph/nodes # 获取节点列表 PUT /api/knowledge-graph/nodes/{id} # 更新节点 DELETE /api/knowledge-graph/nodes/{id} # 删除节点 ``` ### 关系管理 ``` POST /api/knowledge-graph/relations # 创建节点关系 GET /api/knowledge-graph/nodes/{id}/relations # 获取节点关系 DELETE /api/knowledge-graph/relations/{id} # 删除关系 ``` ### 资源管理 ``` POST /api/knowledge-graph/resources # 创建资源 GET /api/knowledge-graph/resources/{id} # 获取资源详情 GET /api/knowledge-graph/resources # 获取资源列表 PUT /api/knowledge-graph/resources/{id} # 更新资源 DELETE /api/knowledge-graph/resources/{id} # 删除资源 ``` ### G6 可视化数据 ``` GET /api/knowledge-graph/g6/full-graph # 完整图数据 POST /api/knowledge-graph/g6/sub-graph # 子图数据 GET /api/knowledge-graph/g6/tree/{rootId} # 树状图数据 ``` ### 图分析 ``` GET /api/knowledge-graph/analysis/shortest-path # 最短路径 GET /api/knowledge-graph/analysis/neighbors/{id} # 邻居节点 GET /api/knowledge-graph/analysis/statistics # 图统计 GET /api/knowledge-graph/analysis/centrality/{id} # 节点中心性 GET /api/knowledge-graph/analysis/connected-components # 连通分量 GET /api/knowledge-graph/analysis/cycles # 环路检测 ``` ## 数据库设计 ### 节点表 (kg_node) - id, name, node_type, description - properties (JSON), x_position, y_position - style_config (JSON), created_at, updated_at ### 资源表 (kg_resource) - id, name, resource_type, description - file_path, file_size, mime_type, url - metadata (JSON), created_at, updated_at ### 节点关系表 (kg_node_relation) - id, source_node_id, target_node_id - relation_type, weight, description - properties (JSON), is_directed, created_at, updated_at ### 节点资源关联表 (kg_node_resource) - id, node_id, resource_id - relation_type, weight, description - properties (JSON), created_at, updated_at ## 快速开始 ### 1. 环境准备 ```bash # 确保安装了 JDK 1.8 和 MySQL 8.0 java -version mysql --version ``` ### 2. 数据库配置 ```sql CREATE DATABASE knowledge_graph CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; ``` ### 3. 配置文件 编辑 `src/main/resources/application.properties`: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/knowledge_graph?useSSL=false&serverTimezone=UTC&characterEncoding=utf8 spring.datasource.username=your_username spring.datasource.password=your_password ``` ### 4. 构建和运行 ```bash # 构建项目 mvn clean compile # 运行测试 mvn test # 启动应用 mvn spring-boot:run ``` ### 5. 访问应用 - 应用地址: http://localhost:8080/knowledge-graph - API 文档: http://localhost:8080/knowledge-graph/swagger-ui.html - H2 控制台 (测试): http://localhost:8080/knowledge-graph/h2-console ## G6 前端集成 ### 数据格式示例 ```javascript // G6 图数据格式 { "nodes": [ { "id": "1", "label": "概念节点", "type": "circle", "x": 100, "y": 200, "comboId": "concept", "style": { "fill": "#1890ff" }, "data": { "nodeType": "concept", "description": "..." } } ], "edges": [ { "id": "1", "source": "1", "target": "2", "label": "关联", "type": "line", "weight": 0.8, "style": { "stroke-width": 2 } } ], "combos": [ { "id": "concept", "label": "概念类 (5)", "type": "rect", "style": { "fill": "#e6f7ff" } } ] } ``` ### 前端调用示例 ```javascript // 获取完整图数据 fetch('/knowledge-graph/api/knowledge-graph/g6/full-graph') .then(response => response.json()) .then(data => { // 使用 G6 渲染图数据 const graph = new G6.Graph({ container: 'graph-container', data: data, // ... G6 配置 }); }); // 获取子图数据 fetch('/knowledge-graph/api/knowledge-graph/g6/sub-graph', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify([1, 2, 3]) // 节点 ID 数组 }) .then(response => response.json()) .then(data => { // 渲染子图 }); ``` ## 测试 项目包含完整的测试套件: - **单元测试**: Service 和 Controller 层测试 - **集成测试**: Repository 和数据库集成测试 - **API 测试**: REST API 端到端测试 ```bash # 运行所有测试 mvn test # 运行特定测试类 mvn test -Dtest=KnowledgeGraphServiceTest # 生成测试报告 mvn surefire-report:report ``` ## 扩展功能 ### 1. 图算法扩展 - PageRank 算法 - 社区发现算法 - 图嵌入和相似度计算 ### 2. 数据导入导出 - CSV/Excel 批量导入 - GraphML/GEXF 格式导出 - 知识图谱备份和恢复 ### 3. 高级查询 - 复杂 Gremlin 查询支持 - 图模式匹配 - 路径查询和推理 ## 性能优化 1. **数据库优化** - 适当的索引策略 - 连接池配置优化 - 查询性能监控 2. **图计算优化** - TinkerGraph 内存管理 - 大图分片处理 - 异步图分析 3. **缓存策略** - Redis 缓存热点数据 - G6 数据缓存 - 查询结果缓存 ## 部署建议 ### Docker 部署 ```dockerfile FROM openjdk:8-jre-slim COPY target/knowledge-graph-1.0.0.jar app.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "/app.jar"] ``` ### 生产环境配置 - 数据库连接池调优 - 日志级别配置 - 监控和告警设置 - 备份策略制定 ## 贡献指南 1. Fork 项目 2. 创建特性分支 3. 编写测试用例 4. 提交代码变更 5. 创建 Pull Request ## 许可证 Apache License 2.0