4.8. Cypher Queries

In Java, you can use the query language like this:

AbstractGraphDatabase db = new EmbeddedGraphDatabase( DB_PATH );
ExecutionEngine engine = new ExecutionEngine( db );
ExecutionResult result = engine.execute( "start n=node(0) where 1=1 return n" );
System.out.println( result );
[Note]Note

The classes used here are from the org.neo4j.cypher.javacompat package, see link to the Java API below.

Which will output:

+-----------+
| n         |
+-----------+
| Node[0]{} |
+-----------+
1 rows, 0 ms

You can get a list of columns in the result:

List<String> columns = result.columns();
System.out.println( columns );

This outputs:

[n]

To fetch the result items in a single column, do like this:

Iterator<Node> n_column = result.columnAs( "n" );
for ( Node node : asIterable( n_column ) )
{
    System.out.println( node );
}

In this case there’s only one node in the result:

Node[0]

Full source code of the example: JavaQuery.java

For more information on the Java interface to Cypher, see the Java API.