Neo4J - 返回节点名称而不是ID

时间:2017-07-07 22:40:47

标签: neo4j nodes relationships

我想知道如何在Java控制台中返回节点名而不是节点ID。

控制台中显示以下输出:

enter image description here

所需的输出应如下所示:

enter image description here

只是没有所有信息,只有节点名称(等于Airportnames)。

我的Java代码如下所示:

    package com.routesNeo4j;

import org.neo4j.driver.v1.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by e on 11.06.17.
 */
public class Neo4JRouting implements AutoCloseable, Neo4J_Connector {

    static Driver driver;


    public Neo4JRouting(String startAirport, String destinationAirport, StatementResult shortestPath) {

        driver = GraphDatabase.driver("bolt://ec2-13-58-101-13.us-east-2.compute.amazonaws.com:7687",
                AuthTokens.basic("neo4j", "Einloggen_123"));

        try(Session session = driver.session()) {

            shortestPath = session.run("MATCH (a:" + startAirport.toLowerCase() + "), (b:" + destinationAirport.toLowerCase()
            + "), p = allShortestPaths((a)-[r*1..4]-(b)) UNWIND rels(p) AS rel RETURN nodes(p), sum(rel.weight) " +
                    "AS weight ORDER BY sum(rel.weight)");


            List<Record> storeList = storeList(shortestPath);
            while (shortestPath.hasNext()) {
                System.out.println(shortestPath.next().toString());
            }
            System.out.println(storeList);


        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    public List<Record> storeList(StatementResult statementResult) {

        List<Record> list = new ArrayList<>();
        while (statementResult.hasNext()) {

            list.add(statementResult.next());

        }

        return list;

    }

    @Override
    public Driver runDriver(String user, AuthToken basicAuthToken) throws Exception {
        return null;
    }

    @Override
    public void close() throws Exception {

    }

}

我期待着你的回答。非常感谢!

1 个答案:

答案 0 :(得分:0)

您返回的每一行都包含节点列表和权重。这就是您在查询中提出的问题,以及您获得的内容。所以你必须&#34;解包&#34;这会产生你想要的格式。

一些代码片段来显示我的意思:

        StatementResult vResult = vSession.run(aCypher);

        while (vResult.hasNext()) {
            Record vRecord = vResult.next();

            vMutator.pushNode("row");

            for (Pair <String, Value> vListEntry : vRecord.fields()) {
                process_listentry(vSession, vMutator, vListEntry.key(), vListEntry.value());
            }

            vMutator.popNode(); // row
        }

然后在process_listentry中:

private void process_listentry(Session vSession, IHDSMutator vMutator, String vKey, Value vValue) {
...
    else if (vValue.type().equals(vSession.typeSystem().NODE())){
        vMutator.pushNode(vKey);
        vMutator.addNode("_id", Long.toString(vValue.asNode().id()));
        for (String lLabel : vValue.asNode().labels()) {
            vMutator.addNode("_label", lLabel);
        }
        for (String lKey : vValue.asNode().keys()) {
            Value lValue = vValue.asNode().get(lKey);
            process_listentry(vSession, vMutator, lKey, lValue);
        }
        vMutator.popNode();
    }

... 但它确实取决于你在查询中提出的要求,因此必须解压...

希望这有帮助, 汤姆