Giter VIP home page Giter VIP logo

movies-java-jdbc's Introduction

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[role=include]

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

movies-java-jdbc's People

Contributors

jexp avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.