How To Connect Java : Spring MVC to Neo4J Graph Database

Categories Software Development

This is a simple stepwise approach to show the basic steps involved in connecting a typical Java application to Neo4J graph database. Your final output should look similar to what we have in the image below. The data displayed are the ones retrieved from Neo4J, the creation of the data is also covered later in this article.

demo code output
The final output of the demo code

The core tools used for this demo:
1. Maven for dependency management.
2. Spring MVC: for the core development
3. Neo4J-Java Driver for communicating with Neo4J

Step 1. Import Sample Data into Neo4J on Windows

My preference for Windows platform is basically because that is the OS of my development machine. I am going to be using a CSV data sample “book data” which you can download here. Place the data in CSV format inside the import folder in the Neo4J document directory (“…\Documents\Neo4j\default.graphdb\import\bookdatatest.csv”).
In a rare case, the import folder might be non-existent, then you will have to create it manually and copy the CSV file inside afterwards. You will run the following Cypher query, which should automatically import the data into the Neo4J database on your running Neo4J platform. Community version of Neo4J downloaded on the official website here


USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "file:///Bookdatatest.csv" as row CREATE (:Books{AmazonID:row.AMAZON,FileName:row.Filename,ImageURL:row.IMAGEURL,Title:row.Title,
AUTHOR:row.AUTHOR,CATEGORYID:row.CATEGORYID,CATEGORY:row.CATEGORY})
MERGE (a:Author {Name:row.AUTHOR})
MERGE (t:title {Name:row.TITLE})
MERGE (t)-[:WRITTENBY]-(a);

Neo4J also allows the importation of data in diverse formats like JSON, CSV as seen above or a plain text file with a delimiter of preference.
Step 2: Download Neo4J- Java Driver.

I am using the basic driver as it works just fine like others with little or no abstraction required at the user end. There are other forms of Java compatible drivers as listed here, which can be used to achieve the same purpose based on the preferred style of coding. The following code snippet below shows the basic way to connect with Neo4J using the aforementioned driver.

Driver driver = GraphDatabase.driver("bolt://localhost", AuthTokens.basic("username", "password"));
Session session = driver.session();
StatementResult result = session.run("your cypher command");
while(result.hasNext){
//process the retrieved result
}
session.close();
driver.close();

  1.  Supply your Neo4J URL and authentication credentials.
  2.  Open a session and run your cypher command which should be in string format.
  3.  Process the result returned by the ran session.
  4.  Close the session and the connection.

Step 3. Write A Sample Query to retrieve and display the data

You can download the complete code from the tutorial branch of this repository.

The complete demo project in the repository is a simple maven web project and should, therefore, run seamlessly.