Neo4j Cypher - API Example


In the previous chapter, we have already discussed on how to develop and test a Java Application using Neo4j Native Java API. Now We will discuss about Neo4j Cypher Java API in this chapter.

Before developing application, please refer "Neo4j Java Environment Setup" chapter to setup Eclipse IDE to develop this application

If you observe the Neo4j Native Java API approach, it is very tedious and cumbersome to develop large applications. So to avoid this complexity, Neo4j has introduced another set of Java API.

This Java API is used to execute Neo4j CQL commands directly. It is similar to JDBC API to execute SQL commands directly.

Neo4j Cypher Java API Example

This example demonstrates how to develop a Java application in Eclipse IDE to develop and test Neo4j Cypher Java API Example

Step 1 - Create a Java class JavaNeo4jCQLRetrivalTest

Neo4j CQL Tutorial

Now start writing Neo4j Java API coding to perform Neo4j DB operations

Step 2 - Create a Neo4j Database

GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
GraphDatabaseService db= dbFactory.newEmbeddedDatabase("C:/TPNeo4jDB");

It creates a Schema/Database for us at the specified path as shown below.This is similar to "CREATE DATABASE" command of Oracle SQL.

Neo4j CQL Tutorial

Step 3 - Create a Neo4j Cypher Execution Engine.It is used to execute Neo4j CQL commands in Java Applications.

ExecutionEngine execEngine = new ExecutionEngine(graphDb);

Step 4 - By using Neo4j Cypher Execution Engine, execute Neo4j CQL Command to retrieve results of CQL MATCH command.

ExecutionResult execResult = execEngine.execute
   ("MATCH (java:JAVA) RETURN java");

Step 5 - Get the CQL Command results in a String to print the results in the console

String results = execResult.dumpToString();

Step 6 - Final source code.


import org.neo4j.cypher.javacompat.ExecutionEngine;
import org.neo4j.cypher.javacompat.ExecutionResult;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

public class JavaNeo4jCQLRetrivalTest {
   public static void main(String[] args) {
      GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory();

      GraphDatabaseService graphDb = graphDbFactory.newEmbeddedDatabase("C:/TPNeo4jDB");

      ExecutionEngine execEngine = new ExecutionEngine(graphDb);
      ExecutionResult execResult = execEngine.execute("MATCH (java:JAVA) RETURN java");
      String results = execResult.dumpToString();

Step 7 - Before executing this Java Program, check your Neo4j is in Shutdown mode or not. If not, please click on "Stop" button to showdown it.

Neo4j CQL Tutorial

Step 8 - Execute the Java Program and observe the output in Eclipse IDE Console.

Neo4j CQL Tutorial

Select our Neo4j Database folder and click on "Start" button

Neo4j CQL Tutorial

Once This DB is started successfully, access Neo4j Browser by clicking on "http://localhost:7474" link to observe our data.

Neo4j CQL Tutorial

Step 9 - Type below command at $ prompt of Neo4j Data Browser

MATCH (java:JAVA) RETURN java.TutorialID,java.Title,
Neo4j CQL Tutorial

View the results in the Data Browser

Neo4j CQL Tutorial

If we observe Eclipse IDE results and Neo4j Data Browser results, both are correct.


Like this, we can execute any CQL Commands by using Neo4j JAVA API.

If our Neo4j Server is up and running by referring our newly created database, then we cannot execute our program as the server is already got lock for this database.

So when we execute our previous program, we will get some error stack trace Couldn't lock lock file C:\TPNeo4jDB\lock because another process already holds the lock.

To avoid this issue, first stop our server, then execute the program.

Because by default Neo4j DB Server accepts only one lock at a time. In Realtime applications, Ne04J DBA people will update DB properties to allow some number of locks at a time.