简体   繁体   中英

How to get all node's relationships in neo4j using Java Connector Jar

Here's my graph

在此处输入图片说明

I'm trying to get the details of the nodes and its relationship in java code and below is my code. I'm able to get the details of the nodes but not the relationships. And the java docs has no information and methods that allow exposing of the relationships.

My Code

import java.util.List;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Record;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.Value;
import org.neo4j.driver.internal.InternalResult;

public class Neo4jTest {

    private Driver driver;

    public Neo4jTest() {
        loadDriver();
    }

    private void loadDriver() {
        driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "Test"));
    }

    public Result createNodes(String query) {
        try (Session session = driver.session())
        {
            InternalResult result = (InternalResult) session.writeTransaction(tx -> tx.run(query));
            return result;
        }
    }

    public List<Record> execute(String query) {
        try (Session session = driver.session())
        {
            Result result = session.run(query);
            return result.list();
        }
    }

    public void cleanUp(String object) {
        String cleanUpQuery = "match(n:" + object +") detach delete n";
        execute(cleanUpQuery);
    }

    public void printResults(List<Record> results) {
        for (Record record : results) {
            Value rec = record.get("n");
            System.out.print("Record --> [");
            for (String key : rec.keys()) {
                System.out.print(key + " : " + rec.get(key) + ", ");
            }
            System.out.println("]");

            //TODO: Need to get all Relationship of this particular node
        }
    }

    public static void main(String[] args) {

        String createQuery = "create \n" + 
                "(a:People {name : \"Vivek\", age : 20}), \n" + 
                "(b:People {name : \"Abdul\", age : 25}), \n" + 
                "(c:People {name : \"John\", age : 22}),\n" + 
                "" +
                "(a)-[:KNOWS]->(b),\n" + 
                "(b)-[:FRIEND_OF]->(c),\n" + 
                "(c)-[:KNOWS]->(a)";

        String fetchQuery = "match(n:People) return n";

        Neo4jTest obj = new Neo4jTest();
        obj.cleanUp("People");

        // Create Nodes
        obj.createNodes(createQuery);

        // Fetch Nodes
        List<Record> results = obj.execute(fetchQuery);

        // Process Results
        obj.printResults(results);

    }
}

Output

Record --> [name : "Vivek", age : 20, ]
Record --> [name : "Abdul", age : 25, ]
Record --> [name : "John", age : 22, ]

JavaDoc Link -> https://neo4j.com/docs/api/java-driver/current/

Relevant Result Objects

org.neo4j.driver.Record;
org.neo4j.driver.Value;

Other Details

Neo4j Community Version - 3.5.14

Neo4j Java Driver Version - 4.0.0

Update

(for cybersam answer)

The first query doesn't return the relationship as part of the r variable. 在此处输入图片说明

[Update] And the second query also doesn't return relationship 在此处输入图片说明

Results in the neo4j browser and code are the same.

Output for query

MATCH (n:People) WITH COLLECT(n) AS nodes MATCH (:People)-[r]->(:People) WITH nodes, COLLECT(TYPE(r)) AS rels RETURN nodes, rels

在此处输入图片说明

Thanks for any help!

[UPDATED]

The current fetchQuery is only requesting and returning nodes.

You could use this instead (and make appropriate changes to printResults ):

String fetchQuery = "MATCH (n:People)-[r]-(m:People) RETURN n, r, m";

Or, you could use this (which returns separate lists for People nodes and the relationships between them, but is less efficient):

String fetchQuery =
  "MATCH (n:People) WITH COLLECT(n) AS nodes " +
  "MATCH (:People)-[r]->(:People) WITH nodes, COLLECT(r) AS rels" +
  "RETURN nodes, rels";

By the way, People should really be Person since each node is for 1 person.

[ADDENDUM]

To see the type and end nodes of each relationship:

String fetchQuery =
  "MATCH (a:People)-[r]->(b:People) " +
  "RETURN TYPE(r) AS type_r, a, b";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM