java操作Neo4j

java操作Neo4j

pom.xml添加内容

 <dependencies>
    <!-- 服务器开发需要的jar包 -->
      <groupId>org.neo4j.driver</groupId>
      <artifactId>neo4j-java-driver</artifactId>
      <version>1.5.0</version>
    </dependency>
    <dependency>
    <!-- 嵌入式开发需要的jar包 -->
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j</artifactId>
        <version>3.3.4</version>
    </dependency>
  </dependencies>

Neo4j服务器开发

java代码:

import org.neo4j.driver.v1.*;

import static org.neo4j.driver.v1.Values.parameters;

/**
 * @author yejianyu
 * @date 2020/3/30
 */
public class Main {
    public static void main(String[] args) {
        Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j","123456"));
        Session session = driver.session();
        session.run("CREATE (n:Person {name: {name},title: {title}})",
            parameters( "name", "Arthur001", "title", "King001" ));
        StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = {name} " +
                "RETURN a.name AS name, a.title AS title",
            parameters( "name", "Arthur001" ) );
        while (result.hasNext()) {
            Record record = result.next();
            System.out.println( record.get( "title" ).asString() + " " + record.get( "name" ).asString() );
        }
        session.close();
        driver.close();
    }
}

嵌入式开发

java代码:

import org.neo4j.graphdb.*;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yejianyu
 * @date 2020/3/30
 */
public class Main2 {
    public static void main(String[] args) {
        //创建、加载数据库
        GraphDatabaseService graphdb = new GraphDatabaseFactory().newEmbeddedDatabase(new File("C://lsw/graph"));
        System.out.println("Database Load!");
        //开启事务
        try (Transaction tx = graphdb.beginTx()){
            Node steve = graphdb.createNode(Lables.USER);
            steve.setProperty("name","steve");
            Node linda = graphdb.createNode(Lables.USER);
            linda.setProperty("name","linda");
            steve.createRelationshipTo( linda, RelationshipTypes.IS_FRIEND_OF );
            System.out.println("created node name is " + steve.getProperty("name"));
            tx.success();
        }
        //查询数据库
        String query ="match (n:USER) return n.name as name";
        Map<String, Object > parameters = new HashMap<String, Object>();
        try ( Result result = graphdb.execute( query, parameters ) ) {
            while ( result.hasNext() ) {
                Map<String, Object> row = result.next();
                for ( String key : result.columns() ) {
                    System.out.printf( "%s = %s%n", key, row.get( key ) );
                }
            }
        }
        registerShutdownHook(graphdb);
        System.out.println("Database Shutdown!");
    }
    //设置标签,必须继承Label接口
    public enum Lables implements Label{
        USER, MOVIE;
    }
    public enum RelationshipTypes implements RelationshipType{
        IS_FRIEND_OF, HAS_SEEN;
    }

    private static void registerShutdownHook(final GraphDatabaseService graphDb){
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run(){
                graphDb.shutdown();
            }
        });
    }
}

---------本文结束感谢您的阅读---------

评论

 热烈欢迎各位大佬专家莅临玖涯博客指导检查!

 交换友链的朋友请前往友情链接

 热烈欢迎各位大佬专家莅临玖涯博客指导检查!

 交换友链的朋友请前往友情链接

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×