JAVA-API operation of graph database Neo4j learning

Neo4j series

1. Basic understanding of graph database Neo4j learning essays
2. Core content of graph database Neo4j learning essays
3. Basic operations of graph database Neo4j learning essays
4. Advanced operations of graph database Neo4j learning essays
5. JAVA-API for graph database Neo4j learning Operation
6. SpringBoot integration of graph database Neo4j learning



foreword

I talked about Neo4j and core concepts, basic additions, deletions, modifications, and advanced operations, but the project mainly relies on the backend to obtain data and provide it to the frontend for display, so it is unavoidable to connect Neo4j to operate through the development language. I am a pure Pure JAVA siege lion, use JAVA to operate.

1. JAVA Driver version selection

With the continuous version iteration of Neo4j , the latest version announced on the official website is already 5.X , so the corresponding JAVA Driver version will also change accordingly. For the specific version, see the screenshot below.
The latest version is 5.x , but the corresponding Java version is also 17. Generally, the latest version is not used, so this article takes 4.4 as the standard, so Java 8 or 11 is fine.

Driver Correspondence
Code cloud address fast channel

The project still uses Maven to manage the project and introduces the java driver driver package.

<dependency>
    <groupId>org.neo4j.driver</groupId>
    <artifactId>neo4j-java-driver</artifactId>
    <version>4.0.0</version>
</dependency>

2. Get the link

Similar to MySQL and SQL Server databases, the JAVA driver provides methods for obtaining links.

/**
 * @param uri
 * @param username
 * @param password
 * @return 获取Neo4j链接
 */
private static Session getSession(String uri, String username, String password){
    
    
    Driver driver = GraphDatabase.driver(uri, AuthTokens.basic(username, password));
    Session session = driver.session();
    if(session.isOpen()){
    
    
        System.out.println("连接Neo4j成功!" + session);
        return session;
    }else{
    
    
        System.out.println("连接Neo4j失败!");
    }
    return null;
}

Results of the:
Connect Neo4j Test

3. Add, delete, modify and check operations

3.1. New nodes and relationships

3.1.1 Adding new nodes

Mainly use the native way to construct a Query object and send the Create Node statement.

private static void createNode(String... values){
    
    
    Driver driver = getDriver(URI, USERANME, PASSWORD);
    if(Objects.nonNull(driver)){
    
    
        Session session = driver.session();
        for (String value : values) {
    
    
            Query query = new Query("create (n:loveperson) set n.name = $name return id(n)", Values.parameters("name", value));
            Result result = session.run(query);
            int id = result.single().get(0).asInt();
            System.out.println(id);
        }
        session.close();
    }
    driver.close();
}

Use the main() method to create a node, and successfully output two ids of 152 and 153, indicating that the creation of the node is successful. The following query is used to verify whether there is a node with id = 153.

insert image description here

3.1.2 Add relationship

Or send a create relationship statement.

private static void createLink(int id1, int id2){
    
    
    Driver driver = getDriver(URI, USERANME, PASSWORD);
    if(Objects.nonNull(driver)) {
    
    
        Session session = driver.session();
        Query query = new Query("match (n:loveperson),(m:loveperson) where id(n)=$id1 and id(m)=$id2\n" +
                "create (n)-[rel:`夫妻`]->(m) return rel", Values.parameters("id1", id1, "id2", id2));
        Result result = session.run(query);
        String s = result.single().get(0).asRelationship().toString();
        System.out.println(s);
        session.close();
    }
    driver.close();
}

3.2. Delete nodes and relationships

3.2.1 Delete relationship

Delete the relationship, why delete the relationship first, because if the relationship is not deleted first, the node cannot be deleted, as long as MySQL is associated with a foreign key, then this record cannot be deleted.

private static void deleteLink(int id){
    
    
    Driver driver = getDriver(URI, USERANME, PASSWORD);
    if(Objects.nonNull(driver)) {
    
    
        Session session = driver.session();
        Query query = new Query("match (n:loveperson)-[rel]->(m:loveperson) where id(rel)=$id delete rel", Values.parameters("id", id));
        session.run(query);
        session.close();
    }
    driver.close();
}

3.2.2 Delete Node

private static void deleteNode(int id){
    
    
    Driver driver = getDriver(URI, USERANME, PASSWORD);
    if(Objects.nonNull(driver)) {
    
    
        Session session = driver.session();
        Query query = new Query("match (n:loveperson) where id(n)=$id delete n", Values.parameters("id", id));
        session.run(query);
        session.close();
    }
    driver.close();
}

3.3. Modify nodes and relationships

3.3.1 Modifying Relationships

To modify a relationship is to delete the original relationship. Big head Liu met a confidant and was tempted, so he betrayed Wang Yun, who had a big face. They broke the original relationship between husband and wife and established a relationship of betrayal.

private static void updateLink(int id){
    
    
    Driver driver = getDriver(URI, USERANME, PASSWORD);
    if(Objects.nonNull(driver)) {
    
    
        Session session = driver.session();
        Query query = new Query("match (n:loveperson)-[rel]->(m:loveperson) where id(rel)=$id\n" +
                "create (n)-[r:背叛]->(m)\n" +
                "delete rel\n" +
                "return type(r)", Values.parameters("id", id));
        Result result = session.run(query);
        String s = result.single().get(0).toString();
        System.out.println(s);
        session.close();
    }
    driver.close();
}

3.3.2 Modify Node

Ever since Big Head Liu betrayed Wang Yun, Wang Yun's hobby has been to die to people's hearts.

private static void updateNode(int id){
    
    
    Driver driver = getDriver(URI, USERANME, PASSWORD);
    if(Objects.nonNull(driver)) {
    
    
        Session session = driver.session();
        Query query = new Query("match (n:loveperson) where id(n)=$id set n.hobby = '心已死' return n", Values.parameters("id", id));
        Result result = session.run(query);
        String s = result.single().get(0).asNode().get("name").asString();
        System.out.println(s);
        session.close();
    }
    driver.close();
}

3.4. Query nodes and relationships

3.4.1 Query Node

private static void createNode(String... values){
    
    
Driver driver = getDriver(URI, USERANME, PASSWORD);
 if(Objects.nonNull(driver)){
    
    
     Session session = driver.session();
     for (String value : values) {
    
    
         Query query = new Query("create (n:loveperson) set n.name = $name return id(n)", Values.parameters("name", value));
         Result result = session.run(query);
         int id = result.single().get(0).asInt();
         System.out.println(id);
     }
     session.close();
 }
 driver.close();
}

3.4.2 Query Relationship

private static void queryLink(){
    
    
    Driver driver = getDriver(URI, USERANME, PASSWORD);
    Session session = driver.session();
    if(Objects.nonNull(session)){
    
    
        Query query = new Query("match (n:loveperson)-[rel]->(m:loveperson) return rel");
        List<String> list = session.beginTransaction().run(query).list(record -> {
    
    
            return record.get(0).asRelationship().type();
        });
        list.forEach(System.out::println);
        session.close();
        driver.close();
    }
}

4. Summary

In fact, you need to learn the basic operations, so the Java connection is very simple. The Java Driver is just an auxiliary operation tool, and there is no technical content. Block a lot.
However, it is relatively easy to use a simple Java API, and no one in real development is willing to write this way, because Spring Data still integrates the operation of Neo4j.

Guess you like

Origin blog.csdn.net/qq_19283249/article/details/128263747