Commit 2fd8c237 by mahx

修改TempNode创建

parent 645ccfa1
...@@ -550,6 +550,7 @@ public class MetaDataController { ...@@ -550,6 +550,7 @@ public class MetaDataController {
logger.info("开始同步元数据:从mongo到neo4j"); logger.info("开始同步元数据:从mongo到neo4j");
String flag = "success"; String flag = "success";
metadataService.syschroRelationFromMongo(catalogName); metadataService.syschroRelationFromMongo(catalogName);
metadataService.deleteTempNodeByCatalogName(catalogName);
metadataService.createTempNode(catalogName); metadataService.createTempNode(catalogName);
return flag; return flag;
} }
...@@ -560,8 +561,8 @@ public class MetaDataController { ...@@ -560,8 +561,8 @@ public class MetaDataController {
} }
@PostMapping("/removeTempRelation") @PostMapping("/removeTempRelation")
public void removeTempRelation(@RequestParam String sqlId, @RequestParam String catalogName) { public void removeTempRelation(@RequestParam String etlScriptId, @RequestParam String catalogName) {
mongoDbService.removeTempRelation(sqlId, catalogName); mongoDbService.removeTempRelation(etlScriptId, catalogName);
} }
@PostMapping("/createTempNode") @PostMapping("/createTempNode")
...@@ -570,8 +571,8 @@ public class MetaDataController { ...@@ -570,8 +571,8 @@ public class MetaDataController {
} }
@PostMapping("/deleteTempNode") @PostMapping("/deleteTempNode")
public void deleteTempNode(@RequestParam String sqlId) { public void deleteTempNode(@RequestParam String catalogName) {
metadataService.deleteTempNode(sqlId); metadataService.deleteTempNodeByCatalogName(catalogName);
} }
......
...@@ -10,16 +10,46 @@ import org.neo4j.ogm.annotation.NodeEntity; ...@@ -10,16 +10,46 @@ import org.neo4j.ogm.annotation.NodeEntity;
@NodeEntity(label = "TempNode") @NodeEntity(label = "TempNode")
public class TempNode extends BaseNode { public class TempNode extends BaseNode {
private String sqlId;
private String metadataId; private String metadataId;
public String getSqlId() { private String etlScriptId;
return sqlId;
private String etlJobId;
private String catalogName;
private Long parentId;
public String getEtlScriptId() {
return etlScriptId;
}
public void setEtlScriptId(String etlScriptId) {
this.etlScriptId = etlScriptId;
}
public String getEtlJobId() {
return etlJobId;
}
public void setEtlJobId(String etlJobId) {
this.etlJobId = etlJobId;
}
public String getCatalogName() {
return catalogName;
}
public void setCatalogName(String catalogName) {
this.catalogName = catalogName;
}
public Long getParentId() {
return parentId;
} }
public void setSqlId(String sqlId) { public void setParentId(Long parentId) {
this.sqlId = sqlId; this.parentId = parentId;
} }
public String getMetadataId() { public String getMetadataId() {
......
package com.keymobile.metadata.metadataRelation.respository; package com.keymobile.metadata.metadataRelation.respository;
import java.util.List;
import com.keymobile.metadata.metadataRelation.pojo.TempNode; import com.keymobile.metadata.metadataRelation.pojo.TempNode;
import org.springframework.data.neo4j.annotation.Query; import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository; import org.springframework.data.neo4j.repository.Neo4jRepository;
...@@ -12,8 +14,14 @@ import org.springframework.data.repository.query.Param; ...@@ -12,8 +14,14 @@ import org.springframework.data.repository.query.Param;
*/ */
public interface TempNodeRepository extends Neo4jRepository<TempNode, Long> { public interface TempNodeRepository extends Neo4jRepository<TempNode, Long> {
Long deleteTempNodesBySqlId(String sqlId); Long deleteTempNodesByEtlScriptId(String etlScriptId);
Long deleteTempNodesByEtlJobId(String etlJobId);
Long deleteTempNodesByCatalogName(String catalogName);
@Query("match (source:BaseNode)-[r]-(target:BaseNode) where source.catalogName=$catalogName or target.catalogName=$catalogName delete r")
Long deleteAllRelation(@Param("catalogName") String catalogName);
@Query("match (source:BaseNode)-[r]-(target:BaseNode) where source.sqlId=$sqlId or target.sqlId=$sqlId delete r") List<TempNode> findByCatalogNameAndEtlJobIdAndParentIdAndName(String catalogName, String etlJobId, Long parentId, String name);
Long deleteAllRelation(@Param("sqlId") String sqlId);
} }
...@@ -4,7 +4,6 @@ import com.keymobile.metadata.metadataRelation.pojo.BaseRelationship; ...@@ -4,7 +4,6 @@ import com.keymobile.metadata.metadataRelation.pojo.BaseRelationship;
import com.keymobile.metadata.metadataRelation.pojo.MetaData; import com.keymobile.metadata.metadataRelation.pojo.MetaData;
import com.keymobile.metadata.metadataRelation.pojo.TempNode; import com.keymobile.metadata.metadataRelation.pojo.TempNode;
import com.keymobile.metadata.metadataRelation.pojo.returnBean.ReturnReslult; import com.keymobile.metadata.metadataRelation.pojo.returnBean.ReturnReslult;
import org.springframework.data.mongodb.core.query.Meta;
import java.util.List; import java.util.List;
...@@ -73,7 +72,7 @@ public interface IMetadataService { ...@@ -73,7 +72,7 @@ public interface IMetadataService {
void createTempNode(String catalogName); void createTempNode(String catalogName);
void deleteTempNode(String sqlId); void deleteTempNodeByCatalogName(String catalogName);
List<TempNode> findAllTempNode(); List<TempNode> findAllTempNode();
} }
...@@ -22,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -22,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.*; import java.util.*;
...@@ -404,27 +405,38 @@ public class MetadataServiceImpl implements IMetadataService { ...@@ -404,27 +405,38 @@ public class MetadataServiceImpl implements IMetadataService {
return labels; return labels;
} }
private Long createNode(Map<String, Long> nameIdMap, String sqlId, String name, boolean isTable) { private Long createNode(Map<String, Long> nameIdMap, String catalogName, String etlJobId, String etlScriptId, String name, boolean isTable, Long parentId) {
Long nodeId = nameIdMap.get(name); String identifier = String.format("%s,%s,%s", etlJobId, parentId, name);
Long nodeId = nameIdMap.get(identifier);
if (nodeId == null) { if (nodeId == null) {
String[] split = StringUtils.split(name, '.'); String[] split = StringUtils.split(name, '.');
if (split.length == 2) { if (split.length == 2) {
String tableName = split[0]; String tableName = split[0];
String columnName = split[1]; String columnName = split[1];
Long tableId = createNode(nameIdMap, sqlId, tableName, true); Long tableId = createNode(nameIdMap, catalogName, etlJobId, etlScriptId, tableName, true, null);
nodeId = createNode(nameIdMap, sqlId, columnName, false); nodeId = createNode(nameIdMap, catalogName, etlJobId, etlScriptId, columnName, false, tableId);
if (tableId != null && nodeId != null) { if (tableId != null && nodeId != null) {
compositionRelationRespository.mergeConpositionRelation(tableId, nodeId); compositionRelationRespository.mergeConpositionRelation(tableId, nodeId);
} }
} else { } else {
TempNode tempNode = new TempNode(); TempNode tempNode;
String modelName = isTable ? "TempTable" : "TempColumn"; List<TempNode> list = tempNodeRepository.findByCatalogNameAndEtlJobIdAndParentIdAndName(catalogName, etlJobId, parentId, name);
tempNode.setMetadataId(modelName + "=" + UUID.randomUUID()); if (!CollectionUtils.isEmpty(list)) {
tempNode.setName(name); tempNode = list.get(0);
tempNode.setSqlId(sqlId); } else {
tempNode = new TempNode();
String modelName = isTable ? "TempTable" : "TempColumn";
tempNode.setMetadataId(modelName + "=" + UUID.randomUUID());
tempNode.setCatalogName(catalogName);
tempNode.setEtlJobId(etlJobId);
tempNode.setEtlScriptId(etlScriptId);
tempNode.setName(name);
}
tempNode.setParentId(parentId);
tempNode = tempNodeRepository.save(tempNode); tempNode = tempNodeRepository.save(tempNode);
nodeId = tempNode.getId(); nodeId = tempNode.getId();
nameIdMap.put(name, nodeId); nameIdMap.put(identifier, nodeId);
} }
} }
return nodeId; return nodeId;
...@@ -434,23 +446,21 @@ public class MetadataServiceImpl implements IMetadataService { ...@@ -434,23 +446,21 @@ public class MetadataServiceImpl implements IMetadataService {
public void createTempNode(String catalogName) { public void createTempNode(String catalogName) {
int page = 0, pageSize = 500; int page = 0, pageSize = 500;
long totalElement = mongoDbServiceImpl.countTempRelation(PageRequest.of(page, pageSize), catalogName); long totalElement = mongoDbServiceImpl.countTempRelation(PageRequest.of(page, pageSize), catalogName);
Map<String, Long> sqlIdNodeIdMap = new HashMap<>();
Map<String, Long> nameIdMap = new HashMap<>(); Map<String, Long> nameIdMap = new HashMap<>();
for (; page < totalElement; page += pageSize) { for (; page < totalElement; page += pageSize) {
List<JSONObject> list = mongoDbServiceImpl.findTempRelationByPage(PageRequest.of(page, pageSize, Sort.by("sqlId")), catalogName); List<JSONObject> list = mongoDbServiceImpl.findTempRelationByPage(PageRequest.of(page, pageSize, Sort.by("etlJobId")), catalogName);
for (JSONObject relation : list) { for (JSONObject relation : list) {
String sqlId = relation.getString("sqlId"); String sqlId = relation.getString("etlSqlId");
deleteTempNode(sqlId); String etlScriptId = relation.getString("etlScriptId");
String etlJobId = relation.getString("etlJobId");
deleteTempNodeByCatalogName(sqlId);
MetaData metadata = metadataRepository.findMetaDataByMetadataId(sqlId); MetaData metadata = metadataRepository.findMetaDataByMetadataId(sqlId);
if (metadata == null) { if (metadata == null) {
logger.error("id : {} is not found", sqlId); logger.error("id : {} is not found", sqlId);
continue; continue;
} }
Long id = metadata.getId(); Long id = metadata.getId();
if (!sqlIdNodeIdMap.containsKey(sqlId)) {
sqlIdNodeIdMap.put(sqlId, id);
nameIdMap = new HashMap<>();
}
String source = relation.getString("source"); String source = relation.getString("source");
String target = relation.getString("target"); String target = relation.getString("target");
if (StringUtils.isBlank(source) || StringUtils.isBlank(target)) { if (StringUtils.isBlank(source) || StringUtils.isBlank(target)) {
...@@ -459,13 +469,13 @@ public class MetadataServiceImpl implements IMetadataService { ...@@ -459,13 +469,13 @@ public class MetadataServiceImpl implements IMetadataService {
String type = relation.getString("type"); String type = relation.getString("type");
if ("Input".equalsIgnoreCase(type)) { if ("Input".equalsIgnoreCase(type)) {
Long nodeId = createNode(nameIdMap, sqlId, source, false); Long nodeId = createNode(nameIdMap, catalogName, etlJobId, etlScriptId, source, false, null);
if (nodeId != null) { if (nodeId != null) {
inputRelationReRepository.mergeInputRelation(nodeId, id); inputRelationReRepository.mergeInputRelation(nodeId, id);
} }
} }
if ("Output".equalsIgnoreCase(type)) { if ("Output".equalsIgnoreCase(type)) {
Long nodeId = createNode(nameIdMap, sqlId, target, false); Long nodeId = createNode(nameIdMap, catalogName, etlJobId, etlScriptId, target, false, null);
if (nodeId != null) { if (nodeId != null) {
outputRelationRepository.mergeOutRelation(id, nodeId); outputRelationRepository.mergeOutRelation(id, nodeId);
} }
...@@ -475,9 +485,9 @@ public class MetadataServiceImpl implements IMetadataService { ...@@ -475,9 +485,9 @@ public class MetadataServiceImpl implements IMetadataService {
} }
@Override @Override
public void deleteTempNode(String sqlId) { public void deleteTempNodeByCatalogName(String catalogName) {
tempNodeRepository.deleteAllRelation(sqlId); tempNodeRepository.deleteAllRelation(catalogName);
tempNodeRepository.deleteTempNodesBySqlId(sqlId); tempNodeRepository.deleteTempNodesByCatalogName(catalogName);
} }
@Override @Override
......
...@@ -60,8 +60,8 @@ public class MongoDbServiceImpl { ...@@ -60,8 +60,8 @@ public class MongoDbServiceImpl {
list.forEach(o -> mongoTemplate.save(o, PREFIX_MD_RELATION_TEMP_NODE + catalogName)); list.forEach(o -> mongoTemplate.save(o, PREFIX_MD_RELATION_TEMP_NODE + catalogName));
} }
public void removeTempRelation(String sqlId, String catalogName) { public void removeTempRelation(String etlScriptId, String catalogName) {
Query query = Query.query(Criteria.where("sqlId").is(sqlId)); Query query = Query.query(Criteria.where("etlScriptId").is(etlScriptId));
mongoTemplate.remove(query, PREFIX_MD_RELATION_TEMP_NODE + catalogName); mongoTemplate.remove(query, PREFIX_MD_RELATION_TEMP_NODE + catalogName);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment