- Neo4j Tutorial
- Neo4j - Home
- Neo4j - Overview
- Neo4j - Data Model
- Neo4j - Environment Setup
- Neo4j - Building Blocks
- Neo4j CQL Write Clauses
- Neo4j - Merge Command
- Neo4j - Set Clause
- Neo4j - Delete Clause
- Neo4j - Remove Clause
- Neo4j - Foreach Clause
- Neo4j CQL Read Clause
- Neo4j - Match Clause
- Neo4j - Optional Match Clause
- Neo4j - Where Clause
- Neo4j - Count Function
- Neo4j CQL General Clauses
- Neo4j - Return Clause
- Neo4j - Order By Clause
- Neo4j - Limit Clause
- Neo4j - Skip Clause
- Neo4j - With Clause
- Neo4j - Unwind Clause
- Neo4j CQL Functions
- Neo4j - String Functions
- Neo4j - Aggregation Function
- Neo4j CQL Admin
- Neo4j - Backup & Restore
- Neo4j - Index
- Neo4j - Create Unique Constraint
- Neo4j - Drop Unique
- Neo4j Useful Resources
- Neo4j - Quick Guide
- Neo4j - Useful Resources
- Neo4j - Discussion
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
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.
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(); System.out.println(results);
Step 6 - Final source code.
package com.tp.neo4j.java.cql.examples; 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(); System.out.println(results); } }
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.
Step 8 - Execute the Java Program and observe the output in Eclipse IDE Console.
Select our Neo4j Database folder and click on "Start" button
Once This DB is started successfully, access Neo4j Browser by clicking on "http://localhost:7474" link to observe our data.
Step 9 - Type below command at $ prompt of Neo4j Data Browser
MATCH (java:JAVA) RETURN java.TutorialID,java.Title, java.NoOfChapters,java.Status
View the results in the Data Browser
If we observe Eclipse IDE results and Neo4j Data Browser results, both are correct.
NOTE -
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
java.io.IOException: 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.