Commit 94a59521 authored by zhengfunan's avatar zhengfunan

stater版首次提交

parents
Pipeline #384 failed with stages
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="corretto-1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="67e936c8-2d55-4655-871e-e6e153e4d8e1" name="更改" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/uiDesigner.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/com/tongda/tddopo/auto/Neo4jAutoConfiguration.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/com/tongda/tddopo/config/Neo4jProperties.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/com/tongda/tddopo/neo4j/Neo4jServer.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/com/tongda/tddopo/service/TDGraphService.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/com/tongda/tddopo/tasks/SynchronizeMetadata.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/com/tongda/tddopo/util/GraphUtil.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/resources/META-INF/spring.factories" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="MavenImportPreferences">
<option name="importingSettings">
<MavenImportingSettings>
<option name="workspaceImportEnabled" value="true" />
</MavenImportingSettings>
</option>
</component>
<component name="ProjectId" id="2UeUXtwjbhwh54lBi8nJQtzpQb7" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"WebServerToolWindowFactoryState": "false",
"last_opened_file_path": "D:/ideawork/stater-tdtopo",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"spring.configuration.checksum": "35834eaec7657be6760ebdbe364857eb",
"vue.rearranger.settings.migration": "true"
}
}]]></component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="D:\ideawork\stater-tddopo\src\main\java\com\tongda\tddopo" />
<recent name="D:\ideawork\stater-tddopo\src\main\resources" />
<recent name="D:\ideawork\stater-tddopo\src\main\java\com\tongda" />
</key>
<key name="CopyClassDialog.RECENTS_KEY">
<recent name="com.tongda.tddopo" />
</key>
</component>
<component name="RunManager">
<configuration name="Test" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="com.tongda.tddopo.test.Test" />
<module name="stater-tdtopo" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.tongda.tddopo.test.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="应用程序.Test" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="应用程序级" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="默认任务">
<changelist id="67e936c8-2d55-4655-871e-e6e153e4d8e1" name="更改" comment="" />
<created>1693299583537</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1693299583537</updated>
<workItem from="1693299584659" duration="7284000" />
<workItem from="1693363826067" duration="10263000" />
<workItem from="1693386223381" duration="1313000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tongda</groupId>
<artifactId>stater-tdtopo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<!-- <version>5.9.0</version>-->
<version>3.5.30</version>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.32</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.tongda.tddopo.auto;
import com.tongda.tddopo.config.Neo4jProperties;
import com.tongda.tddopo.neo4j.Neo4jServer;
import org.neo4j.graphdb.GraphDatabaseService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
@Configuration
@EnableConfigurationProperties(Neo4jProperties.class)
public class Neo4jAutoConfiguration {
@Resource
private Neo4jProperties properties; // 使用配置
@Bean
@ConditionalOnMissingBean
public GraphDatabaseService init() {
//初始化neo4j
GraphDatabaseService graphDatabaseService=new Neo4jServer().init(properties.getPath(),properties.getBolt());
System.out.println("neo4j-server启动成功");
return graphDatabaseService;
}
}
package com.tongda.tddopo.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "neo4j")
@Data
public class Neo4jProperties {
private String path="data/";
private String bolt="true";
}
package com.tongda.tddopo.neo4j;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.graphdb.schema.IndexDefinition;
import org.neo4j.graphdb.schema.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import javax.annotation.PostConstruct;
import java.io.File;
@Configuration
public class Neo4jServer {
public static GraphDatabaseService graphDatabaseService;
// public static DatabaseManagementService managementService;
// @Autowired
// private Environment environment;
// @PostConstruct
public GraphDatabaseService init(String file,String bolt){
// GraphDatabaseSettings.BoltConnector boltConnector = GraphDatabaseSettings.boltConnector( "0" );
graphDatabaseService = new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder(new File(file))
.setConfig( GraphDatabaseSettings.pagecache_memory, "8192M" )
.setConfig( GraphDatabaseSettings.string_block_size, "600" )
.setConfig( GraphDatabaseSettings.array_block_size, "3000" )
// .setConfig( boltConnector.type, "BOLT" )
// .setConfig( boltConnector.enabled, "true" )
// .setConfig( boltConnector.address, "localhost:7687")
.newGraphDatabase();
IndexDefinition indexDefinition;
try ( Transaction tx = graphDatabaseService.beginTx() )
{
// Schema schema = graphDatabaseService.schema();
// indexDefinition = schema.indexFor( Label.label( "MDE" ))
// .on( "id" )
// .create();
// indexDefinition = schema.indexFor( Label.label( "AST" ))
// .on( "id" )
// .create();
tx.success();
}
return graphDatabaseService;
}
// private static void registerShutdownHook( final GraphDatabaseService graphDb )
// {
// // Registers a shutdown hook for the Neo4j instance so that it
// // shuts down nicely when the VM exits (even if you "Ctrl-C" the
// // running application).
// Runtime.getRuntime().addShutdownHook( new Thread()
// {
// @Override
// public void run()
// {
// graphDb.shutdown();
// }
// } );
// }
// @PostConstruct
// public void init(){
//
// Path of = Path.of(environment.getProperty("spring.neo4j.path"));
// managementService = new DatabaseManagementServiceBuilder(of)
// .setConfig( BoltConnector.enabled, true)
//// .setConfig( HttpConnector.enabled, true )
//// .setConfig( HttpConnector.listen_address, new SocketAddress(Integer.parseInt(environment.getProperty("spring.neo4j.port"))) )
// .setConfig( BoltConnector.listen_address, new SocketAddress(Integer.parseInt(environment.getProperty("spring.neo4j.port"))))
//// .setConfig( GraphDatabaseSettings.auth_enabled,true)
//// .setConfig( GraphDatabaseSettings.data_directory,of)
// .setConfig( GraphDatabaseSettings.initial_default_database,environment.getProperty("spring.data.neo4j.database"))
// .setConfig(GraphDatabaseSettings.transaction_timeout, Duration.ofSeconds(60))
// .build();
// graphDatabaseService = managementService.database(environment.getProperty("spring.data.neo4j.database"));
// }
}
package com.tongda.tddopo.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tongda.tddopo.neo4j.Neo4jServer;
import com.tongda.tddopo.util.GraphUtil;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.neo4j.graphdb.*;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class TDGraphService {
GraphDatabaseService graphDb= Neo4jServer.graphDatabaseService;
public static HashMap<String ,String> keymap=new HashMap<>();
static {
keymap.put("version","integer");
keymap.put("valueMax","double");
keymap.put("valueMin","double");
}
/**
* 添加元数据
* 新xml格式输入
* 重复的内容会覆盖
* @param xml 元数据扁平化格式结果
* @return 成功返回 true
*/
public boolean addMetaData(String xml){
//清理数据
//MATCH (n)-[r]-() WITH r,n LIMIT 1000000 DETACH DELETE r,n RETURN count(*);
System.out.println("接收数据开始处理:"+xml);
long totalMilliSeconds1 = System.currentTimeMillis();
try(Transaction transaction = graphDb.beginTx()) {
//解析XML数据
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
Element graph = document.getRootElement();
Element reNodes=graph.element("Nodes");
//处理节点
List<Element> elements=reNodes.elements("Node");
long totalMilliSeconds2 = System.currentTimeMillis();
// System.out.println("xml处理时间:"+(totalMilliSeconds2-totalMilliSeconds1));
Map<String ,Node> nodeMap=new HashMap<>();
//节点处理循环
for (Element e : elements) {
//判断节点是否存在
long totalMilliSecondsSelect1 = System.currentTimeMillis();
Node node = graphDb.findNode(Label.label(e.attributeValue("tag")), "id", e.attributeValue("id")==null?"null":e.attributeValue("id"));
long totalMilliSecondsSelect2 = System.currentTimeMillis();
// System.out.println("查询节点耗时:"+(totalMilliSecondsSelect2-totalMilliSecondsSelect1));
if (node == null) {
//不存在创建空节点
long totalMilliSeconds = System.currentTimeMillis();
node=graphDb.createNode();
long totalMilliSeconds3 = System.currentTimeMillis();
// System.out.println("库操作/创建空节点耗时:"+(totalMilliSeconds3-totalMilliSeconds));
} else {
//存在则循环移除属性
long totalMilliSeconds = System.currentTimeMillis();
Iterable<String> i=node.getPropertyKeys();
for (String s:i) {
node.removeProperty(s);
}
long totalMilliSeconds4 = System.currentTimeMillis();
// System.out.println("库操作/重置属性耗时:"+(totalMilliSeconds4-totalMilliSeconds));
}
//设置标签
node.addLabel(Label.label(e.attributeValue("tag")));
//判断dataType属性
long totalMilliSeconds5 = System.currentTimeMillis();
if (e.attributeValue("dataType") != null) {
String[] dataTypes = e.attributeValue("dataType").split("\\.");
System.out.println(e.attributeValue("dataType"));
System.out.println(Arrays.toString(dataTypes));
String dataType=dataTypes[dataTypes.length-1];
node.setProperty("dataType",dataType);
if(e.attributeValue("mode")!=null){
if (e.attributeValue("mode").endsWith(".multi.select.value")||e.attributeValue("mode").endsWith("range.value")){
dataType="string";
}
}
//设置value
if (e.attributeValue("value")!=null&&(!e.attributeValue("value").equals(""))){
switch (dataType) {
case "string": {
node.setProperty("value",e.attributeValue("value"));
e.remove(e.attribute("dataType"));
e.remove(e.attribute("value"));
break;
}
case "integer": {
int value = Integer.parseInt(e.attributeValue("value"));
node.setProperty("value",value);
System.out.println("-----------------------------------------");
e.remove(e.attribute("dataType"));
e.remove(e.attribute("value"));
break;
}
case "float": {
float value = Float.parseFloat(e.attributeValue("value"));
node.setProperty("value",value);
e.remove(e.attribute("dataType"));
e.remove(e.attribute("value"));
break;
}
}
}
}
long totalMilliSeconds6 = System.currentTimeMillis();
// System.out.println("库操作/处理value:"+(totalMilliSeconds6-totalMilliSeconds5));
//处理特定字段的值类型,字段类型集合keymap
for (Attribute a : e.attributes()) {
if (keymap.get(a.getName()) == null) {
node.setProperty(a.getName(),a.getValue());
} else {
String type = keymap.get(a.getName());
switch (type) {
case "long":
node.setProperty(a.getName(),Long.valueOf(a.getValue()));
break;
case "integer":
node.setProperty(a.getName(),Integer.valueOf(a.getValue()) );
break;
case "double":
node.setProperty(a.getName(),Double.valueOf(a.getValue()));
break;
}
}
}
long totalMilliSeconds7 = System.currentTimeMillis();
// System.out.println("库操作/处理属性字段类型总耗时:"+(totalMilliSeconds7-totalMilliSeconds6));
//将节点对象添加到map
nodeMap.put(e.attributeValue("id"),node);
}
long totalMilliSeconds8 = System.currentTimeMillis();
// System.out.println("库操作/节点处理总耗时:"+(totalMilliSeconds8-totalMilliSeconds2));
//先删除中央节点向下的关系
long totalMilliSeconds9= System.currentTimeMillis();
String id = graph.attributeValue("rootID");
String tag="";
for (Element e : elements) {
if(id.equals(e.attributeValue("id"))){
tag=e.attributeValue("tag");
}
}
//删除线测试
removeGraphRelation(id);
// Node rootNode = graphDb.findNode(Label.label(tag), "id",id);
// Iterable<Relationship> resourceIterable=rootNode.getRelationships();
// for (Relationship relationship:resourceIterable){
// String s=relationship.getType().name();
// if (s.equals("HAS_METADATA")) {
// relationship.delete();
//// System.out.println("执行删除");
// }
//
// }
long totalMilliSeconds10 = System.currentTimeMillis();
// System.out.println("库操作/删除中央节点向下的关系耗时:"+(totalMilliSeconds10-totalMilliSeconds9));
//重新添加关系
List<Element> relationships = graph.element("Edges").elements("Edge");
for (Element e : relationships) {
Node fromNode=nodeMap.get(e.attributeValue("from"));
Node toNode=nodeMap.get(e.attributeValue("to"));
if(fromNode==null){
String fromTag=e.attributeValue("from").split("-")[0];
fromNode = graphDb.findNode(Label.label(fromTag), "id", e.attributeValue("from"));
}
if(toNode==null){
String toTag=e.attributeValue("to").split("-")[0];
toNode = graphDb.findNode(Label.label(toTag), "id", e.attributeValue("to"));
}
if (fromNode!=null&&toNode!=null){
RelationshipType relationshipType=RelationshipType.withName(e.attributeValue("relType"));
long totalMilliSeconds11 = System.currentTimeMillis();
Iterable<Relationship> iterator=fromNode.getRelationships();
Relationship relationship=null;
for (Relationship relationship1:iterator){
if(relationship1.getProperty("hash").equals(e.attributeValue("hash"))){
relationship=relationship1;
}
}
long totalMilliSeconds12 = System.currentTimeMillis();
// System.out.println("库操作/查询关系耗时:"+(totalMilliSeconds12-totalMilliSeconds11));
if (relationship==null){
relationship=fromNode.createRelationshipTo(toNode,relationshipType);
for (Attribute a : e.attributes()) {
relationship.setProperty(a.getName(),a.getValue());
}
}
}else {
System.out.println("连线节点不存在:"+fromNode+"/"+toNode);
}
}
long totalMilliSeconds11 = System.currentTimeMillis();
// System.out.println("库操作/添加关系总耗时:"+(totalMilliSeconds11-totalMilliSeconds10));
long totalMilliSeconds12 = System.currentTimeMillis();
transaction.success();
long totalMilliSeconds13 = System.currentTimeMillis();
System.out.println("success耗时:"+(totalMilliSeconds13-totalMilliSeconds12));
System.out.println("提交------------------------------------------------------------------------------");
} catch (DocumentException e) {
// System.out.println(e.toString());
throw new RuntimeException(e);
}
return true;
}
/**
* 只更新节点node
* @param xml
* @return
*/
public String updateNodes(String xml){
try(Transaction transaction = graphDb.beginTx()) {
//解析XML数据
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
Element root = document.getRootElement();
//处理节点
List<Element> elements=root.elements("Nodes");
Element nodes=elements.get(0);
for (Element e : nodes.elements()) {
//属性拼装对象
String s = "";
if (e.attributeValue("dataType") != null) {
String dataType = e.attributeValue("dataType");
switch (dataType) {
case "string": {
String value = e.attributeValue("value");
s = s + "value:" + value + ",";
e.remove(e.attribute("value"));
break;
}
case "integer": {
int value = Integer.parseInt(e.attributeValue("value"));
s = s + "value:" + value + ",";
e.remove(e.attribute("value"));
break;
}
case "float": {
float value = Float.parseFloat(e.attributeValue("value"));
s = s + "value:" + value + ",";
e.remove(e.attribute("value"));
break;
}
}
}
//处理特定字段的值类型,字段类型集合keymap
for (Attribute a : e.attributes()) {
if (keymap.get(a.getName()) == null) {
s = s + a.getName() + ":'" + a.getValue() + "',";
} else {
String type = keymap.get(a.getName());
switch (type) {
case "long":
s = s + a.getName() + ":" + Long.valueOf(a.getValue()) + ",";
break;
case "integer":
s = s + a.getName() + ":" + Integer.valueOf(a.getValue()) + ",";
break;
case "double":
s = s + a.getName() + ":" + Double.valueOf(a.getValue()) + ",";
break;
}
}
}
s = s.substring(0, s.length() - 1);
//根据id查询节点是否存在,不存在新增,存在则将属性全部重新set一遍
Node node = graphDb.findNode(Label.label(e.attributeValue("tag")), "id", e.attributeValue("id"));
if (node != null){
String reset = "MATCH (n:" + e.attributeValue("tag") + ") WHERE n.id = '" + e.attributeValue("id") +
"' SET n = {" + s + "}";
graphDb.execute(reset);
}
}
transaction.success();
} catch (DocumentException e) {
throw new RuntimeException(e);
}
return "true";
}
/**
*移除节点
* @param xml
* @return
*/
public String removeNodes(String xml){
try(Transaction transaction = graphDb.beginTx()) {
//解析XML数据
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
Element root = document.getRootElement();
//重新添加关系
List<Element> relationships = root.elements("Edges");
Element relations = relationships.get(0);
//删除关系
for (Element e : relations.elements()) {
String relationMerge ="match (n)-[r{from:'"+e.attributeValue("from")+"', to:'"+e.attributeValue("to")+"'}]-(m) delete r";
System.out.println(relationMerge);
graphDb.execute(relationMerge);
}
//删除节点
List<Element> elements=root.elements("Nodes");
Element nodes=elements.get(0);
for (Element e : nodes.elements()) {
//根据id查询节点是否存在,不存在新增,存在则将属性全部重新set一遍
String remove = "match (n{id:'"+e.attributeValue("id")+"'}) delete n";
graphDb.execute(remove);
}
transaction.success();
} catch (DocumentException e) {
throw new RuntimeException(e);
}
return "true";
}
/**
* 根据id添加节点tag
* @param json
* {
* "tag": "TagTwo",
* "idList": [
* "Product-1112041739683627008",
* "MetadataEntry-1112041739683627015"
* ]
* }
* @return
*/
public String addTag(String json){
try(Transaction transaction = graphDb.beginTx()) {
JSONObject jsonObject= JSON.parseObject(json);
String tag = jsonObject.get("tag").toString();
List<String > idList= (List<String>) jsonObject.get("idList");
String addTag="match (n) where ";
for (String s: idList) {
addTag=addTag+"n.id='"+s+"' or ";
}
addTag=addTag.substring(0,addTag.length()-3);
addTag=addTag+" set n:"+ tag+" return n";
// System.out.println(addTag);
graphDb.execute(addTag);
transaction.success();
}
return "true";
}
/**
* 根据id删除节点
* @param json
* {
* "idList": [
* "CandidateValue-1094237139551387649",
* "MetadataEntry-1092842332425814016"
* ]
* }
* @return
*/
public String removeNode(String json){
try(Transaction transaction = graphDb.beginTx()) {
JSONObject jsonObject= JSON.parseObject(json);
List<String > idList= (List<String>) jsonObject.get("idList");
String cypher="match (n) where ";
for (String s: idList) {
cypher=cypher+"n.id='"+s+"' or ";
}
cypher=cypher.substring(0,cypher.length()-3);
cypher=cypher+" delete n";
graphDb.execute(cypher);
transaction.success();
}
return "true";
}
/**
* 删除节点及关系
* @param json
* {
* "idList": [
* "CandidateValue-1094237139551387649",
* "MetadataEntry-1092842332425814016"
* ]
* }
* @return
*/
public String removeNodeAndRelation(String json){
try(Transaction transaction = graphDb.beginTx()){
JSONObject jsonObject= JSON.parseObject(json);
List<String > idList= (List<String>) jsonObject.get("idList");
String cypher="match (n) where ";
for (String s: idList) {
cypher=cypher+"n.id='"+s+"' or ";
}
cypher=cypher.substring(0,cypher.length()-3);
cypher=cypher+" DETACH DELETE n";
graphDb.execute(cypher);
transaction.success();
}
return "true";
}
/**
* 根据id删除节点及以下可以删除的节点和关系
*/
public String removeGraph(String id){
String cypher="";
String cypher2="";
try(Transaction transaction = graphDb.beginTx()){
String tag=id.split("-")[0];
switch (tag){
case "MDET":
cypher="match (n:"+tag+")-[r:HAS_VALUE]->(m) where n.id='"+id+"' DETACH DELETE n,m";
break;
case "ASTT":
cypher="match (n:"+tag+")-[r:HAS_METADATA]->(m) where n.id='"+id+"' DETACH DELETE n,m";
break;
case "AST":
cypher="match (n:"+tag+")-[r:HAS_METADATA]->(m) where n.id='"+id+"' DETACH DELETE n,m";
break;
case "REST":
cypher="match (n:"+tag+")-[r:HAS_METADATA]->(m) where n.id='"+id+"' DETACH DELETE n,m";
break;
case "RES":
cypher="match (n:"+tag+")-[r:HAS_METADATA]->(m) where n.id='"+id+"' DETACH DELETE n,m";
break;
case "TEM":
cypher="match (n:"+tag+")-[r:HAS_ROLE]->(m) where n.id='"+id+"' DETACH DELETE n,m";
cypher2="match (m:Product)<-[t:HAS_ASSET]-(n:"+tag+"{id:'"+id+"'})-[r:HAS_ASSET]->(p:Product) " +
"match (m)-[y{depend:'"+id+"'}]->(p) " +
"delete y";
break;
}
Result result;
if(!cypher2.equals("")){
result=graphDb.execute(cypher2);
}
result=graphDb.execute(cypher);
// System.out.println(result.toString());
transaction.success();
}
return "true";
}
public String removeGraphRelation(String id){
String cypher="";
String cypher2="";
try(Transaction transaction = graphDb.beginTx()){
String tag=id.split("-")[0];
switch (tag){
case "MDET":
cypher="match (n:"+tag+")-[r:HAS_VALUE]->(m) where n.id='"+id+"' DETACH DELETE r";
break;
case "ASTT":
cypher="match (n:"+tag+")-[r:HAS_METADATA]->(m) where n.id='"+id+"' DETACH DELETE r";
break;
case "AST":
cypher="match (n:"+tag+")-[r:HAS_METADATA]->(m) where n.id='"+id+"' DETACH DELETE r";
break;
case "REST":
cypher="match (n:"+tag+")-[r:HAS_METADATA]->(m) where n.id='"+id+"' DETACH DELETE r";
break;
case "RES":
cypher="match (n:"+tag+")-[r:HAS_METADATA]->(m) where n.id='"+id+"' DETACH DELETE r";
break;
case "TEM":
cypher="match (n:"+tag+")-[r:HAS_ROLE]->(m) where n.id='"+id+"' DETACH DELETE r";
cypher2="match (m:Product)<-[t:HAS_ASSET]-(n:"+tag+"{id:'"+id+"'})-[r:HAS_ASSET]->(p:Product) " +
"match (m)-[y{depend:'"+id+"'}]->(p) " +
"delete y";
break;
}
Result result;
if(!cypher2.equals("")){
result=graphDb.execute(cypher2);
}
result=graphDb.execute(cypher);
// System.out.println(result.toString());
transaction.success();
}
return "true";
}
/**
* 移除关系
* @param json
* {
* "fromID": "aaa",
* "toID": "bbb",
* "relation": "HAS_RESOURCE"
* }
* @return
*/
public String removeRelation(String json){
//{”fromID“:"aaa","toID":"bbb","relation":"HAS_RESOURCE"}
try(Transaction transaction = graphDb.beginTx()){
JSONObject jsonObject= JSON.parseObject(json);
String cypher="match (n{id:'"+jsonObject.get("fromID")+"'})-[r:"+jsonObject.get("relation")+"]->(m{id:'"+jsonObject.get("toID")+"'}) " +
"DELETE r";
System.out.println(cypher);
graphDb.execute(cypher);
transaction.success();
}
return "true";
}
/**
* 添加关系
* @param json
* {
* "fromID": "aaa",
* "toID": "bbb",
* "relation": "HAS_RESOURCE"
* }
* @return
*/
public String addRelation(String json){
//{”fromID“:"aaa","toID":"bbb","relation":"HAS_RESOURCE"}
try(Transaction transaction = graphDb.beginTx();){
JSONObject jsonObject= JSON.parseObject(json);
String cypher="match (n{id:'"+jsonObject.get("fromID")+"'}),"+
"(m{id:'"+jsonObject.get("toID")+"'}) " +
"MERGE (n)-[r:"+jsonObject.get("relation")+"]->(m)";
graphDb.execute(cypher);
transaction.success();
}
return "true";
}
/**
* 完全删除一个标签的标记
* @param json
* {
* "tag": "aaa"
* }
* @return
*/
public String removeTagAllNode(String json){
try(Transaction transaction = graphDb.beginTx()){
JSONObject jsonObject= JSON.parseObject(json);
String cypher="MATCH (n) REMOVE n:"+jsonObject.get("tag")+" ";
graphDb.execute(cypher);
transaction.success();
}
return "true";
}
/**
* 根据id删除指定节点的标签
* @param json
* {
* "tag": "aaa",
* "id": "aaaa8"
* }
* @return
*/
public String removeTagWithNode(String json){
try(Transaction transaction = graphDb.beginTx()){
JSONObject jsonObject= JSON.parseObject(json);
String cypher="MATCH (n{id:'"+jsonObject.get("id")+"'}) REMOVE n:"+jsonObject.get("tag")+" ";
graphDb.execute(cypher);
transaction.success();
}
return "true";
}
/**
* 根据tag和id查询节点,id是或的关系
* @param json
*{
* "tag": [
* "ProductTemplate"
* ],
* "idList": [
* "ProductTemplate-1112009760707706880"
* ]
* }
* @return
*/
public String searchMetadata(String json){
try(Transaction transaction = graphDb.beginTx()){
JSONObject jsonObject= JSON.parseObject(json);
List<String > tagList= (List<String>) jsonObject.get("tag");
//添加tag条件
String cypher="match (n";
for (String s:
tagList) {
cypher=cypher+":"+s;
}
cypher=cypher+") ";
//添加id条件
List<String > idList= (List<String>) jsonObject.get("idList");
if (idList.size()!=0){
cypher=cypher+"where ";
for (String s:
idList) {
cypher=cypher+"n.id='"+s+"' or ";
}
cypher=cypher.substring(0,cypher.length()-3);
}
cypher=cypher+" return n";
Result result=graphDb.execute(cypher);
ArrayList<String> list=new ArrayList<>();
while (result.hasNext()) {
Map<String,Object> row = result.next();
for (Map.Entry<String,Object> column : row.entrySet()) {
JSONObject json1 = new JSONObject(((Node)column.getValue()).getAllProperties());
list.add(json1.toString());
}
}
transaction.success();
return list.toString();
}
}
/**
* 根据tag和label查询节点,id是或的关系
* @param json
* {
* "tag": [
* "MetadataEntry"
* ],
* "label": "温度"
* }
* @return
*/
public String searchByLabel(String json){
try(Transaction transaction = graphDb.beginTx()){
JSONObject jsonObject= JSON.parseObject(json);
List<String > tagList= (List<String>) jsonObject.get("tag");
//添加tag条件
String cypher="match (n";
for (String s:
tagList) {
cypher=cypher+":"+s;
}
cypher=cypher+") ";
//模糊查询内容
cypher=cypher+" where n.label contains '"+jsonObject.get("label")+"' return n";
System.out.println(cypher);
Result result=graphDb.execute(cypher);
ArrayList<String> list=new ArrayList<>();
while (result.hasNext()) {
Map<String,Object> row = result.next();
for (Map.Entry<String,Object> column : row.entrySet()) {
JSONObject json1 = new JSONObject(((Node)column.getValue()).getAllProperties());
list.add(json1.toString());
}
}
transaction.success();
return list.toString();
}
}
/**
* 根据tag和vakue查询节点,id是或的关系
* {
* "tag": [
* "MetadataEntry"
* ],
* "value": "温度"
* }
* @return
*/
public List<String > searchByValueOrLabel(String[] tag,String queryData){
try(Transaction transaction = graphDb.beginTx()){
//添加tag条件
System.out.println(Arrays.toString(tag));
String cypher="match (m";
for (String s:
tag) {
cypher=cypher+":"+s;
}
cypher=cypher+") -[r*1]->(n) ";
//模糊查询内容
cypher=cypher+" where n.value contains '"+queryData+"'"+" or n.label contains '"+queryData+"'"+" return m";
System.out.println(cypher);
Result result=graphDb.execute(cypher);
ArrayList<String> list=new ArrayList<>();
while (result.hasNext()) {
Map<String,Object> row = result.next();
for (Map.Entry<String,Object> column : row.entrySet()) {
JSONObject json1 = new JSONObject(((Node)column.getValue()).getAllProperties());
list.add(json1.toString());
}
}
transaction.success();
return list;
}
}
/**
* 查询节点的关系
* @param json
* {"id":"MetadataEntry-1094237139551387648"}
* @return
*/
public String nodeRelationType(String json){
try(Transaction transaction = graphDb.beginTx()){
JSONObject jsonObject= JSON.parseObject(json);
String id= jsonObject.get("id").toString();
String type=id.split("-")[0];
String cypher="match " +
"(n)-[i]-(m:"+type+"{id:\""+id+"\"}) " +
"return DISTINCT type(i)";
System.out.println(cypher);
Result result=graphDb.execute(cypher);
List<JSONObject> list=new ArrayList<>();
while (result.hasNext()) {
Map<String,Object> row = result.next();
for (Map.Entry<String,Object> column : row.entrySet()) {
JSONObject jsonType=new JSONObject();
jsonType.put("type",column.getValue().toString());
// jsonType.put("label","");
list.add(jsonType);
}
}
HashMap<String ,List<JSONObject>> map=new HashMap<>();
map.put("up",list);
transaction.success();
return JSONObject.toJSONString(map);
}
}
/**
*根据 节点id,上下行,关系类别 查询节点
* @param json
* {
* "id": "ProductTemplate-1112009760707706880",
* "type": "HAS_METADATA",
* "directional": "down"
* }
* @return
*/
public String nodeAndRelationToNode(String json){
try(Transaction transaction = graphDb.beginTx()){
JSONObject jsonObject= JSON.parseObject(json);
String type=jsonObject.get("id").toString().split("-")[0];
String cypher="match (n)-[i:"+jsonObject.get("type").toString()+"]-(m:"+type+"{id:\""+jsonObject.get("id").toString()+"\"}) return DISTINCT properties(n)";
Result result=graphDb.execute(cypher);
List<Object> listNode=new ArrayList<>();
while (result.hasNext()) {
Map<String, Object> row = result.next();
for (Map.Entry<String, Object> column : row.entrySet()) {
listNode.add(column.getValue());
}
}
transaction.success();
return JSONObject.toJSONString(listNode);
}
}
/**
* 添加自定义标签字典表节点
* CustomizationTag
* @param json
* {
* "name": "TagTwo",
* "label": "自定义标签",
* "description": "xxxx类型的标记"
* }
* @return
*/
public String addTagType(String json){
try(Transaction transaction = graphDb.beginTx()){
JSONObject jsonObject= JSON.parseObject(json);
String cypher="merge (n:CustomizationTag"+
"{name:'"+jsonObject.get("name")+"',label:'"+jsonObject.get("label")+"',description:'"+jsonObject.get("description")+"'})";
graphDb.execute(cypher);
transaction.success();
return "true";
}
}
/**
* 查询自定义标签表
* @return
*/
public String searchTagType(){
try(Transaction transaction = graphDb.beginTx()){
String cypher="match (n:CustomizationTag) return n";
Result result=graphDb.execute(cypher);
ArrayList<String> list=new ArrayList<>();
while (result.hasNext()) {
Map<String,Object> row = result.next();
for (Map.Entry<String,Object> column : row.entrySet()) {
JSONObject json1 = new JSONObject(((Node)column.getValue()).getAllProperties());
list.add(json1.toString());
}
}
transaction.success();
return list.toString();
}
}
/**
* 删除自定义标签
* @param json
* {"name":"TagTwo"}
* @return
*/
public String removeTagType(String json){
try(Transaction transaction = graphDb.beginTx();){
JSONObject jsonObject= JSON.parseObject(json);
String cypher="match (n:CustomizationTag{name:'"+jsonObject.get("name")+"'}) delete n";
graphDb.execute(cypher);
transaction.success();
return "true";
}
}
/**
* 添加自定义关系类型字典表节点
* @param json
* {
* "name": "TagTwo",
* "label": "自定义关系",
* "description": "xxxx类型的关系"
* }
* @return
*/
public String addRelationType(String json){
try(Transaction transaction = graphDb.beginTx()){
JSONObject jsonObject= JSON.parseObject(json);
String cypher="merge (n:CustomizationRelation"+
"{name:'"+jsonObject.get("name")+"',label:'"+jsonObject.get("label")+"',description:'"+jsonObject.get("description")+"'})";
graphDb.execute(cypher);
transaction.success();
return "true";
}
}
/**
* 查询自定义关系类型表
* @return
*/
public String searchRelationType(){
try(Transaction transaction = graphDb.beginTx()){
String cypher="match (n:CustomizationRelation) return n";
Result result=graphDb.execute(cypher);
ArrayList<String> list=new ArrayList<>();
while (result.hasNext()) {
Map<String,Object> row = result.next();
for (Map.Entry<String,Object> column : row.entrySet()) {
JSONObject json1 = new JSONObject(((Node)column.getValue()).getAllProperties());
list.add(json1.toString());
}
}
transaction.success();
return list.toString();
}
}
/**
* 删除自定义关系类型
* {"name":"TagTwo"}
* @param json
* @return
*/
public String removeRelationType(String json){
try(Transaction transaction = graphDb.beginTx()){
JSONObject jsonObject= JSON.parseObject(json);
String cypher="match (n:CustomizationRelation{name:'"+jsonObject.get("name")+"'}) delete n";
graphDb.execute(cypher);
transaction.success();
return "true";
}
}
/**
* 获取一个节点下的所有信息
* //获取下级节点
* // match (n{id:"Product-1092049602028240896aaa"})-[*1..5]->(m)
* // return DISTINCT
* // n,m
* //获取所有下级关系
* //match (n{id:"Product-1092049602028240896aaa"})-[r*1..5]->(m)
* // return DISTINCT r
* @param json
* @return
*/
public String searchGraph(String json){
try(Transaction transaction = graphDb.beginTx()){
JSONObject jsonObject= JSON.parseObject(json);
//获取中心节点
String tage=jsonObject.get("id").toString().split("-")[0];
String cypher="match (n:"+tage+"{id:'"+jsonObject.get("id")+"'}) return DISTINCT n";
Result result=graphDb.execute(cypher);
HashMap<String,JSONObject> centerNodeMap= GraphUtil.nodesUtil(result);
Iterator<String> iterator=centerNodeMap.keySet().iterator();
String id="";
JSONObject centerNode=null;
if(iterator.hasNext()){
id=iterator.next();
centerNode=centerNodeMap.get(id);
}
//下行步长内的节点
cypher="match (n:"+tage+"{id:'"+jsonObject.get("id")+"'})-[*1.."+jsonObject.get("downsteps")+"]->(m) " +
"return DISTINCT n,m";
System.out.println(cypher);
result=graphDb.execute(cypher);
HashMap<String,JSONObject> hashMapNode=GraphUtil.nodesUtil(result);
hashMapNode.remove(id);
//下行步长内的关系
cypher="match p=(n:"+tage+"{id:'"+jsonObject.get("id")+"'})-[r*1.."+jsonObject.get("downsteps")+"]->(m) return DISTINCT relationships(p)";
System.out.println(cypher);
result=graphDb.execute(cypher);
ArrayList<JSONObject> relationList=GraphUtil.linksUtil(result);
//map转list
JSONObject returnJson=new JSONObject();
ArrayList<JSONObject> nodeList=new ArrayList<>();
for (String s:hashMapNode.keySet()){
JSONObject jsonObject1=hashMapNode.get(s);
nodeList.add(jsonObject1);
}
//上行步长内的节点
cypher="match (n:"+tage+"{id:'"+jsonObject.get("id")+"'})<-[*1.."+jsonObject.get("upsteps")+"]-(m)" +
"return DISTINCT n,m";
result=graphDb.execute(cypher);
HashMap<String,JSONObject> parentNodes=GraphUtil.nodesUtil(result);
parentNodes.remove(id);
ArrayList<JSONObject> parentNodeList=new ArrayList<>();
for (String s:parentNodes.keySet()){
JSONObject jsonObject1=parentNodes.get(s);
parentNodeList.add(jsonObject1);
}
//上行步长内的关系
cypher="match p=(n:"+tage+"{id:'"+jsonObject.get("id")+"'})<-[r*1.."+jsonObject.get("upsteps")+"]-(m) return DISTINCT relationships(p)";
result=graphDb.execute(cypher);
ArrayList<JSONObject> parentRelationList=GraphUtil.linksUtil(result);
nodeList.add(centerNode);
nodeList.addAll(parentNodeList);
returnJson.put("nodes",nodeList);
relationList.addAll(parentRelationList);
returnJson.put("links",relationList);
String re=returnJson.toString();
transaction.close();
return re;
}
}
/**
*根据id查询图
* @param json
* [
* "Product-1112041472149946368",
* "Product-1112039689512026112"
* ]
* @return
*/
public String searchIdGraph(String json){
try(Transaction transaction = graphDb.beginTx()){
JSONArray jsonArray= JSONObject.parseObject(json,JSONArray.class);
//id列表内的节点
String cypher="match (n) where n.id in "+jsonArray.toString()+" return DISTINCT n";
System.out.println(cypher);
Result result=graphDb.execute(cypher);
HashMap<String,JSONObject> hashMapNode=GraphUtil.nodesUtil(result);
//map转list
JSONObject returnJson=new JSONObject();
ArrayList<JSONObject> nodeList=new ArrayList<>();
for (String s:hashMapNode.keySet()){
JSONObject jsonObject1=hashMapNode.get(s);
jsonObject1.put("id",s);
nodeList.add(jsonObject1);
}
//id列表内的节点间的关系
cypher="match (n)-[i*1]->(m) where n.id in "+jsonArray+" " +
"and" +
"" + " m.id in "+jsonArray+" return DISTINCT i";
System.out.println(cypher);
result=graphDb.execute(cypher);
ArrayList<JSONObject> relationList=GraphUtil.linksUtil(result);
returnJson.put("nodes",nodeList);
returnJson.put("links",relationList);
String re=returnJson.toString();
transaction.close();
return re;
}
}
/**
* 数据统计的接口
* match (n)-[HAS_METADATA]->(m) where (n.id="Product-1092049602028240896" or n.id="Product-1092049602028240896aaa")
* and (m.key="fai.env.temperature" or m.key="fai.env.working.temperature" or m.key="book.student.math") return n,m
* @param json
* {
* "idList": ["Product-1092049602028240896","Product-1092049602028240896aaa"],
* "tagList": ["Product"],
* "keyList": ["fai.env.temperature","fai.env.working.temperature","book.student.math"]*
* }
* @return
*/
public String searchData(String json) {
try(Transaction transaction = graphDb.beginTx()){
JSONObject inputJson= JSON.parseObject(json);
List<String > tagList= (List<String>) inputJson.get("tagList");
//添加tag
String cypher="match (n";
for (String s:
tagList) {
cypher=cypher+":"+s;
}
cypher=cypher+")-[HAS_METADATA]->(m) ";
//添加id条件
List<String > idList= (List<String>) inputJson.get("idList");
if (idList.size()!=0){
cypher=cypher+"where (";
for (String s:
idList) {
cypher=cypher+"n.id='"+s+"' or ";
}
cypher=cypher.substring(0,cypher.length()-4)+")";
}
//添加key
cypher=cypher+" and (";
List<String > keyList= (List<String>) inputJson.get("keyList");
if (keyList.size()!=0){
for (String s:
keyList) {
cypher=cypher+"m.key='"+s+"' or ";
}
cypher=cypher.substring(0,cypher.length()-4)+")";
}
cypher=cypher+" return n.id,m";
Result result=graphDb.execute(cypher);
JSONObject vector=new JSONObject();
ArrayList<JSONObject> scalarList=new ArrayList<>();
HashMap<String ,ArrayList<JSONObject>> hashMap=new HashMap<>();
while (result.hasNext()) {
Map<String,Object> row = result.next();
Set<Map.Entry<String, Object>> set = row.entrySet();
Iterator<Map.Entry<String, Object>> i = set.iterator();
Map.Entry<String, Object> entry1=i.next();
ArrayList<JSONObject> dataList;
String key=entry1.getValue().toString();
if (hashMap.get(key)==null){
dataList=new ArrayList<>();
}else {
dataList=hashMap.get(entry1.getValue().toString());
}
Map.Entry<String, Object> entry2=i.next();
JSONObject jsonEntry = new JSONObject(((Node)entry2.getValue()).getAllProperties());
JSONObject data =new JSONObject();
data.put("uom",jsonEntry.get("nom"));
data.put("value",jsonEntry.get("value"));
data.put("dataType",jsonEntry.get("dataType"));
data.put("key",jsonEntry.get("key"));
data.put("label",jsonEntry.get("label"));
dataList.add(data);
hashMap.put(key,dataList);
}
for (String s:
hashMap.keySet()) {
JSONObject jsonObject=new JSONObject();
jsonObject.put("scalar",hashMap.get(s));
scalarList.add(jsonObject);
}
JSONObject inputDataSet=new JSONObject();
vector.put("Vector",scalarList);
inputDataSet.put("inputDataSet",vector);
transaction.success();
return inputDataSet.toString();
}
}
/**
* 查询全部数据(暂不使用)
* @return
*/
public String searchGraphAll() {
try(Transaction transaction = graphDb.beginTx()) {
String cypher = "match (n)-[r*1]-(m) return n,r,m limit 3000";
System.out.println(cypher);
Result result = graphDb.execute(cypher);
JSONObject returnJson = GraphUtil.LinksAndNodesUtil(result);
String re = returnJson.toString();
transaction.close();
return re;
}
}
/**
* 高级查询
* {
* "tags": [
* "Product",
* "Resource"
* ],
* "label": "Oracle数据库",
* "keys": [
* {
* "key": "MetadataEntryTemplate-responsible_person_name",
* "value": "刘三"
* }
* ,
* {
* "key": "MetadataEntryTemplate-responsible_department",
* "value": "建设部"
* }
* ]
* }
* @param jsonObject
* @return
*/
public HashMap<String ,HashMap<String,List<String>>> searchAllByValueOrLabel(JSONObject jsonObject) {
List<String> tags=JSONArray.parseArray(jsonObject.get("tags").toString(),String.class);
String label=jsonObject.getString("label");
List<HashMap> keys=JSONArray.parseArray(jsonObject.get("keys").toString(),HashMap.class);
try(Transaction transaction = graphDb.beginTx()){
//添加tag条件
String cypher="match (n) where (";
for (String s:
tags) {
cypher=cypher+" n:"+s+" or";
}
cypher = cypher.substring(0, cypher.length() - 2);
cypher=cypher+") and n.label contains '"+label+"' with n as p ";
if (keys.size()>0){
cypher=cypher+"match ";
int index=0;
String where=" where ";
for (Map map:keys){
cypher=cypher+"(p)-->(p"+index+"{key:'"+map.get("key")+"'}),";
String datatype=map.get("datatype").toString();
String symbol=map.get("symbol").toString();
if(datatype.equals("number")){
switch (symbol) {
case "=" :
where=where+" p"+index+".value="+map.get("value")+" and";
break;
case ">" :
where=where+" p"+index+".value>"+map.get("value")+" and";
break;
case "<" :
where=where+" p"+index+".value<"+map.get("value")+" and";
break;
}
}
if(datatype.equals("string")){
switch (symbol) {
case "=" :
where=where+" p"+index+".value='"+map.get("value")+"' and";
break;
case "contains" :
where=where+" p"+index+".value contains '"+map.get("value")+"' and";
break;
}
}
index++;
}
cypher = cypher.substring(0, cypher.length() - 1);
where = where.substring(0, where.length() - 3);
cypher=cypher+where;
}
cypher=cypher+" return p";
System.out.println(cypher);
Result result=graphDb.execute(cypher);
ArrayList<String> list=new ArrayList<>();
HashMap<String ,HashMap<String,List<String>>> hashMap=new HashMap();
while (result.hasNext()) {
Map<String,Object> row = result.next();
for (Map.Entry<String,Object> column : row.entrySet()) {
JSONObject jsonNode = new JSONObject(((Node)column.getValue()).getAllProperties());
// System.out.println("node:"+jsonNode);
String tag="";
if (jsonNode.get("tag")==null){
tag="default";
}else {
tag=jsonNode.get("tag").toString();
}
HashMap<String,List<String>> tagMap=hashMap.get(tag);
String template=jsonNode.get("template").toString();
if (tagMap==null){
tagMap=new HashMap<>();
}
List<String> nodelist=tagMap.get(template);
if(nodelist==null){
nodelist=new ArrayList<>();
}
nodelist.add(jsonNode.toString());
tagMap.put(template,nodelist);
hashMap.put(tag,tagMap);
}
}
return hashMap;
}
}
/**
* 查询tag类型
* @return
*/
public List<String> searchTagsType() {
try(Transaction transaction = graphDb.beginTx()){
String cypher="match(n) return distinct labels(n)";
Result result=graphDb.execute(cypher);
List<String > tagsList=new ArrayList<>();
while (result.hasNext()) {
Map<String,Object> row = result.next();
for (Map.Entry<String,Object> column : row.entrySet()) {
ArrayList<String> arrayList= (ArrayList<String>) column.getValue();
if (arrayList.size()==1){
tagsList.add(arrayList.get(0));
}
}
}
transaction.success();
return tagsList;
}
}
/**
* 删除孤立的节点
* @return
*/
public void deleteLoneNode() {
try(Transaction transaction = graphDb.beginTx()){
String cypher="match(n) where not(n) -- () delete(n)";
Result result=graphDb.execute(cypher);
transaction.success();
}
}
}
package com.tongda.tddopo.tasks;
import com.alibaba.fastjson.JSONObject;
import com.tongda.tddopo.service.TDGraphService;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@Component
@PropertySource(value = "classpath:application.properties")
public class SynchronizeMetadata {
// @Value("${db.login.user}")
// private String user;
// @Value("${db.login.password}")
// private String password;
// @Value("${db.login.loginId}")
// private String loginId;
// @Value("${db.login.loginPort}")
// private int loginPort;
// @Value("${db.pimserver.serverId}")
// private String serverId;
// @Value("${db.pimserver.serverPort}")
// private int serverPort;
// @Value("${db.pimserver.dbName}")
// private String dbName;
//
// private static Long lastTime;
//
// @Scheduled(cron="${cron.msg}")
// public void updateMetadata() {
// //初始化登录信息
// RemoteSaAuthHandler rsh = initConfig();
// lastTime=0L;
// //初次更新时初值
// if(lastTime==null){
// lastTime=System.currentTimeMillis();
// }
// //获取更新列表
// String url="http://"+ rsh.serverIP +":"+ rsh.serverPort+"/pims/index/swap?dbName="+rsh.dbName+"&timestamp="+lastTime;
// //保存更新时间
// lastTime=System.currentTimeMillis();
// String response= PimHttpUtil.sendGet(rsh.saToken,url);
// String data=responseData(response);
// metadataListHandle(data,rsh);
// }
//
// @Scheduled(cron="${cron.msg.date}")
// public void updateMetadataDate() {
// //初始化登录信息
// RemoteSaAuthHandler rsh = initConfig();
//// System.out.println("登录数据"+rsh);
// long time=System.currentTimeMillis()-1000*60*60*24;
// String url="http://"+ rsh.serverIP +":"+ rsh.serverPort+"/pims/index/swap?dbName="+rsh.dbName+"&timestamp="+time;
//
// String response= PimHttpUtil.sendGet(rsh.saToken,url);
//// System.out.println(response);
// JSONObject jsonObject=JSONObject.parseObject(response);
// String data=jsonObject.getString("data");
// metadataListHandle(data,rsh);
// }
//每天删除独立的节点
// @Scheduled(cron="${cron.msg.date}")
// public void deleteLoneNode() {
// TDGraphService tdGraphService=new TDGraphService();
// tdGraphService.deleteLoneNode();
// }
// //更新数据方法 createStamp
// public void selectById(Element e,RemoteSaAuthHandler rsh){
//
//
// String id=e.attributeValue("id");
// String func=e.attributeValue("func");
// long totalMilliSeconds1 = System.currentTimeMillis();
// String url1="http://"+ rsh.serverIP +":"+ rsh.serverPort+"/pims/index/graph?dbName="+rsh.dbName+"&id="+id+"&func="+func;
// String response= PimHttpUtil.sendGet(rsh.saToken,url1);
// long totalMilliSeconds2 = System.currentTimeMillis();
// System.out.println("拉取数据时间:"+(totalMilliSeconds2-totalMilliSeconds1));
// JSONObject jsonObject=JSONObject.parseObject(response);
// String xml=jsonObject.getString("data");
// System.out.println("返回id:"+id);
// TDGraphApi tdGraphApi=new TDGraphApi();
// tdGraphApi.addData(xml);
// long totalMilliSeconds3 = System.currentTimeMillis();
// System.out.println("添加数据时间:"+(totalMilliSeconds3-totalMilliSeconds2));
//
// }
//
// //处理更新id集合
// public void metadataListHandle(String data,RemoteSaAuthHandler rsh) {
//
// SAXReader saxReader = new SAXReader();
// try {
// System.out.println(data);
// Document document = saxReader.read(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)));
// Element root = document.getRootElement();
// //处理节点
// List<Element> elements=root.elements("GraphicUpdate");
// int i=0;
// for (Element e:elements) {
// i++;
// if (e.attributeValue("type").equals("Delete")){
// deleteById(e);
// }else if (checkTimeById(e)){
// selectById(e,rsh);
// }
// System.out.println("数量:"+i);
// }
// } catch (DocumentException e) {
// throw new RuntimeException(e);
// }
// }
//
// //检查id在neo4j里的时间,判断是否需要更新
// public boolean checkTimeById(Element e){
// long stamp=Long.parseLong(e.attributeValue("stamp"));
// String id=e.attributeValue("id");
// String type=e.attributeValue("id").split("-")[0];
// TDGraphApi tdGraphApi=new TDGraphApi();
// JSONObject json=new JSONObject();
//
// List<String> list=new ArrayList<>();
// list.add(type);
// json.put("tag",list);
// List<String> listid=new ArrayList<>();
// listid.add(id);
// json.put("idList",listid);
// String s=tdGraphApi.searchMetadata(json.toString());
// System.out.println("返回结果"+s);
// List<JSONObject> relist=JSONObject.parseArray(s,JSONObject.class);
//
// if (relist.size()>0){
// String time= (String) relist.get(0).get("updateStamp");
// long timeL=Long.parseLong(time);
// timeL=0;
//// System.out.println(time);
//// LocalDateTime date = LocalDateTime.parse(time, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
//// timeNeo4j=date.toInstant(ZoneOffset.of("+8")).toEpochMilli();
// if(timeL>=stamp){
// System.out.println("无需更新");
// return false;
// }
//
// }
//// System.out.println(timeNeo4j+"/"+stamp);
//
// return true;
// }
//
// /**
// * 删除向下关联1范围内的所有节点及关系,
// * match (n)-->(m) where n.id='ResourceTemplate-1133463217943609344' DETACH DELETE n,m
// * @param e
// */
// public void deleteById(Element e){
// String id=e.attributeValue("id");
// TDGraphApi tdGraphApi=new TDGraphApi();
// tdGraphApi.removeGraph(id);
// }
//
// public RemoteSaAuthHandler initConfig(){
//
//// RemoteSaAuthHandler rsh = new RemoteSaAuthHandler("graphsdk","q1w2e3r4","121.199.160.117",9000);
//// rsh.setAppInstance("150.158.75.213",8080,"TXPIMDB");
// RemoteSaAuthHandler rsh = new RemoteSaAuthHandler(user,password,loginId,loginPort);
// String token=rsh.loginGetSaToken();
// if (token==null||token.equals("")){
// rsh.loginGetSaToken();
// }
// //登录获取token
// rsh.setAppInstance(serverId,serverPort,dbName);
// System.out.println("登录数据"+rsh);
// return rsh;
// }
//
// /**
// * 处理返回数据
// * @param response
// * @return
// */
// public String responseData(String response){
// System.out.println("response"+response);
// JSONObject jsonObject=JSONObject.parseObject(response);
// String data="";
// if (jsonObject.get("code").toString().equals("200")){
// data=jsonObject.getString("data");
// }else {
// System.out.println("错误信息:"+jsonObject.getString("data"));
// }
// return data;
// }
}
package com.tongda.tddopo.util;
import com.alibaba.fastjson.JSONObject;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Result;
import java.util.*;
public class GraphUtil {
/**
* neo4j返回结果 节点处理工具node
* return m,n
* @param result
* @return
*/
public static HashMap<String, JSONObject> nodesUtil(Result result){
HashMap<String,JSONObject> hashMapNode=new HashMap<>();
while (result.hasNext()) {
Map<String,Object> row = result.next();
for (Map.Entry<String,Object> column : row.entrySet()) {
Node node=(Node)column.getValue();
Iterator<Label> iterator=node.getLabels().iterator();
ArrayList<String> arrayList=new ArrayList<>();
while (iterator.hasNext()){
arrayList.add(iterator.next().toString());
}
JSONObject jsonObject=new JSONObject(node.getAllProperties());
jsonObject.put("tags",arrayList);
hashMapNode.put(column.getValue().toString(),jsonObject);
}
}
return hashMapNode;
}
/**
* neo4j返回结果 关系处理工具link
* 适用此方法需要查询时设置关系的步长,默认可以为1,如[r*1],保证返回值中是一个list
* return r
* @param result
* @return
*/
public static ArrayList<JSONObject> linksUtil(Result result){
ArrayList<JSONObject> relationList=new ArrayList<>();
while (result.hasNext()) {
Map<String,Object> row = result.next();
for (Map.Entry<String,Object> column : row.entrySet()) {
ArrayList<Relationship> arrayList=(ArrayList<Relationship>)column.getValue();
for (Relationship relationship :arrayList){
JSONObject jsonObject1=new JSONObject();
jsonObject1.put("source",relationship.getStartNode().getAllProperties().get("id"));
jsonObject1.put("target",relationship.getEndNode().getAllProperties().get("id"));
jsonObject1.put("type",relationship.getType().toString());
relationList.add(jsonObject1);
}
}
}
return relationList;
}
/**
* 步长为一的返回关系结果处理 r为1时
* @param result
* @return
*/
public static JSONObject LinksAndNodesUtil(Result result){
// System.out.println(result.resultAsString());
ArrayList<JSONObject> relationList=new ArrayList<>();
HashMap<String,JSONObject> hashMapNode=new HashMap<>();
while (result.hasNext()) {
Map<String,Object> row = result.next();
// System.out.println(row.toString());
for (Map.Entry<String,Object> column : row.entrySet()) {
// System.out.println(column.toString());
if (column.getKey().equals("r")){
// System.out.println(column.getValue());
List<Relationship> relationships=(List<Relationship>)column.getValue();
Relationship relationship=relationships.get(0);
JSONObject jsonObject1=new JSONObject();
jsonObject1.put("source",relationship.getStartNode().getProperty("id").toString());
jsonObject1.put("target",relationship.getEndNode().getProperty("id").toString());
jsonObject1.put("type",relationship.getType().toString());
relationList.add(jsonObject1);
}else {
Node node=(Node)column.getValue();
Iterator<Label> iterator=node.getLabels().iterator();
ArrayList<String> arrayList=new ArrayList<>();
while (iterator.hasNext()){
arrayList.add(iterator.next().toString());
}
JSONObject jsonObject=new JSONObject(node.getAllProperties());
jsonObject.put("tags",arrayList);
hashMapNode.put(column.getValue().toString(),jsonObject);
}
}
}
ArrayList<JSONObject> nodeList = new ArrayList();
Iterator iterator = hashMapNode.keySet().iterator();
String s;
while(iterator.hasNext()) {
s = (String)iterator.next();
JSONObject jsonObject = hashMapNode.get(s);
nodeList.add(jsonObject);
}
JSONObject returnJson = new JSONObject();
returnJson.put("nodes", nodeList);
returnJson.put("links", relationList);
return returnJson;
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tongda.tddopo.auto.Neo4jAutoConfiguration
\ No newline at end of file
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