• neo4j图形数据库Java应用


    CREATE
    
    (陈冠希:明星     {名称: "陈冠希"}),
    
    (王菲:明星         {名称: "王菲"}),
    
    (李亚鹏:明星     {名称: "李亚鹏"}),
    
    (瞿颖:明星         {名称: "瞿颖"}),
    
    (张柏芝:明星     {名称: "张柏芝"}),
    
    (谢霆锋:明星     {名称: "谢霆锋"}),
    
    (周迅:明星         {名称: "周迅"}),
    
    (张亚东:明星     {名称: "张亚东"}),
    
    (窦唯:明星         {名称: "窦唯"}),
    
    (窦鹏:明星         {名称: "窦鹏"}),
    
    (李大齐:明星     {名称: "李大齐"}),
    
    (朴树:明星         {名称: "朴树"}),
    
    (窦颖:明星         {名称: "窦颖"}),
    
    (梁朝伟:明星     {名称: "梁朝伟"}),
    
    (刘嘉玲:明星     {名称: "刘嘉玲"}),
    
    (周迅)-[:旧爱]->(窦鹏),
    
    (周迅)-[:旧爱]->(李大齐),
    
    (周迅)-[:旧爱]->(朴树),
    
    (周迅)-[:旧爱]->(李亚鹏),
    
    (周迅)-[:绯闻]->(梁朝伟),
    
    (周迅)-[:绯闻]->(谢霆锋),
    
    (王菲)-[:离异]->(李亚鹏),
    
    (王菲)-[:旧爱]->(谢霆锋),
    
    (王菲)-[:离异]->(窦唯),
    
    (谢霆锋)-[:旧友]->(陈冠希),
    
    (谢霆锋)-[:离异]->(张柏芝),
    
    (张亚东)-[:制作人]->(朴树),
    
    (张亚东)-[:离异]->(窦颖),
    
    (张亚东)-[:制作人]->(王菲),
    
    (李亚鹏)-[:旧爱]->(瞿颖),
    
    (陈冠希)-[:旧爱]->(张柏芝),
    
    (瞿颖)-[:旧爱]->(张亚东),
    
    (窦唯)-[:堂兄弟]->(窦鹏),
    
    (窦唯)-[:兄妹]->(窦颖),
    
    (梁朝伟)-[:夫妻]->(刘嘉玲),
    
    (王菲)-[:好友]->(刘嘉玲)
    MATCH (n:明星) RETURN n

    效果:


    直接关系

    MATCH m=(:明星 {名称:"周迅"})-[*..1]-() RETURN m

    最短路径

    MATCH p = AllShortestPaths(
    
    (周迅:明星 {名称:"周迅"})-[*..6]-(王菲:明星 {名称:"王菲"})
    
    ) RETURN p

    效果:


    MATCH p=(()<-[:旧爱]-()-[:旧爱]->()) RETURN p LIMIT 50

    MATCH p=(()<-[:旧爱]-()<-[:旧爱]-()-[:旧爱]->()) RETURN p LIMIT 50

    MATCH p=(()<-[:旧爱]-()<-[:旧爱]-()) RETURN p LIMIT 50

    直接上代码.

    bolt连接:

    package com;
    
    import org.neo4j.driver.v1.AuthTokens;
    import org.neo4j.driver.v1.Driver;
    import org.neo4j.driver.v1.GraphDatabase;
    import org.neo4j.driver.v1.Session;
    import org.neo4j.driver.v1.StatementResult;
    import org.neo4j.driver.v1.Transaction;
    import org.neo4j.driver.v1.TransactionWork;
    
    import static org.neo4j.driver.v1.Values.parameters;
    
    public class HelloWorldExample implements AutoCloseable {
    	private final Driver driver;
    
    	public HelloWorldExample(String uri, String user, String password) {
    		driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));
    	}
    
    	@Override
    	public void close() throws Exception {
    		driver.close();
    	}
    
    	public void printGreeting(final String message) {
    		try (Session session = driver.session()) {
    			String greeting = session
    					.writeTransaction(new TransactionWork<String>() {
    						@Override
    						public String execute(Transaction tx) {
    							StatementResult result = tx
    									.run("CREATE (a:Greeting) "
    											+ "SET a.message = $message "
    											+ "RETURN a.message + ', from node ' + id(a)",
    											parameters("message", message));
    							return result.single().get(0).asString();
    						}
    					});
    			System.out.println(greeting);
    		}
    	}
    
    	public static void main(String... args) throws Exception {
    		try (HelloWorldExample greeter = new HelloWorldExample(
    				"bolt://localhost:7687", "neo4j", "123456")) {
    			greeter.printGreeting("hello, world");
    		}
    	}
    }
    package com;
    
    import static org.neo4j.driver.v1.Values.parameters;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.neo4j.driver.v1.AuthTokens;
    import org.neo4j.driver.v1.Driver;
    import org.neo4j.driver.v1.GraphDatabase;
    import org.neo4j.driver.v1.Record;
    import org.neo4j.driver.v1.Session;
    import org.neo4j.driver.v1.StatementResult;
    import org.neo4j.driver.v1.Value;
    
    public class Neo4jBoltAPIDBOperation {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Driver driver = GraphDatabase.driver("bolt://127.0.0.1:7687",
    				AuthTokens.basic("neo4j", "123456"));
    		Session session = driver.session();
    
    		session.run("CREATE (a:Person {name: {name}, title: {title}})",
    				parameters("name", "gm", "title", "King"));
    
    		StatementResult result = session.run(
    				"MATCH (a:Person) WHERE a.name = {name} "
    						+ "RETURN a.name AS name, a.title AS title",
    				parameters("name", "gm"));
    
    		while (result.hasNext()) {
    			Record record = (Record) result.next();
    			System.out.println(record.get("title").asString() + " "
    					+ record.get("name").asString());
    		}
    
    		session.close();
    		driver.close();
    	}
    
    }

    直接操作数据库文件(确保服务已关闭):

    package com;
    
    import java.io.File;
    
    import org.neo4j.graphdb.GraphDatabaseService;
    import org.neo4j.graphdb.Node;
    import org.neo4j.graphdb.Relationship;
    import org.neo4j.graphdb.Transaction;
    import org.neo4j.graphdb.factory.GraphDatabaseFactory;
    
    public class Neo4jJavaAPIDBOperation {
    	public static void main(String[] args) {
    		GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
    		GraphDatabaseService db = dbFactory.newEmbeddedDatabase(new File("E:/neo4j-community-3.3.5/data/databases/graph.db"));
    		try (Transaction tx = db.beginTx()) {
    
    			Node javaNode = db.createNode(Tutorials.JAVA);
    			javaNode.setProperty("TutorialID", "JAVA001");
    			javaNode.setProperty("Title", "Learn Java");
    			javaNode.setProperty("NoOfChapters", "25");
    			javaNode.setProperty("Status", "Completed");
    
    			Node scalaNode = db.createNode(Tutorials.SCALA);
    			scalaNode.setProperty("TutorialID", "SCALA001");
    			scalaNode.setProperty("Title", "Learn Scala");
    			scalaNode.setProperty("NoOfChapters", "20");
    			scalaNode.setProperty("Status", "Completed");
    
    			Relationship relationship = javaNode.createRelationshipTo(
    					scalaNode, TutorialRelationships.JVM_LANGIAGES);
    			relationship.setProperty("Id", "1234");
    			relationship.setProperty("OOPS", "YES");
    			relationship.setProperty("FP", "YES");
    
    			tx.success();
    		}
    		System.out.println("Done successfully");
    	}
    }
    
    package com;
    
    import org.neo4j.graphdb.Label;
    public enum Tutorials implements Label {
    	JAVA,SCALA,SQL,NEO4J;
    }
    package com;
    
    import org.neo4j.graphdb.RelationshipType;
    
    public enum TutorialRelationships implements RelationshipType {
    	JVM_LANGIAGES, NON_JVM_LANGIAGES;
    }
    
    <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.test</groupId>
    	<artifactId>neo4j</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<dependencies>
    		<dependency>
    			<groupId>org.neo4j</groupId>
    			<artifactId>neo4j</artifactId>
    			<version>3.3.5</version>
    		</dependency>
    		<dependency>
    			<groupId>org.neo4j.driver</groupId>
    			<artifactId>neo4j-java-driver</artifactId>
    			<version>1.4.4</version>
    		</dependency>
    	</dependencies>
    	<build />
    </project>


    与Spring集成未完待续、、、





  • 相关阅读:
    Ubuntu 20.04 国内下载eclipse 网速慢解决方案
    Ubuntu20.04+GeForce RTX 2080 SUPER+cuda11.1+cudnn8.0.4+openCV4.4.0编译
    计算机专业考研复试面试----计算机组成原理
    计算机专业考研复试面试----操作系统
    Gabor滤波(个人学习)
    局部保留投影算法(个人学习)
    【转载】matlab常用函数
    latex常见的错误(自己经常出现的)
    latex常用的宏包(转载)
    C语言基础知识
  • 原文地址:https://www.cnblogs.com/gmhappy/p/9472432.html
Copyright © 2020-2023  润新知