Goals If you are a Java developer, this guide provides an overview of options for connecting to Neo4j. While this guide is not comprehensive, it will introduce the different APIs and link to the relevant resources. Prerequisites You should be… Learn More →

Goals
If you are a Java developer, this guide provides an overview of options for connecting to Neo4j. While this guide is not comprehensive, it will introduce the different APIs and link to the relevant resources.
Prerequisites
You should be familiar with the graph database concepts and the property graph model. Having installed Neo4j and tried out our Cypher Query language helps too. When developing with Neo4j, please use JDK 7 or 8 and your favorite IDE.
Intermediate

java


Neo4j for Java Developers

Neo4j offers a rich set of integration possibilities for developers using Java or other JVM-languages.

The standalone Neo4j Server can be installed on any machine and then accessed via its binary “Bolt” protocol through our official driver or any bolt-enabled drivers.

The server’s additional HTTP API is used by many other integrations, using drivers or any other HTTP client library.

You can also extend Neo4j by implementing user defined procedures for Cypher in Java or other JVM languages, writing server extensions which act as REST-endpoints or by embedding Neo4j into your own Java applications.

Neo4j Java Driver

The Neo4j Java driver is officially supported by Neo4j and connects to the database using the binary protocol. It aims to be minimal, while being idiomatic to Java.

When using Maven, add this to your pom.xml file:

<groupId>org.neo4j.driver</groupId>

<artifactId>neo4j-java-driver</artifactId>

<version>1.0.0</version>

For Gradle or Grails, this is how to add the dependency:

compile 'org.neo4j.driver:neo4j-java-driver:1.0.0'

For other build systems, see information available at Maven Central.

import org.neo4j.driver.v1.*;



Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "neo4j" ) );

Session session = driver.session();



session.run( "CREATE (a:Person {name:'Arthur', title:'King'})" );



StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title" );

while ( result.hasNext() )

{

    Record record = result.next();

    System.out.println( record.get( "title" ).asString() + " " + record.get("name").asString() );

}



session.close();

driver.close();

Name

Version

Authors

neo4j-java-driver

1.0.0

The Neo4j Team

Package

Example

StackOverflow

Docs

API

Source

Spring Data Neo4j

sdn

Neo4j’s Spring Data integration was the founding project of the Spring Data efforts, started by Rod Johnson and Emil Eifrem. Spring Data Neo4j was recently rewritten to incorporate the many improvements in Neo4j and Cypher but also in the Spring Data ecosystem.

It integrates tightly with the Spring Framework and offers Object-Graph mapping on top of Neo4j.

Features (based on Neo4j-OGM):

  • Spring Boot Integration (since 1.4)
  • annotation based object graph mapping
  • interface based repository support with annotated and derived finder methods
  • fast class metadata scanning
  • optimized management of data loading and change tracking for minimal data transfers
  • multiple transports: binary protocol, HTTP and embedded
  • persistence lifecycle events

More detail can be found on the dedicated Spring Data Neo4j page.

Object-Graph-Mapping with Neo4j-OGM

Developing Java Business Applications often requires mapping rich domain models to your database. The Neo4j-OGM library is a pure Java library that can persist (annotated) domain objects using Neo4j. It uses Cypher statements to handle those operations in Neo4j.

The OGM supports tracking changes to minimize necessary updates and transitive persistence (reading and updating neighborhoods of an object).

The connection to Neo4j handled by a driver layer, which can use the binary protocol, HTTP or Neo4j’s embedded APIs.

Authors

The Neo4j and GraphAware Teams

Package

http://maven.org

Source

https://github.com/neo4j/neo4j-ogm

Docs

http://neo4j.com/docs/ogm/java/stable

Article

http://neo4j.com/blog/neo4j-java-object-graph-mapper-released/

Example

https://github.com/neo4j-examples/neo4j-ogm-university

Using Neo4j’s Embedded Java API

Being implemented in Java you can use Neo4j’s Java APIs also directly in your programs, much like HSQL or Derby. This is great for unit testing, but also useful for very high performance requirements and single-tier setups.

Besides an object-oriented API to the graph database, working with Node, Relationship, and Path objects, it also offers highly customizable, high-speed traversal- and graph-algorithm implementations.

As a JVM developer you are also on the bright side when it comes to inserting large amounts of data into Neo4j. With its non-transactional, highly-concurrent batch-insertion APIs it can ingest billions of nodes and relationships in just minutess.

That’s why we also chose this approach for extending Neo4j.

User Defined Procedures

User Defined Procedures are now available within Cypher and encapsulate dedicated functionality.

Just by annotating methods of a Java class with @Procedure and deploying the resulting jar file into your Neo4j installation, you can make new functionality easily available within the query language.

To get started we provided a template project and documentation the developer manual.

More useful examples for user defined procedures can be found in our neo4j-contrib GitHub repository.

The Example Project

The Neo4j example project is a small, one page webapp for the movies database built into the Neo4j tutorial. The front-end page is the same for all drivers: movie search, movie details, and a graph visualization of actors and movies. Each backend implementation shows you how to connect to Neo4j from each of the different languages and drivers.

You can learn more about our small, consistent example project across many different language drivers here. You will find the implementations for all drivers as individual GitHub repositories, which you can clone and deploy directly.

Extending Neo4j Server with a REST Server Extension

Another extension point are Neo4j Server REST extensions. They encapsulate certain graph or business functionality behind a domain specific REST protocol which can be accessed using HTTP requests against the mounted extension points.

After declaring the dependencies to Neo4j and Neo4j-Server-APIs you can create REST-resources that are made available by Jersey at startup.

Within those resources you can get access to all Neo4j Java APIs via the injected dependencies to perform any graph operation.

Their results are then collected and rendered in the required format, e.g. JSON, XML, plain text or binary.

You test your server extensions using the neo4j-harness tooling which allows to start an in-process Neo4j server to test against.

This example project shows how you would do that for our movie application endpoints.

Using Neo4j Server with JDBC

Being a Java developer you’re probably familiar with JDBC as a way to work with relational databases, either directly or via abstractions like Spring’s JDBCTemplate or MyBatis. Many tools for ETL, Business Intelligence, and data management also use JDBC drivers to interact with relational databases.

As Cypher, like SQL, is a textual parametrizable query language that can return tabular results, it was possible for us to support the JDBC APIs and make a Neo4j-JDBC driver available. More details on how to use it can be found with our example project for Java-JDBC. There, we implement the backend as a minimal Java webapp that leverages Neo4j-JDBC to connect to Neo4j Server.

Authors

Rickard Öberg, Michael Hunger, Stefan Armbruster and many more

Package

http://m2.neo4j.org/content/repositories/releases/org/neo4j/neo4j-jdbc/

Source

https://github.com/neo4j-contrib/neo4j-jdbc

Example

https://github.com/neo4j-examples/movies-java-jdbc

Neo4j Community Drivers

The drivers below have been thankfully contributed by the Neo4j community. Many of these are fully featured and well maintained. But we don’t take any reponsibility for their fitness for use with the most recent Versions of Neo4j.

JCypher

JCypher provides seamlessly integrated Java access to Neo4j at different levels of abstraction:

At the topmost level of abstraction, JCypher allows you to map complex business domains to graph databases. You can take an arbitrarily complex graph of domain objects or POJOs and store it in a straightforward way into Neo4j for later retrieval. You do not need to modify your domain object classes in any way, there is also no need for annotations. JCypher provides a default mapping out of the box.

At the same level of abstraction, Domain Queries provide the power and expressiveness of queries on a graph database, while being formulated on domain objects or on types of domain objects, respectively. The true power of Domain Queries comes from the fact, that the graph of domain objects is backed by a graph database.

At the next lower level of abstraction – access to graph databases – is provided based on a generic graph model. While simple, the model allows you to easily navigate and manipulate graphs.

At the bottom level of abstraction, a native Java DSL in the form of a fluent Java API allows you to intuitively and comfortably formulate queries against Neo4j.

Author

Wolfgang Schützelhofer

Package

http://maven.org

Source

http://github.com/Wolfgang-Schuetzelhofer/jcypher

Docs

http://jcypher.iot-solutions.net/

Article

http://neo4j.com/blog/jcypher-focus-on-your-domain-model-not-how-to-map-it-to-the-database/

Groovy & Grails: Neo4j Grails Plugin

grails

The goal of GORM for Neo4j is to provide a ‘as-complete-as-possible’ GORM implementation that maps domain classes and instances to the Neo4j nodespace. The following features are supported:

  • Marshalling from Neo4j Nodes to Groovy types and back again
  • Support for GORM dynamic finders, criteria and named queries
  • Session-managed transactions
  • Access to Neo4j’s traversal capabilities
  • Access the Neo4j graph database in all flavours (Embedded, REST and HA)

Authors

Stefan Armbruster, Graeme Rocher

Package

http://www.grails.org/plugin/neo4j

Source

https://github.com/grails/grails-data-mapping/tree/master/grails-datastore-gorm-neo4j

Docs

http://grails.github.io/grails-data-mapping/latest/neo4j/

Clojure: Neocons

clojure

An idiomatic, feature-rich Clojure client which supports (almost) all Neo4k REST API features and is constantly tested against bleeding edge server changes, like the Cypher language improvements.

Authors

Michael Klishin, Rohit Aggarwal

Package

https://clojars.org/clojurewerkz/neocons

Source

https://github.com/michaelklishin/neocons

Docs

http://clojureneo4j.info/

Scala: AnormCypher

scala

A Cypher-oriented Scala library modeled after the Play! framework’s Anorm library.

Author

Eve Freeman

Source

https://github.com/AnormCypher/AnormCypher

Docs

http://anormcypher.org

JPA: Hibernate OGM

Hibernate Object/Grid Mapper (OGM) with Neo4j Support.

Authors

Davide D’Alto, Gunnar Moelling, Emmanuel Bernard

Package

http://maven.org

Source

https://github.com/hibernate/hibernate-ogm/tree/master/neo4j

Docs

http://docs.jboss.org/hibernate/ogm/5.0/reference/en-US/html_single/#ogm-neo4j

Article

Blog, JPL Queries

Example

https://github.com/TimmyStorms/hibernate-ogm-neo4j-example