| @@ -178,6 +178,11 @@ | |||
| <artifactId>client-java-api</artifactId> | |||
| <version>12.0.1</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>com.opencsv</groupId> | |||
| <artifactId>opencsv</artifactId> | |||
| <version>5.7.1</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>io.fabric8</groupId> | |||
| @@ -6,12 +6,14 @@ import com.ruoyi.common.core.web.controller.BaseController; | |||
| import com.ruoyi.common.core.web.page.TableDataInfo; | |||
| import com.ruoyi.platform.domain.kg.KgInfo; | |||
| import com.ruoyi.platform.domain.kg.KgInfoPageVo; | |||
| import com.ruoyi.platform.domain.kg.KnowledgeGraphVersion; | |||
| import com.ruoyi.platform.domain.kg.dto.KgInfoIdDTO; | |||
| import com.ruoyi.platform.service.KgInfoService; | |||
| import com.ruoyi.system.api.domain.SysDictData; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.validation.annotation.Validated; | |||
| import org.springframework.web.bind.annotation.*; | |||
| import org.springframework.web.multipart.MultipartFile; | |||
| import javax.servlet.http.HttpServletResponse; | |||
| import java.util.List; | |||
| @@ -52,6 +54,7 @@ public class KgInfoController extends BaseController { | |||
| /** | |||
| * 图谱列表 | |||
| * | |||
| * @return | |||
| */ | |||
| @GetMapping("/list") | |||
| @@ -63,16 +66,18 @@ public class KgInfoController extends BaseController { | |||
| /** | |||
| * 图谱详情 | |||
| * | |||
| * @param kgInfoIdDTO | |||
| * @return | |||
| */ | |||
| @GetMapping | |||
| public R<KgInfo> getKgInfo( KgInfoIdDTO kgInfoIdDTO) { | |||
| public R<KgInfo> getKgInfo(KgInfoIdDTO kgInfoIdDTO) { | |||
| return R.ok(kgInfoService.getKgInfo(kgInfoIdDTO)); | |||
| } | |||
| /** | |||
| * 删除图谱 | |||
| * | |||
| * @param id | |||
| * @return | |||
| */ | |||
| @@ -84,25 +89,68 @@ public class KgInfoController extends BaseController { | |||
| /** | |||
| * 图谱版本列表 | |||
| * | |||
| * @param kgInfoIdDTO | |||
| * @return | |||
| */ | |||
| @GetMapping("/version/list") | |||
| public TableDataInfo versionList(KgInfoIdDTO kgInfoIdDTO) { | |||
| startPage(); | |||
| List<KgInfo> kgInfo = kgInfoService.versionList(kgInfoIdDTO); | |||
| List<KnowledgeGraphVersion> kgInfo = kgInfoService.versionList(kgInfoIdDTO); | |||
| return getDataTable(kgInfo); | |||
| } | |||
| /** | |||
| * 图谱版本导出 | |||
| * | |||
| * @param response | |||
| * @param kgInfoIdDTO | |||
| */ | |||
| @GetMapping("/version/export") | |||
| public void versionExport(HttpServletResponse response,KgInfoIdDTO kgInfoIdDTO) { | |||
| public void versionExport(HttpServletResponse response, KgInfoIdDTO kgInfoIdDTO) { | |||
| KgInfo KgInfo = kgInfoService.getKgInfo(kgInfoIdDTO); | |||
| ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class); | |||
| util.exportExcel(response, null, "图谱数据"); | |||
| } | |||
| /** | |||
| * 图谱版本导入 | |||
| * | |||
| * @param file | |||
| * @param kgId | |||
| */ | |||
| @PostMapping("/version/upload") | |||
| public R<String> uploadCsv(@RequestParam("file") MultipartFile file,@RequestParam("kgId") String kgId) { | |||
| if (file == null || file.isEmpty()) { | |||
| throw new IllegalArgumentException("文件不能为空"); | |||
| } | |||
| if (!file.getOriginalFilename().endsWith(".csv")) { | |||
| throw new IllegalArgumentException("仅支持 CSV 文件"); | |||
| } | |||
| kgInfoService.uploadCsv(file, kgId); | |||
| return R.ok(); | |||
| } | |||
| /** | |||
| * 图谱版本全量更新 | |||
| * | |||
| * @param id 版本id | |||
| */ | |||
| @PostMapping("/version/fullUpdate") | |||
| public R<String> fullUpdate(@RequestParam("id") Long id) { | |||
| kgInfoService.fullUpdate(id); | |||
| return R.ok(); | |||
| } | |||
| /** | |||
| * 图谱版本增量更新 | |||
| * | |||
| * @param id | |||
| */ | |||
| @PostMapping("/version/incrementalUpdate") | |||
| public void incrementalUpdate(@RequestParam("id") Long id) { | |||
| kgInfoService.incrementalUpdate(id); | |||
| } | |||
| } | |||
| @@ -57,7 +57,7 @@ public class KnowledgeGraphController { | |||
| * @return 单条数据 | |||
| */ | |||
| @GetMapping("{id}") | |||
| public AjaxResult queryById(@PathVariable("id") Integer id) { | |||
| public AjaxResult queryById(@PathVariable("id") Long id) { | |||
| return AjaxResult.success(this.knowledgeGraphService.queryById(id)); | |||
| } | |||
| @@ -95,32 +95,32 @@ public class KnowledgeGraphController { | |||
| } | |||
| @PostMapping("/upload") | |||
| public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, | |||
| @RequestParam("knowledgeGraphId") Integer knowledgeGraphId, | |||
| @RequestParam("version") String version) { | |||
| KnowledgeGraph knowledgeGraph = knowledgeGraphService.queryById(knowledgeGraphId); | |||
| KnowledgeGraphVersion knowledgeGraphVersion = new KnowledgeGraphVersion(); | |||
| knowledgeGraphVersion.setKgId(knowledgeGraphId); | |||
| knowledgeGraphVersion.setVersion(version); | |||
| knowledgeGraphVersionService.insert(knowledgeGraphVersion); | |||
| // 构建文件存储路径 | |||
| String baseDir = env.getProperty("file.upload.base.dir", "/data/knowledgegraph"); | |||
| LoginUser loginUser = SecurityUtils.getLoginUser(); | |||
| Path filePath = Paths.get(baseDir, loginUser.getUsername(), knowledgeGraph.getKgName(), version, file.getOriginalFilename()); | |||
| try { | |||
| // 确保路径存在,如果不存在则创建 | |||
| Files.createDirectories(filePath.getParent()); | |||
| // 保存文件 | |||
| file.transferTo(filePath.toFile()); | |||
| } catch (IOException e) { | |||
| return AjaxResult.error("文件上传失败: " + e.getMessage()); | |||
| } | |||
| return AjaxResult.success("文件上传成功,存储路径为: " + filePath.toString()); | |||
| } | |||
| // @PostMapping("/upload") | |||
| // public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, | |||
| // @RequestParam("knowledgeGraphId") Long knowledgeGraphId, | |||
| // @RequestParam("version") Integer version) { | |||
| // KnowledgeGraph knowledgeGraph = knowledgeGraphService.queryById(knowledgeGraphId); | |||
| // KnowledgeGraphVersion knowledgeGraphVersion = new KnowledgeGraphVersion(); | |||
| // knowledgeGraphVersion.setKgId(knowledgeGraphId); | |||
| // knowledgeGraphVersion.setVersion(version); | |||
| // knowledgeGraphVersionService.insert(knowledgeGraphVersion); | |||
| // | |||
| // | |||
| // // 构建文件存储路径 | |||
| // String baseDir = env.getProperty("file.upload.base.dir", "/data/knowledgegraph"); | |||
| // LoginUser loginUser = SecurityUtils.getLoginUser(); | |||
| // Path filePath = Paths.get(baseDir, loginUser.getUsername(), knowledgeGraph.getKgName(), version, file.getOriginalFilename()); | |||
| // | |||
| // try { | |||
| // // 确保路径存在,如果不存在则创建 | |||
| // Files.createDirectories(filePath.getParent()); | |||
| // // 保存文件 | |||
| // file.transferTo(filePath.toFile()); | |||
| // } catch (IOException e) { | |||
| // return AjaxResult.error("文件上传失败: " + e.getMessage()); | |||
| // } | |||
| // | |||
| // return AjaxResult.success("文件上传成功,存储路径为: " + filePath.toString()); | |||
| // } | |||
| } | |||
| @@ -43,7 +43,7 @@ public class KnowledgeGraphVersionController { | |||
| * @return 单条数据 | |||
| */ | |||
| @GetMapping("{id}") | |||
| public ResponseEntity<KnowledgeGraphVersion> queryById(@PathVariable("id") Integer id) { | |||
| public ResponseEntity<KnowledgeGraphVersion> queryById(@PathVariable("id") Long id) { | |||
| return ResponseEntity.ok(this.knowledgeGraphVersionService.queryById(id)); | |||
| } | |||
| @@ -76,7 +76,7 @@ public class KnowledgeGraphVersionController { | |||
| * @return 删除是否成功 | |||
| */ | |||
| @DeleteMapping | |||
| public ResponseEntity<Boolean> deleteById(Integer id) { | |||
| public ResponseEntity<Boolean> deleteById(Long id) { | |||
| return ResponseEntity.ok(this.knowledgeGraphVersionService.deleteById(id)); | |||
| } | |||
| @@ -84,7 +84,7 @@ public class KnowledgeGraphVersionController { | |||
| * 增量更新数据 | |||
| */ | |||
| @GetMapping("/incrementalUpdate") | |||
| public ResponseEntity<Boolean> incrementalUpdate(Integer id) { | |||
| public ResponseEntity<Boolean> incrementalUpdate(Long id) { | |||
| return ResponseEntity.ok(this.knowledgeGraphVersionService.incrementalUpdate(id)); | |||
| } | |||
| @@ -1,5 +1,7 @@ | |||
| package com.ruoyi.platform.domain.kg; | |||
| import lombok.Data; | |||
| import java.util.Date; | |||
| import java.io.Serializable; | |||
| @@ -9,10 +11,11 @@ import java.io.Serializable; | |||
| * @author makejava | |||
| * @since 2025-03-11 14:52:09 | |||
| */ | |||
| @Data | |||
| public class KnowledgeGraph implements Serializable { | |||
| private static final long serialVersionUID = 783648356146355028L; | |||
| private Integer id; | |||
| private Long id; | |||
| private String kgName; | |||
| @@ -30,78 +33,5 @@ public class KnowledgeGraph implements Serializable { | |||
| private Integer state; | |||
| public Integer getId() { | |||
| return id; | |||
| } | |||
| public void setId(Integer id) { | |||
| this.id = id; | |||
| } | |||
| public String getKgName() { | |||
| return kgName; | |||
| } | |||
| public void setKgName(String kgName) { | |||
| this.kgName = kgName; | |||
| } | |||
| public String getKgDesc() { | |||
| return kgDesc; | |||
| } | |||
| public void setKgDesc(String kgDesc) { | |||
| this.kgDesc = kgDesc; | |||
| } | |||
| public Integer getKgNoumenon() { | |||
| return kgNoumenon; | |||
| } | |||
| public void setKgNoumenon(Integer kgNoumenon) { | |||
| this.kgNoumenon = kgNoumenon; | |||
| } | |||
| public Date getCreateTime() { | |||
| return createTime; | |||
| } | |||
| public void setCreateTime(Date createTime) { | |||
| this.createTime = createTime; | |||
| } | |||
| public String getCreateBy() { | |||
| return createBy; | |||
| } | |||
| public void setCreateBy(String createBy) { | |||
| this.createBy = createBy; | |||
| } | |||
| public Date getUpdateTime() { | |||
| return updateTime; | |||
| } | |||
| public void setUpdateTime(Date updateTime) { | |||
| this.updateTime = updateTime; | |||
| } | |||
| public String getUpdateBy() { | |||
| return updateBy; | |||
| } | |||
| public void setUpdateBy(String updateBy) { | |||
| this.updateBy = updateBy; | |||
| } | |||
| public Integer getState() { | |||
| return state; | |||
| } | |||
| public void setState(Integer state) { | |||
| this.state = state; | |||
| } | |||
| } | |||
| @@ -1,5 +1,7 @@ | |||
| package com.ruoyi.platform.domain.kg; | |||
| import lombok.Data; | |||
| import java.util.Date; | |||
| import java.io.Serializable; | |||
| @@ -9,20 +11,35 @@ import java.io.Serializable; | |||
| * @author makejava | |||
| * @since 2025-03-11 14:52:10 | |||
| */ | |||
| @Data | |||
| public class KnowledgeGraphVersion implements Serializable { | |||
| private static final long serialVersionUID = -60506903543301209L; | |||
| private Integer id; | |||
| private Long id; | |||
| private String name; | |||
| private Integer version; | |||
| private String transactionId; | |||
| private String version; | |||
| private String content; | |||
| private Integer kgId; | |||
| private String kgId; | |||
| private int status = 0; | |||
| private Integer status; | |||
| private Integer updateMethod; | |||
| private Long entitynum; | |||
| /** | |||
| * 概念个数 | |||
| */ | |||
| private Integer conceptsCount; | |||
| /** | |||
| * 关系个数 | |||
| */ | |||
| private Integer relationsCount; | |||
| private Date updateTime; | |||
| @@ -33,95 +50,5 @@ public class KnowledgeGraphVersion implements Serializable { | |||
| private String updateBy; | |||
| private Integer state; | |||
| public Integer getId() { | |||
| return id; | |||
| } | |||
| public void setId(Integer id) { | |||
| this.id = id; | |||
| } | |||
| public String getVersion() { | |||
| return version; | |||
| } | |||
| public void setVersion(String version) { | |||
| this.version = version; | |||
| } | |||
| public Integer getKgId() { | |||
| return kgId; | |||
| } | |||
| public void setKgId(Integer kgId) { | |||
| this.kgId = kgId; | |||
| } | |||
| public int getStatus() { | |||
| return status; | |||
| } | |||
| public void setStatus(int status) { | |||
| this.status = status; | |||
| } | |||
| public Integer getUpdateMethod() { | |||
| return updateMethod; | |||
| } | |||
| public void setUpdateMethod(Integer updateMethod) { | |||
| this.updateMethod = updateMethod; | |||
| } | |||
| public Long getEntitynum() { | |||
| return entitynum; | |||
| } | |||
| public void setEntitynum(Long entitynum) { | |||
| this.entitynum = entitynum; | |||
| } | |||
| public Date getUpdateTime() { | |||
| return updateTime; | |||
| } | |||
| public void setUpdateTime(Date updateTime) { | |||
| this.updateTime = updateTime; | |||
| } | |||
| public Date getCreateTime() { | |||
| return createTime; | |||
| } | |||
| public void setCreateTime(Date createTime) { | |||
| this.createTime = createTime; | |||
| } | |||
| public String getCreateBy() { | |||
| return createBy; | |||
| } | |||
| public void setCreateBy(String createBy) { | |||
| this.createBy = createBy; | |||
| } | |||
| public String getUpdateBy() { | |||
| return updateBy; | |||
| } | |||
| public void setUpdateBy(String updateBy) { | |||
| this.updateBy = updateBy; | |||
| } | |||
| public Integer getState() { | |||
| return state; | |||
| } | |||
| public void setState(Integer state) { | |||
| this.state = state; | |||
| } | |||
| } | |||
| @@ -13,9 +13,21 @@ public class KgInfoIdDTO { | |||
| @NotNull | |||
| private Long id; | |||
| /** | |||
| * 名字 | |||
| */ | |||
| @NotNull | |||
| private String name; | |||
| /** | |||
| * 版本 | |||
| */ | |||
| @NotNull | |||
| private String version; | |||
| /** | |||
| * 状态 | |||
| */ | |||
| @NotNull | |||
| private String status; | |||
| } | |||
| @@ -1,6 +1,7 @@ | |||
| package com.ruoyi.platform.mapper.kg; | |||
| import com.ruoyi.platform.domain.kg.KnowledgeGraph; | |||
| import org.apache.ibatis.annotations.Mapper; | |||
| import org.apache.ibatis.annotations.Param; | |||
| import org.springframework.data.domain.Pageable; | |||
| @@ -20,7 +21,8 @@ public interface KnowledgeGraphDao { | |||
| * @param id 主键 | |||
| * @return 实例对象 | |||
| */ | |||
| KnowledgeGraph queryById(Integer id); | |||
| @Mapper | |||
| KnowledgeGraph queryById(Long id); | |||
| /** | |||
| * 查询指定行数据 | |||
| @@ -1,6 +1,8 @@ | |||
| package com.ruoyi.platform.mapper.kg; | |||
| import com.ruoyi.platform.domain.kg.KnowledgeGraphVersion; | |||
| import com.ruoyi.platform.domain.kg.dto.KgInfoIdDTO; | |||
| import org.apache.ibatis.annotations.Mapper; | |||
| import org.apache.ibatis.annotations.Param; | |||
| import org.springframework.data.domain.Pageable; | |||
| @@ -12,6 +14,7 @@ import java.util.List; | |||
| * @author makejava | |||
| * @since 2025-03-11 14:52:09 | |||
| */ | |||
| @Mapper | |||
| public interface KnowledgeGraphVersionDao { | |||
| /** | |||
| @@ -20,7 +23,7 @@ public interface KnowledgeGraphVersionDao { | |||
| * @param id 主键 | |||
| * @return 实例对象 | |||
| */ | |||
| KnowledgeGraphVersion queryById(Integer id); | |||
| KnowledgeGraphVersion queryById(Long id); | |||
| /** | |||
| * 查询指定行数据 | |||
| @@ -31,6 +34,14 @@ public interface KnowledgeGraphVersionDao { | |||
| */ | |||
| List<KnowledgeGraphVersion> queryAllByLimit(KnowledgeGraphVersion knowledgeGraphVersion, @Param("pageable") Pageable pageable); | |||
| /** | |||
| * 查询指定行数据 | |||
| * | |||
| * @param kgInfoIdDTO 查询条件 | |||
| * @return 对象列表 | |||
| */ | |||
| List<KnowledgeGraphVersion> queryAll(KgInfoIdDTO kgInfoIdDTO); | |||
| /** | |||
| * 统计总行数 | |||
| * | |||
| @@ -78,7 +89,8 @@ public interface KnowledgeGraphVersionDao { | |||
| * @param id 主键 | |||
| * @return 影响行数 | |||
| */ | |||
| int deleteById(Integer id); | |||
| int deleteById(Long id); | |||
| Integer queryMaxVersion(String kgId); | |||
| } | |||
| @@ -2,7 +2,9 @@ package com.ruoyi.platform.service; | |||
| import com.ruoyi.platform.domain.kg.KgInfo; | |||
| import com.ruoyi.platform.domain.kg.KgInfoPageVo; | |||
| import com.ruoyi.platform.domain.kg.KnowledgeGraphVersion; | |||
| import com.ruoyi.platform.domain.kg.dto.KgInfoIdDTO; | |||
| import org.springframework.web.multipart.MultipartFile; | |||
| import java.util.List; | |||
| @@ -19,5 +21,11 @@ public interface KgInfoService { | |||
| void deleteKgInfoById(Long id); | |||
| List<KgInfo> versionList(KgInfoIdDTO kgInfoIdDTO); | |||
| List<KnowledgeGraphVersion> versionList(KgInfoIdDTO kgInfoIdDTO); | |||
| void uploadCsv(MultipartFile file,String kgId); | |||
| void fullUpdate(Long id); | |||
| void incrementalUpdate(Long id); | |||
| } | |||
| @@ -2,21 +2,62 @@ package com.ruoyi.platform.service.impl; | |||
| import cn.hutool.core.date.DateTime; | |||
| import cn.hutool.core.date.DateUtil; | |||
| import cn.hutool.core.text.csv.*; | |||
| import cn.hutool.core.util.IdUtil; | |||
| import cn.hutool.core.util.ObjectUtil; | |||
| import cn.hutool.json.JSONArray; | |||
| import cn.hutool.json.JSONObject; | |||
| import cn.hutool.json.JSONUtil; | |||
| import com.ruoyi.platform.domain.kg.KgInfo; | |||
| import com.ruoyi.platform.domain.kg.KgInfoPageVo; | |||
| import com.ruoyi.platform.domain.kg.KnowledgeGraphVersion; | |||
| import com.ruoyi.platform.domain.kg.dto.KgInfoIdDTO; | |||
| import com.ruoyi.platform.mapper.kg.KgInfoMapper; | |||
| import com.ruoyi.platform.mapper.kg.KnowledgeGraphVersionDao; | |||
| import com.ruoyi.platform.service.KgInfoService; | |||
| import com.ruoyi.platform.service.kg.FriendsWith; | |||
| import com.ruoyi.platform.service.kg.Person; | |||
| import com.ruoyi.platform.service.kg.PersonRepository; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.data.neo4j.core.Neo4jClient; | |||
| import org.springframework.stereotype.Service; | |||
| import org.springframework.transaction.annotation.Transactional; | |||
| import org.springframework.web.multipart.MultipartFile; | |||
| import java.io.InputStreamReader; | |||
| import java.util.ArrayList; | |||
| import java.util.HashMap; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.zip.CRC32; | |||
| @Service | |||
| public class KgInfoServiceImpl implements KgInfoService { | |||
| @Autowired | |||
| private KgInfoMapper kgInfoMapper; | |||
| @Autowired | |||
| private KnowledgeGraphVersionDao knowledgeGraphVersionDao; | |||
| @Autowired | |||
| private Neo4jClient neo4jClient; | |||
| @Autowired | |||
| private PersonRepository personRepository; | |||
| private static final Logger log = LoggerFactory.getLogger(KgInfoServiceImpl.class); | |||
| // 关系映射配置(可扩展) | |||
| private static final Map<String, String> RELATION_MAPPING = new HashMap<>(); | |||
| static { | |||
| RELATION_MAPPING.put("导演", "DIRECTED_BY"); | |||
| RELATION_MAPPING.put("主演", "ACTED_IN"); | |||
| } | |||
| @Override | |||
| public void insertKgInfo(KgInfo KgInfo) { | |||
| KgInfo.setVersion("1"); | |||
| @@ -28,7 +69,7 @@ public class KgInfoServiceImpl implements KgInfoService { | |||
| @Override | |||
| public void updateKgInfo(KgInfo KgInfo) { | |||
| kgInfoMapper.updateByPrimaryKeySelective(KgInfo) ; | |||
| kgInfoMapper.updateByPrimaryKeySelective(KgInfo); | |||
| } | |||
| @Override | |||
| @@ -48,8 +89,97 @@ public class KgInfoServiceImpl implements KgInfoService { | |||
| } | |||
| @Override | |||
| public List<KgInfo> versionList(KgInfoIdDTO kgInfoIdDTO) { | |||
| return kgInfoMapper.selectByPrimaryKey(kgInfoIdDTO); | |||
| public List<KnowledgeGraphVersion> versionList(KgInfoIdDTO kgInfoIdDTO) { | |||
| return knowledgeGraphVersionDao.queryAll(kgInfoIdDTO); | |||
| } | |||
| @Override | |||
| public void uploadCsv(MultipartFile file, String kgId) { | |||
| try (InputStreamReader reader = new InputStreamReader(file.getInputStream())) { | |||
| // 创建 CsvReader | |||
| CsvReader csvReader = CsvUtil.getReader(); | |||
| // 读取所有行 | |||
| CsvData rows = csvReader.read(reader); | |||
| KnowledgeGraphVersion knowledgeGraphVersion = new KnowledgeGraphVersion(); | |||
| knowledgeGraphVersion.setStatus(0); | |||
| knowledgeGraphVersion.setKgId(kgId); | |||
| knowledgeGraphVersion.setUpdateMethod(0); | |||
| knowledgeGraphVersion.setVersion(maxVersion(kgId)); | |||
| knowledgeGraphVersion.setName(file.getOriginalFilename()); | |||
| knowledgeGraphVersion.setTransactionId(IdUtil.simpleUUID()); | |||
| knowledgeGraphVersion.setContent(JSONUtil.toJsonStr(rows.getRows())); | |||
| knowledgeGraphVersionDao.insert(knowledgeGraphVersion); | |||
| } catch (Exception e) { | |||
| log.error("e={}",e.getMessage()); | |||
| } | |||
| } | |||
| @Override | |||
| @Transactional | |||
| public void fullUpdate(Long id) { | |||
| KnowledgeGraphVersion knowledgeGraphVersion = knowledgeGraphVersionDao.queryById(id); | |||
| String transactionId = knowledgeGraphVersion.getTransactionId(); | |||
| Integer version = knowledgeGraphVersion.getVersion(); | |||
| JSONArray neo4jContents = JSONUtil.parseArray(knowledgeGraphVersion.getContent()); | |||
| List<Object> objects = neo4jContents.subList(1, neo4jContents.size() - 1); | |||
| // 用于存储已解析的 Person 节点 | |||
| Map<String, Person> personMap = new HashMap<>(); | |||
| objects.forEach(item->{ | |||
| JSONArray lineData = JSONUtil.parseArray(item); | |||
| String name = String.valueOf(lineData.get(0)); | |||
| String age = String.valueOf((lineData.get(1))); | |||
| String relationType = String.valueOf(lineData.get(2)); | |||
| String relatedPerson = String.valueOf(lineData.get(3)); | |||
| String since = lineData.get(4) != null ? String.valueOf(lineData.get(4)) : "0"; | |||
| // 创建或获取 Person 节点 | |||
| Person person = personMap.computeIfAbsent(name, k -> new Person(name, age, new ArrayList<>())); | |||
| person.setVersion(version); | |||
| person.setTransactionId(transactionId); | |||
| // 如果存在关系,则创建关系 | |||
| if (relationType != null && relatedPerson != null ) { | |||
| Person related = personMap.computeIfAbsent(relatedPerson, k -> new Person(relatedPerson, "0", new ArrayList<>())); | |||
| FriendsWith relation = new FriendsWith(related, since); | |||
| related.setTransactionId(transactionId); | |||
| related.setVersion(version); | |||
| relation.setTransactionId(transactionId); | |||
| relation.setVersion(version); | |||
| relation.setRelationType(relationType); // 动态设置关系类型 | |||
| person.getRelations().add(relation); | |||
| } | |||
| // 保存 Person 节点 | |||
| personRepository.save(person); | |||
| }); | |||
| KnowledgeGraphVersion online = new KnowledgeGraphVersion(); | |||
| online.setStatus(1); | |||
| online.setUpdateMethod(1); | |||
| online.setId(id); | |||
| knowledgeGraphVersionDao.update(online); | |||
| } | |||
| @Override | |||
| public void incrementalUpdate(Long id) { | |||
| } | |||
| /** | |||
| * 根据名称和事务id生成实体和关系的唯一id | |||
| * @param input | |||
| * @return | |||
| */ | |||
| private Long createUUID(String input) { | |||
| CRC32 crc32 = new CRC32(); | |||
| crc32.update(input.getBytes()); | |||
| return crc32.getValue(); | |||
| } | |||
| private int maxVersion(String kgId) { | |||
| Integer maxVersion =knowledgeGraphVersionDao.queryMaxVersion(kgId); | |||
| return ObjectUtil.isNull(maxVersion)?1:++maxVersion; | |||
| } | |||
| } | |||
| @@ -0,0 +1,29 @@ | |||
| package com.ruoyi.platform.service.kg; | |||
| import lombok.Data; | |||
| import org.springframework.data.neo4j.core.schema.RelationshipId; | |||
| import org.springframework.data.neo4j.core.schema.RelationshipProperties; | |||
| import org.springframework.data.neo4j.core.schema.TargetNode; | |||
| @Data | |||
| @RelationshipProperties | |||
| public class FriendsWith { | |||
| @RelationshipId | |||
| private Long id; | |||
| @TargetNode | |||
| private Person person; | |||
| private String relationType; // 动态关系类型 | |||
| private String since; | |||
| private String transactionId; | |||
| private Integer version; | |||
| // 构造函数、Getter 和 Setter | |||
| public FriendsWith(Person person, String since) { | |||
| this.person = person; | |||
| this.since = since; | |||
| } | |||
| } | |||
| @@ -18,7 +18,7 @@ public interface KnowledgeGraphService { | |||
| * @param id 主键 | |||
| * @return 实例对象 | |||
| */ | |||
| KnowledgeGraph queryById(Integer id); | |||
| KnowledgeGraph queryById(Long id); | |||
| /** | |||
| * 分页查询 | |||
| @@ -18,7 +18,7 @@ public interface KnowledgeGraphVersionService { | |||
| * @param id 主键 | |||
| * @return 实例对象 | |||
| */ | |||
| KnowledgeGraphVersion queryById(Integer id); | |||
| KnowledgeGraphVersion queryById(Long id); | |||
| /** | |||
| * 分页查询 | |||
| @@ -51,7 +51,7 @@ public interface KnowledgeGraphVersionService { | |||
| * @param id 主键 | |||
| * @return 是否成功 | |||
| */ | |||
| boolean deleteById(Integer id); | |||
| boolean deleteById(Long id); | |||
| Boolean incrementalUpdate(Integer id); | |||
| Boolean incrementalUpdate(Long id); | |||
| } | |||
| @@ -0,0 +1,36 @@ | |||
| package com.ruoyi.platform.service.kg; | |||
| import lombok.Data; | |||
| import org.springframework.data.neo4j.core.schema.GeneratedValue; | |||
| import org.springframework.data.neo4j.core.schema.Id; | |||
| import org.springframework.data.neo4j.core.schema.Node; | |||
| import org.springframework.data.neo4j.core.schema.Relationship; | |||
| import java.util.List; | |||
| @Node | |||
| @Data | |||
| public class Person { | |||
| @Id | |||
| @GeneratedValue | |||
| private Long id; | |||
| private String name; | |||
| private String age; | |||
| private String transactionId; | |||
| private Integer version; | |||
| @Relationship(type = "RELATED_TO", direction = Relationship.Direction.OUTGOING) | |||
| private List<FriendsWith> relations; | |||
| // 构造函数、Getter 和 Setter | |||
| public Person(String name, String age,List<FriendsWith> relations) { | |||
| this.name = name; | |||
| this.age = age; | |||
| this.relations = relations; | |||
| } | |||
| } | |||
| @@ -0,0 +1,6 @@ | |||
| package com.ruoyi.platform.service.kg; | |||
| import org.springframework.data.neo4j.repository.Neo4jRepository; | |||
| public interface PersonRepository extends Neo4jRepository<Person, Long> { | |||
| } | |||
| @@ -35,7 +35,7 @@ public class KnowledgeGraphServiceImpl implements KnowledgeGraphService { | |||
| * @return 实例对象 | |||
| */ | |||
| @Override | |||
| public KnowledgeGraph queryById(Integer id) { | |||
| public KnowledgeGraph queryById(Long id) { | |||
| return this.knowledgeGraphDao.queryById(id); | |||
| } | |||
| @@ -16,19 +16,15 @@ import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.data.domain.Page; | |||
| import org.springframework.data.domain.PageImpl; | |||
| import org.springframework.data.domain.PageRequest; | |||
| import org.springframework.data.neo4j.core.Neo4jTemplate; | |||
| import org.springframework.data.neo4j.core.Neo4jClient; | |||
| import org.springframework.stereotype.Service; | |||
| import javax.annotation.Resource; | |||
| import java.io.FileReader; | |||
| import java.io.IOException; | |||
| import java.io.*; | |||
| import java.nio.file.Files; | |||
| import java.nio.file.Path; | |||
| import java.nio.file.Paths; | |||
| import java.util.ArrayList; | |||
| import java.util.HashMap; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.*; | |||
| /** | |||
| * (KnowledgeGraphVersion)表服务实现类 | |||
| @@ -45,7 +41,7 @@ public class KnowledgeGraphVersionServiceImpl implements KnowledgeGraphVersionSe | |||
| private KnowledgeGraphService knowledgeGraphService; | |||
| @Autowired | |||
| private Neo4jTemplate neo4jTemplate; | |||
| private Neo4jClient neo4jClient; | |||
| /** | |||
| * 通过ID查询单条数据 | |||
| @@ -54,7 +50,7 @@ public class KnowledgeGraphVersionServiceImpl implements KnowledgeGraphVersionSe | |||
| * @return 实例对象 | |||
| */ | |||
| @Override | |||
| public KnowledgeGraphVersion queryById(Integer id) { | |||
| public KnowledgeGraphVersion queryById(Long id) { | |||
| return this.knowledgeGraphVersionDao.queryById(id); | |||
| } | |||
| @@ -102,29 +98,29 @@ public class KnowledgeGraphVersionServiceImpl implements KnowledgeGraphVersionSe | |||
| * @return 是否成功 | |||
| */ | |||
| @Override | |||
| public boolean deleteById(Integer id) { | |||
| public boolean deleteById(Long id) { | |||
| return this.knowledgeGraphVersionDao.deleteById(id) > 0; | |||
| } | |||
| @Override | |||
| public Boolean incrementalUpdate(Integer id) { | |||
| // 找到文件路径 | |||
| KnowledgeGraphVersion knowledgeGraphVersion = knowledgeGraphVersionDao.queryById(id); | |||
| KnowledgeGraph knowledgeGraph = knowledgeGraphService.queryById(knowledgeGraphVersion.getKgId()); | |||
| LoginUser loginUser = SecurityUtils.getLoginUser(); | |||
| // 构建文件路径 | |||
| String filePath = "/data/knowledgegraph/" + loginUser.getUsername() + "/" + knowledgeGraph.getKgName() + "/" + knowledgeGraphVersion.getVersion(); | |||
| String label = knowledgeGraph.getKgName(); | |||
| // 读取CSV文件并解析数据 | |||
| List<CSVRecord> records = readCSVFile(filePath); | |||
| // 增量更新到Neo4j | |||
| incrementalUpdate(records, label); | |||
| public Boolean incrementalUpdate(Long id) { | |||
| // // 找到文件路径 | |||
| // KnowledgeGraphVersion knowledgeGraphVersion = knowledgeGraphVersionDao.queryById(id); | |||
| // KnowledgeGraph knowledgeGraph = knowledgeGraphService.queryById(knowledgeGraphVersion.getKgId()); | |||
| // LoginUser loginUser = SecurityUtils.getLoginUser(); | |||
| // | |||
| // // 构建文件路径 | |||
| // String filePath = "C://data/knowledgegraph/" + knowledgeGraph.getKgName() + "_" + knowledgeGraphVersion.getVersion() + ".csv"; | |||
| // String label = knowledgeGraph.getKgName(); | |||
| // | |||
| // // 读取CSV文件并解析数据 | |||
| // List<CSVRecord> records = readCSVFile(filePath); | |||
| // | |||
| // // 增量更新到Neo4j | |||
| // incrementalUpdate(records, label); | |||
| // 备份数据 | |||
| backupNeo4jData(label, knowledgeGraph.getKgName() + "_" + knowledgeGraphVersion.getVersion()); | |||
| // backupNeo4jData(label, knowledgeGraph.getKgName() + "_" + knowledgeGraphVersion.getVersion()); | |||
| return true; | |||
| } | |||
| @@ -194,18 +190,20 @@ public class KnowledgeGraphVersionServiceImpl implements KnowledgeGraphVersionSe | |||
| } | |||
| query.append("})").append(setClause); | |||
| Driver driver = GraphDatabase.driver("bolt://172.20.32.88:7687", AuthTokens.basic("neo4j", "12345678")); | |||
| Session session = driver.session(); | |||
| Result result = session.run(query.toString(),properties); | |||
| // 执行MERGE操作 | |||
| // neo4jTemplate.execute(query.toString(), properties); | |||
| // | |||
| // Driver driver = GraphDatabase.driver("bolt://172.20.32.9:7687", AuthTokens.basic("neo4j", "12345678")); | |||
| // Session session = driver.session(); | |||
| // Result result = session.run(query.toString(), properties); | |||
| // 执行MERGE操作dataset_dependcy | |||
| query = new StringBuilder("MATCH (n)-[r]->(m) RETURN n, r, m"); | |||
| Collection<Map<String, Object>> all = neo4jClient.query(query.toString()).fetch().all(); | |||
| System.out.printf("", all); | |||
| } | |||
| private void backupNeo4jData(String label, String backupName) { | |||
| try (Driver driver = GraphDatabase.driver("bolt://172.20.32.88:7687", AuthTokens.basic("neo4j", "12345678"))) { | |||
| try (Driver driver = GraphDatabase.driver("bolt://172.20.32.9:7687", AuthTokens.basic("neo4j", "12345678"))) { | |||
| Session session = driver.session(); | |||
| // 查询所有带有指定标签的节点 | |||
| @@ -213,15 +211,15 @@ public class KnowledgeGraphVersionServiceImpl implements KnowledgeGraphVersionSe | |||
| Result result = session.run(query); | |||
| // 将节点数据写入备份文件 | |||
| Path backupPath = Paths.get("/data/knowledgegraph/backup/" + backupName + ".csv"); | |||
| Path backupPath = Paths.get("C://data/knowledgegraph/backup/" + backupName + ".csv"); | |||
| StringBuilder backupContent = new StringBuilder("name,age,city\n"); | |||
| while (result.hasNext()) { | |||
| Record record = result.next(); | |||
| Node node = record.get("n").asNode(); | |||
| backupContent.append(node.get("name").asString()).append(",") | |||
| .append(node.get("age").asInt()).append(",") | |||
| .append(node.get("city").asString()).append("\n"); | |||
| backupContent.append(node.get("Unname").asString()).append(",") | |||
| .append(node.get("Year").asString()).append(",") | |||
| .append(node.get("Entity").asString()).append("\n"); | |||
| } | |||
| Files.write(backupPath, backupContent.toString().getBytes()); | |||
| @@ -16,7 +16,7 @@ | |||
| <!--查询单个--> | |||
| <select id="queryById" resultMap="KnowledgeGraphMap"> | |||
| select idkg_namekg_desckg_noumenoncreate_timecreate_byupdate_timeupdate_bystate | |||
| select id,kg_name,kg_desc,kg_noumenon,create_time,create_by,update_time,update_by,state | |||
| from knowledge_graph | |||
| where id = #{id} | |||
| </select> | |||
| @@ -4,11 +4,15 @@ | |||
| <resultMap type="com.ruoyi.platform.domain.kg.KnowledgeGraphVersion" id="KnowledgeGraphVersionMap"> | |||
| <result property="id" column="id" jdbcType="INTEGER"/> | |||
| <result property="version" column="version" jdbcType="VARCHAR"/> | |||
| <result property="kgId" column="kg_id" jdbcType="INTEGER"/> | |||
| <result property="name" column="name" jdbcType="VARCHAR"/> | |||
| <result property="version" column="version" jdbcType="INTEGER"/> | |||
| <result property="transactionId" column="transaction_id" jdbcType="VARCHAR"/> | |||
| <result property="content" column="content" jdbcType="VARCHAR"/> | |||
| <result property="kgId" column="kg_id" jdbcType="VARCHAR"/> | |||
| <result property="status" column="status" jdbcType="VARCHAR"/> | |||
| <result property="updateMethod" column="update_method" jdbcType="INTEGER"/> | |||
| <result property="entitynum" column="entityNum" jdbcType="INTEGER"/> | |||
| <result property="conceptsCount" column="concepts_count" jdbcType="INTEGER"/> | |||
| <result property="relationsCount" column="relations_count" jdbcType="INTEGER"/> | |||
| <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/> | |||
| <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/> | |||
| <result property="createBy" column="create_by" jdbcType="VARCHAR"/> | |||
| @@ -18,7 +22,7 @@ | |||
| <!--查询单个--> | |||
| <select id="queryById" resultMap="KnowledgeGraphVersionMap"> | |||
| select idversionkg_idstatusupdate_methodentityNumupdate_timecreate_timecreate_byupdate_bystate | |||
| select id,name,content,version,transaction_id,kg_id,status,update_method,concepts_count,relations_count,update_time,create_time,create_by,update_by,state | |||
| from knowledge_graph_version | |||
| where id = #{id} | |||
| </select> | |||
| @@ -26,7 +30,7 @@ | |||
| <!--查询指定行数据--> | |||
| <select id="queryAllByLimit" resultMap="KnowledgeGraphVersionMap"> | |||
| select | |||
| idversionkg_idstatusupdate_methodentityNumupdate_timecreate_timecreate_byupdate_bystate | |||
| id,name,content,version,transaction_id,kg_id,status,update_method,concepts_count,relations_count,update_time,create_time,create_by,update_by,state | |||
| from knowledge_graph_version | |||
| <where> | |||
| <if test="id != null"> | |||
| @@ -35,6 +39,9 @@ | |||
| <if test="version != null and version != ''"> | |||
| and version = #{version} | |||
| </if> | |||
| <if test="transactionId != null and transactionId != ''"> | |||
| and transaction_id = #{transactionId} | |||
| </if> | |||
| <if test="kgId != null"> | |||
| and kg_id = #{kgId} | |||
| </if> | |||
| @@ -44,8 +51,11 @@ | |||
| <if test="updateMethod != null"> | |||
| and update_method = #{updateMethod} | |||
| </if> | |||
| <if test="entitynum != null"> | |||
| and entityNum = #{entitynum} | |||
| <if test="conceptsCount != null"> | |||
| and concepts_count = #{conceptsCount} | |||
| </if> | |||
| <if test="relationsCount != null"> | |||
| and relations_count = #{relationsCount} | |||
| </if> | |||
| <if test="updateTime != null"> | |||
| and update_time = #{updateTime} | |||
| @@ -65,6 +75,19 @@ | |||
| </where> | |||
| limit #{pageable.offset}, #{pageable.pageSize} | |||
| </select> | |||
| <select id="queryAll" resultMap="KnowledgeGraphVersionMap"> | |||
| select | |||
| id,name,content,version,transaction_id,kg_id,status,update_method,concepts_count,relations_count,update_time,create_time,create_by,update_by,state | |||
| from knowledge_graph_version | |||
| <where> | |||
| <if test="name != null"> | |||
| and `name` = #{name} | |||
| </if> | |||
| <if test="status != null and status != ''"> | |||
| and status = #{status} | |||
| </if> | |||
| </where> | |||
| </select> | |||
| <!--统计总行数--> | |||
| <select id="count" resultType="java.lang.Long"> | |||
| @@ -86,8 +109,11 @@ | |||
| <if test="updateMethod != null"> | |||
| and update_method = #{updateMethod} | |||
| </if> | |||
| <if test="entitynum != null"> | |||
| and entityNum = #{entitynum} | |||
| <if test="conceptsCount != null"> | |||
| and concepts_count = #{conceptsCount} | |||
| </if> | |||
| <if test="relationsCount != null"> | |||
| and relations_count = #{relationsCount} | |||
| </if> | |||
| <if test="updateTime != null"> | |||
| and update_time = #{updateTime} | |||
| @@ -109,25 +135,25 @@ | |||
| <!--新增所有列--> | |||
| <insert id="insert" keyProperty="id" useGeneratedKeys="true"> | |||
| insert into knowledge_graph_version(versionkg_idstatusupdate_methodentityNumupdate_timecreate_timecreate_byupdate_bystate) | |||
| values (#{version}#{kgId}#{status}#{updateMethod}#{entitynum}#{updateTime}#{createTime}#{createBy}#{updateBy}#{state}) | |||
| insert into knowledge_graph_version(name,content,version,transaction_id,kg_id,status,update_method,concepts_count,relations_count,update_time,create_time,create_by,update_by,state) | |||
| values (#{name},#{content},#{version},#{transactionId},#{kgId},#{status},#{updateMethod},#{conceptsCount},#{relationsCount},#{updateTime},#{createTime},#{createBy},#{updateBy},#{state}) | |||
| </insert> | |||
| <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true"> | |||
| insert into | |||
| knowledge_graph_version(versionkg_idstatusupdate_methodentityNumupdate_timecreate_timecreate_byupdate_bystate) | |||
| knowledge_graph_version(name,content,version,transaction_id,kg_id,status,update_method,concepts_count,relations_count,update_time,create_time,create_by,update_by,state) | |||
| values | |||
| <foreach collection="entities" item="entity" separator=","> | |||
| (#{entity.version}#{entity.kgId}#{entity.status}#{entity.updateMethod}#{entity.entitynum}#{entity.updateTime}#{entity.createTime}#{entity.createBy}#{entity.updateBy}#{entity.state}) | |||
| (#{entity.name},#{entity.content},#{entity.version},#{entity.transactionId},#{entity.kgId},#{entity.status},#{entity.updateMethod},#{entity.conceptsCount},#{entity.relationsCount},#{entity.updateTime},#{entity.createTime},#{entity.createBy},#{entity.updateBy},#{entity.state}) | |||
| </foreach> | |||
| </insert> | |||
| <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true"> | |||
| insert into | |||
| knowledge_graph_version(versionkg_idstatusupdate_methodentityNumupdate_timecreate_timecreate_byupdate_bystate) | |||
| knowledge_graph_version(name,content,version,transaction_id,kg_id,status,update_method,concepts_count,relations_count,update_time,create_time,create_by,update_by,state) | |||
| values | |||
| <foreach collection="entities" item="entity" separator=","> | |||
| (#{entity.version}#{entity.kgId}#{entity.status}#{entity.updateMethod}#{entity.entitynum}#{entity.updateTime}#{entity.createTime}#{entity.createBy}#{entity.updateBy}#{entity.state}) | |||
| (#{entity.name},#{entity.version},#{entity.kgId},#{entity.status},#{entity.updateMethod},#{entity.entityNum},#{entity.updateTime},#{entity.createTime},#{entity.createBy},#{entity.updateBy},#{entity.state}) | |||
| </foreach> | |||
| on duplicate key update | |||
| version = values(version)kg_id = values(kg_id)status = values(status)update_method = | |||
| @@ -142,6 +168,9 @@ | |||
| <if test="version != null and version != ''"> | |||
| version = #{version}, | |||
| </if> | |||
| <if test="name != null and version != ''"> | |||
| `name` = #{name}, | |||
| </if> | |||
| <if test="kgId != null"> | |||
| kg_id = #{kgId}, | |||
| </if> | |||
| @@ -151,8 +180,11 @@ | |||
| <if test="updateMethod != null"> | |||
| update_method = #{updateMethod}, | |||
| </if> | |||
| <if test="entitynum != null"> | |||
| entityNum = #{entitynum}, | |||
| <if test="conceptsCount != null"> | |||
| and concepts_count = #{conceptsCount} | |||
| </if> | |||
| <if test="relationsCount != null"> | |||
| and relations_count = #{relationsCount} | |||
| </if> | |||
| <if test="updateTime != null"> | |||
| update_time = #{updateTime}, | |||
| @@ -180,5 +212,8 @@ | |||
| where id = #{id} | |||
| </delete> | |||
| <select id="queryMaxVersion" resultType="java.lang.Integer"> | |||
| select max(version) from knowledge_graph_version where kg_id=#{kgId}; | |||
| </select> | |||
| </mapper> | |||