Neo4j Movies Demo App in java with jdbc
HTML Java
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

Readme.adoc

Neo4j Movies Example Application

Even if Neo4j is all about graphs, its graph query language Cypher is well suited to be used with JDBC (Java Database Connectivity). As you probably know, JDBC is a common way to connect to a datastore, especially since there is a lot of tooling and connectors written around it in the Business Intelligence, Data Migration and ETL world.

The Neo4j JDBC driver works with Neo4j server in version 1.9.x and 2.x and with embedded and in-memory databases. It allows you to (transactionally) execute parametrized Cypher statements against your Neo4j database to either create, query or update data.

The Stack

These are the components of our min- Web Application:

  • Application Type: Java-Web Application

  • Web framework: Spark-Java (Micro-Webframework)

  • Neo4j Database Connector: Neo4j-JDBC with Cypher

  • Database: Neo4j-Server

  • Frontend: jquery, bootstrap, d3.js

Endpoints:

Get Movie

// JSON object for single movie with cast
curl http://neo4j-movies.herokuapp.com/movie/The%20Matrix

// list of JSON objects for movie search results
curl http://neo4j-movies.herokuapp.com/search?q=matrix

// JSON object for whole graph viz (nodes, links - arrays)
curl http://neo4j-movies.herokuapp.com/graph

Setup

Spark is a micro-webframework to easily define routes for endpoints and provide their implementation. In our case the implementation calls the MovieService which has one method per endpoint that returns Java collections which are turned into JSON using the Google Gson library.

The MovieService uses the Neo4j-JDBC driver to execute queries against the transactional endpoint of Neo4j-Server. You add the dependency to the JDBC driver in your pom.xml:

link:pom.xml[]

To use the JDBC driver you provide a connection URL, e.g. jdbc:neo4j:localhost:7474, get a Connection which then can be used to create `PreparedStatement`s with your Cypher query.

You would then set parameters on your statement, please note that only numeric parameter-names are possible, starting from 1.

To access the ResultSet you call the cursor method rs.next() in a while loop, within which you can access the result-data by rs.getString("columnName") where the column name is the RETURN clause alias for each column.

Connection con = DriverManager.getConnection("jdbc:neo4j://localhost:7474/");

String query = "MATCH (:Movie {title:{1}})<-[:ACTED_IN]-(a:Person) RETURN a.name as actor";

try (PreparedStatement stmt = con.prepareStatement(query)) {

    stmt.setString(1,"The Matrix");

    try (ResultSet rs = stmt.executeQuery()) {
        while(rs.next()) {
             System.out.println(rs.getString("actor"));
        }
    }
}

Run locally:

Start your local Neo4j Server (Download & Install), open the Neo4j Browser. Then install the Movies data-set with :play movies, click the statement, and hit the triangular "Run" button.

Start this application with:

mvn compile exec:java

You can search for movies by title or and click on any entry.

Deploy to Heroku