1

currently have a cypher query 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 results like this:

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

How can I augment it to spit out the entire query results like the cypher shell does?

2 Answers 2

3

What does your query look like. The Map<String, Object> that is returned will have a key of the variable you return. The Object can be a Path, Node, or Relationship, and this will just call the native toString() on them, which in Java code just returns the Node ID. You have to build your own printer, something that will get the property keys and iterate through each of them.

for (String key : node.getPropertyKeys()) {
    System.out.println("Key: " + key + ", Value: " +  node.getProperty(key));
}
Sign up to request clarification or add additional context in comments.

4 Comments

yes, I am looking for if the object is a node for it to print out the properties and values and if it is a value (such as a count) for it not to. Essentially I want to recreate the console in the web interface or the neo4j-shell.
It's up to you to print out the properties, I don't believe Neo4j provides any convenience method for doing this, but you can do it in Java, I have modified my answer to include code for doing this.
Thanks Nicholas! In your code, what is r and what is relationshipDto?
I have updated to code to make the object more have verbose names
-1

It depends on the RETURN of your query..

For example, if you have a query like this:

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

So, your code can be

    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 Comment

this assumes that you know apriori that n is being returned. I just want to return the string results of the query in order to replicate the neo4j-shell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.