Get node properties using a built-in cypher query in Java

there is currently a cypher request method

public static void RunQuery(String _query)
{
    Properties prop = new Properties();
    final String DB_PATH = "path/to/db"
    GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
    ExecutionEngine engine = new ExecutionEngine(graphDb);
    ExecutionResult result = engine.execute(_query);
    for(Map<String,Object> map : result)
    {
        System.out.println(map.toString());
    }
    graphDb.shutdown();


}

However, this only allows me to get these results:

{a=Node[11303]}
{a=Node[11341]}
{a=Node[11343]}
{a=Node[11347]}
{a=Node[11349]}
{a=Node[11378]}

How can I increase it to spit out all the query results, like the cypher shell does?

0
source share
2 answers

What your request looks like. The returned Map<String, Object>will have the key of the returned variable. The object can be Path, Node or Relationship, and this will only call native toString(), which in Java code will simply return the Node identifier. You must create your own printer, which will receive property keys and iterate over each of them.

for (String key : node.getPropertyKeys()) {
    System.out.println("Key: " + key + ", Value: " +  node.getProperty(key));
}
+2
source

RETURN .

, :

_query="Start x= node(someIndex) Match x-[rel:SOMETHING]-n Return n";

,

    ExecutionResult result = engine.execute(_query);
    Iterator<Node> n_column = result.columnAs("n");
            for (Node outputNode: IteratorUtil.asIterable(n_column)) {
                System.out.println(outputNode.getProperty("yourKey","defaultValueIfNull"));
            }
-1

Source: https://habr.com/ru/post/1536488/


All Articles