|
@@ -0,0 +1,131 @@
|
|
|
+package com.qizhen.healsphere;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.qizhen.healsphere.config.Neo4jUtil;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.neo4j.driver.v1.Session;
|
|
|
+import org.neo4j.driver.v1.StatementResult;
|
|
|
+import org.neo4j.driver.v1.Values;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.context.annotation.ComponentScan;
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+@ComponentScan(basePackages = {"com.qizhen.healsphere"})
|
|
|
+@SpringBootTest
|
|
|
+public class Tree2Neo4jTest {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private Neo4jUtil neo4jUtil;
|
|
|
+
|
|
|
+ private static final String JSON_FOLDER = "/Users/ycw/dev/json_file";
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void mergeData() {
|
|
|
+ try {
|
|
|
+ processAllJsonFiles();
|
|
|
+ System.out.println("数据处理完成");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void processAllJsonFiles() {
|
|
|
+ File folder = new File(JSON_FOLDER);
|
|
|
+ File[] files = folder.listFiles((dir, name) -> name.endsWith(".json"));
|
|
|
+ if (files != null) {
|
|
|
+ try (Session session = neo4jUtil.getSession()) {
|
|
|
+ for (File file : files) {
|
|
|
+ processJsonFile(file, session);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void processJsonFile(File jsonFile, Session session) {
|
|
|
+ try {
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ TreeNode root = mapper.readValue(jsonFile, TreeNode.class);
|
|
|
+ processNode(root, null, session, 1);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void processNode(TreeNode node, Long parentId, Session session, int level) {
|
|
|
+ System.out.println("=======当前时间:" + DateUtil.formatDateTime(new Date()) + ", 处理节点名称:" + node.getName() + "=======");
|
|
|
+ // 根据层级设置不同的标签和属性
|
|
|
+ String label = determineLabel(level);
|
|
|
+
|
|
|
+ //先查询节点名称是否存在
|
|
|
+ String checkNodeQuery = String.format("MATCH (n:%s {name: $name}) RETURN n", label);
|
|
|
+ StatementResult result = session.run(checkNodeQuery, Values.parameters("name", node.getName()));
|
|
|
+ Long nodeId = null;
|
|
|
+ if (result.hasNext()) {
|
|
|
+ nodeId = result.next().get("n").asNode().id();
|
|
|
+ }else{
|
|
|
+ String createNodeQuery = String.format("MERGE (n:%s {source_id: $id, name: $name,source_type: 'diagbotcloud'}) RETURN id(n)", label);
|
|
|
+ session.run(createNodeQuery, Values.parameters(
|
|
|
+ "id", node.getId(),
|
|
|
+ "name", node.getName()
|
|
|
+ ));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有父节点,创建关系
|
|
|
+ if (parentId != null) {
|
|
|
+ String relationshipType = determineRelationshipType(level);
|
|
|
+ if (nodeId == null){
|
|
|
+ String createRelationQuery =
|
|
|
+ String.format("MATCH (parent {source_id: $parentId}), (child {source_id: $childId}) " +
|
|
|
+ "MERGE (parent)-[:%s]->(child)", relationshipType);
|
|
|
+ session.run(createRelationQuery, Values.parameters(
|
|
|
+ "parentId", parentId,
|
|
|
+ "childId", node.getId()
|
|
|
+ ));
|
|
|
+ }else {
|
|
|
+ String createRelationQuery =
|
|
|
+ String.format("MATCH (parent {source_id: $parentId}), (child) WHERE id(child)= $childId " +
|
|
|
+ "MERGE (parent)-[:%s]->(child)", relationshipType);
|
|
|
+ session.run(createRelationQuery, Values.parameters(
|
|
|
+ "parentId", parentId,
|
|
|
+ "childId", nodeId
|
|
|
+ ));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 递归处理子节点
|
|
|
+ if (node.getNextTree() != null) {
|
|
|
+ for (TreeNode child : node.getNextTree()) {
|
|
|
+ processNode(child, node.getId(), session, level + 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String determineLabel(int level) {
|
|
|
+ if (level <= 2) {
|
|
|
+ return "药品类别";
|
|
|
+ } else {
|
|
|
+ return "药品";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String determineRelationshipType(int level) {
|
|
|
+ if (level <= 2) {
|
|
|
+ return "药品类别相关子类";
|
|
|
+ } else {
|
|
|
+ return "药品类别相关子类药品";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|