Skip to content

Neo4j Movies Example application with SparkJava backend using the neo4j-java-driver

License

Notifications You must be signed in to change notification settings

lydianeU/movies-java-bolt

 
 

Repository files navigation

Neo4j Movies Example Application

CI
movie application

This example application demonstrates how easy it is to get started with Neo4j in Java.

It is a very simple web application that uses our Movie graph dataset to provide a search with listing, a detail view and a graph visualization.

THe front-end is just jQuery and d3 and the backend is implemented in Java using spark-java a lightweight web framework.

Using Neo4j from Java is as easy as using any other database that offers a textual query language. With our binary protocol driver for the binary protocol, you get a clean API to manage your Session, Transaction and Statement and iterate over each Value of a Result with a rich conversion DSL.

We can select a database of a server to run against.

Note
The credentials and database are passed as part of the URL which is NOT an official feature but just handled within this app.

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-Java-Driver for Cypher Docs

  • Database: Neo4j-Server (4.x) with multi-database

  • Frontend: jquery, bootstrap, d3.js

Endpoints:

Get Movie

// JSON object for single movie with cast
curl http://localhost:8080/movie/The%20Matrix

// list of JSON objects for movie search results
curl http://localhost:8080/search?q=matrix

// JSON object for whole graph viz (nodes, links - arrays)
curl http://localhost:8080/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-Java driver to execute queries via Neo4j binary protocol "Bolt". You add the dependency to the Neo4j-Java-Driver driver in your pom.xml:

<dependency>
    <groupId>org.neo4j.driver</groupId>
    <artifactId>neo4j-java-driver</artifactId>
    <version>4.1.1</version>
</dependency>

To use the driver, you provide a connection URL, e.g. localhost, and credentials, get a Driver instance, from which you get a Session. The session allows you to execute Cypher statements directly and iterate over the StatementResults to access the resulting `Value`s.

With your statement you can provide parameters as a Java map.

Driver driver = GraphDatabase.driver("bolt://localhost");
String query = "MATCH (:Movie {title:$title})<-[:ACTED_IN]-(a:Person) RETURN a.name as actor";

try (Session session = driver.session()) {
    Result result = session.run(query, singletonMap("title", "The Matrix"));
    while (result.hasNext()) {
        System.out.println(result.next().get("actor"));
    }
}

Run locally

With our online demo server you can test this application:

mvn compile exec:java

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

Or better you can run Neo4j locally to experiment with your own data. Start your local Neo4j Server in Neo4j Desktop (Download & Install), open Neo4j Browser. After logging in, install the Movies graph data set by entering the :play movies command, click the CREATE-statement, and hit the triangular "Run" button.

Configuration options

Environment variable name Default value (or N/A)

PORT

8080

NEO4J_URI

neo4j+s://demo.neo4jlabs.com

NEO4J_USER

movies

NEO4J_PASSWORD

movies

NEO4J_DATABASE

movies

About

Neo4j Movies Example application with SparkJava backend using the neo4j-java-driver

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 38.1%
  • HTML 35.2%
  • JavaScript 26.7%