All Projects → neo4j-examples → Movies Java Spring Boot Jdbc

neo4j-examples / Movies Java Spring Boot Jdbc

Neo4j Movies Demo App in java with spring-boot-jdbc

Labels

== Spring Boot Neo4j JDBC - Movies Example Application

Even if http://neo4j.org[Neo4j] is all about graphs, its graph query language http://neo4j.org/learn/cypher[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 are 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 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.

Here we integrate Neo4j-JDBC with Spring-Boot and the traditional Spring-JDBC (JDBCTemplate) and Spring Web-MVC to create the backend.

=== The Stack

These are the components of our mini Web Application:

=== 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

Spring Boot is a rapid application development environment, bundling necessary Spring modules into easy-to-use starter packages.

It is based on annotation-based Java config and pre-configuring the packages with sensible defaults.

You add the dependency to the JDBC driver and Spring-Boot-Starters to your pom.xml:

[source,xml]

org.springframework.boot spring-boot-starter-jdbc ${spring-boot.version} org.springframework.boot spring-boot-starter-web ${spring-boot.version} org.neo4j neo4j-jdbc 2.3.2 ----

For the backend we use a Spring-WebMVC @RestController whose service methods access an autowired JdbcTemplate.

Currently we get the NEO4J_URL from a system property, but it should be equally simple to get it from an entry in application.properties. If you configure a password with your Neo4j server please set the environment variable like this:

[source]

export NEO4J_URL=jdbc:neo4j:http://localhost:7474?user=neo4j,password=

Using the JDBC-template with Cypher is easy. Here is an example from the /movie endpoint:

[source,java]

@Autowired JdbcTemplate template;

String GET_MOVIE_QUERY = "MATCH (movie:Movie {title:{1}})" + " OPTIONAL MATCH (movie)<-[r]-(person:Person)\n" + " RETURN movie.title as title, collect({name: person.name, " + " job: head(split(lower(type(r)),'_')), role: r.roles}) as cast LIMIT 1";

@RequestMapping("/movie/{title}") public Map<String,Object> movie(@PathVariable("title") String title) { return template.queryForMap(GET_MOVIE_QUERY, title); }

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

You can use all known JdbcTemplate methods, like queryForObjects, queryForMap and RowMapper or ResultSetCallBack as you're used to.

=== Run locally:

Start your local Neo4j Server (http://neo4j.com/download[Download & Install]), open the http://localhost:7474[Neo4j Browser]. Set the initial password to log-in, please use the same password in the NEO4J_URL environment variable. Then install the Movies data-set with :play movies, click the statement, and hit the triangular "Run" button.

Start this application with:

[source,shell]

mvn spring-boot:run

Go to http://localhost:8080

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].