1

这是我的图表

在此处输入图像描述

我正在尝试获取节点的详细信息及其在 java 代码中的关系,下面是我的代码。我能够获取节点的详细信息,但不能获取关系。并且 java 文档没有允许暴露关系的信息和方法。

我的代码

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);

    }
}

输出

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

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

相关结果对象

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

其他详情

Neo4j 社区版 - 3.5.14

Neo4j Java 驱动程序版本 - 4.0.0

更新

(对于cybersam的回答)

第一个查询不会将关系作为 r 变量的一部分返回。 在此处输入图像描述

[更新]第二个查询也没有返回关系 在此处输入图像描述

neo4j 浏览器中的结果和代码是一样的。

查询输出

MATCH (n:People) WITH COLLECT(n) AS 节点 MATCH (:People)-[r]->(:People) WITH 节点,COLLECT(TYPE(r)) AS rels 返回节点,rels

在此处输入图像描述

谢谢你的帮助!

4

1 回答 1

0

[更新]

当前fetchQuery只是请求和返回节点。

您可以改用它(并对 进行适当的更改printResults):

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

或者,您可以使用它(它为节点和它们之间的关系返回单独的列表People,但效率较低):

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

顺便说一句,People应该真的是Person因为每个节点都是 1 人。

[附录]

要查看每个关系的类型和结束节点:

String fetchQuery =
  "MATCH (a:People)-[r]->(b:People) " +
  "RETURN TYPE(r) AS type_r, a, b";
于 2020-03-21T17:30:46.113 回答