Quarkus - Neo4j

Neo4j is a graph database management system developed by Neo4j, Inc. Neo4j is a native graph database focused not only on the data itself, but especially on the relations between data. Neo4j stores data as a property graph, which consists of vertices or nodes as we call them, connected with edges or relationships. Both of them can have properties.

Neo4j offers Cypher, a declarative query language much like SQL. Cypher is used to for both querying the graph and creating or updating nodes and relationships. As a declarative language it used to tell the database what to do and not how to do it.

Learn more about Cypher in the Neo4j Cypher manual. Cypher is not only available in Neo4j, but for example coming to Apache Spark. A spec called OpenCypher is available, too.

Clients communicate over the so called Bolt protocol with the database.

Neo4j - as the most popular graph database according to DB-Engines ranking - provides a variety of drivers for various languages.

The Quarkus Neo4j extension is based on the official Neo4j Java Driver. The extension provides an instance of the driver configured ready for usage in any Quarkus application. You will be able to issue arbitrary Cypher statements over Bolt with this extension. Those statements can be simple CRUD statements as well as complex queries, calling graph algorithms and more.

The driver itself is released under the Apache 2.0 license, while Neo4j itself is available in a GPL3-licensed open-source “community edition”, with online backup and high availability extensions licensed under a closed-source commercial license.

This technology is considered preview.

In preview, backward compatibility and presence in the ecosystem is not guaranteed. Specific improvements might require to change configuration or APIs and plans to become stable are under way. Feedback is welcome on our mailing list or as issues in our GitHub issue tracker.

For a full list of possible extension statuses, check our FAQ entry.

Programming model

The driver and thus the Quarkus extension support three different programming models:

  • Blocking database access (much like standard JDBC)

  • Asynchronous programming based on JDK’s completable futures and related infrastructure

  • Reactive programming based on Reactive Streams

The reactive programming model is only available when connected against a 4.0+ version of Neo4j. Reactive programming in Neo4j is fully end-to-end reactive and therefore requires a server that supports backpressure.

In this guide you will learn how to

  • Add the Neo4j extension to your project

  • Configure the driver

  • And how to use the driver to access a Neo4j database

This guide will focus on asynchronous access to Neo4j, as this is ready to use for everyone. At the end of this guide, there will be a reactive version, which needs however a 4.0 database version.

The domain

As with some of the other guides, the application shall manage fruit entities.

  1. package org.acme.neo4j;
  2. public class Fruit {
  3. public Long id;
  4. public String name;
  5. public Fruit() {
  6. // This is needed for the REST-Easy JSON Binding
  7. }
  8. public Fruit(String name) {
  9. this.name = name;
  10. }
  11. public Fruit(Long id, String name) {
  12. this.id = id;
  13. this.name = name;
  14. }
  15. }

Prerequisites

To complete this guide, you need:

  • JDK 1.8+ installed with JAVA_HOME configured appropriately

  • an IDE

  • Apache Maven 3.6.2+

  • Access to a Neo4j Database

  • Optional Docker for your system

Setup Neo4j

The easiest way to start a Neo4j instance is a locally installed Docker environment.

  1. docker run --publish=7474:7474 --publish=7687:7687 -e 'NEO4J_AUTH=neo4j/secret' neo4j:4.0.0

This starts a Neo4j instance, that publishes its Bolt port on 7687 and a web interface on http://localhost:7474.

Have a look at the download page for other options to get started with the product itself.

Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

Clone the Git repository: git clone https://github.com/quarkusio/quarkus-quickstarts.git, or download an archive.

The solution is located in the neo4j-quickstart directory. It contains a very simple UI to use the JAX-RS resources created here, too.

Creating the Maven project

First, we need a new project. Create a new project with the following command:

  1. mvn io.quarkus:quarkus-maven-plugin:1.7.6.Final:create \
  2. -DprojectGroupId=org.acme \
  3. -DprojectArtifactId=neo4j-quickstart \
  4. -Dextensions="neo4j,resteasy-jsonb"
  5. cd neo4j-quickstart

It generates:

  • the Maven structure

  • a landing page accessible on [http://localhost:8080](http://localhost:8080)

  • example Dockerfile files for both native and jvm modes

  • the application configuration file

  • an org.acme.datasource.GreetingResource resource

  • an associated test

The Neo4j extension has been added already to your pom.xml. In addition, we added resteasy-jsonb, which allows us to expose Fruit instances over HTTP in the JSON format via JAX-RS resources. If you have an already created project, the neo4j extension can be added to an existing Quarkus project with the add-extension command:

  1. ./mvnw quarkus:add-extension -Dextensions="neo4j"

Otherwise, you can manually add this to the dependencies section of your pom.xml file:

  1. <dependency>
  2. <groupId>io.quarkus</groupId>
  3. <artifactId>quarkus-neo4j</artifactId>
  4. </dependency>

Configuring

The Neo4j driver can be configured with standard Quarkus properties:

src/main/resources/application.properties

  1. quarkus.neo4j.uri = bolt://localhost:7687
  2. quarkus.neo4j.authentication.username = neo4j
  3. quarkus.neo4j.authentication.password = secret

You’ll recognize the authentication here that you passed on to the docker command above.

Having done that, the driver is ready to use, there are however other configuration options, detailed below.

Using the driver

General remarks

The result of a statement consists usually of one or more org.neo4j.driver.Record. Those records contain arbitrary values, supported by the driver. If you return a node of the graph, it will be a org.neo4j.driver.types.Node.

We add the following method to the Fruit, as a convenient way to create them:

  1. public static Fruit from(Node node) {
  2. return new Fruit(node.id(), node.get("name").asString());
  3. }

Add a FruitResource skeleton like this and @Inject a org.neo4j.driver.Driver instance:

src/main/java/org/acme/neo4j/FruitResource.java

  1. package org.acme.neo4j;
  2. import javax.inject.Inject;
  3. import javax.ws.rs.Consumes;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.core.MediaType;
  7. import org.neo4j.driver.Driver;
  8. @Path("fruits")
  9. @Produces(MediaType.APPLICATION_JSON)
  10. @Consumes(MediaType.APPLICATION_JSON)
  11. public class FruitResource {
  12. @Inject
  13. Driver driver;
  14. }

Reading nodes

Add the following method to the fruit resource:

src/main/java/org/acme/neo4j/FruitResource.java

  1. @GET
  2. public CompletionStage<Response> get() {
  3. AsyncSession session = driver.asyncSession(); (1)
  4. return session
  5. .runAsync("MATCH (f:Fruit) RETURN f ORDER BY f.name") (2)
  6. .thenCompose(cursor -> (3)
  7. cursor.listAsync(record -> Fruit.from(record.get("f").asNode()))
  8. )
  9. .thenCompose(fruits -> (4)
  10. session.closeAsync().thenApply(signal -> fruits)
  11. )
  12. .thenApply(Response::ok) (5)
  13. .thenApply(ResponseBuilder::build);
  14. }
1Open a new, asynchronous session with Neo4j
2Execute a query. This is a Cypher statement.
3Retrieve a cursor, list the results and create Fruits.
4Close the session after processing
5Create a JAX-RS response

Now start Quarkus in dev mode with:

  1. ./mvnw compile quarkus:dev

and retrieve the endpoint like this

  1. curl localhost:8080/fruits

There are not any fruits, so let’s create some.

Creating nodes

The POST method looks similar. It uses transaction functions of the driver:

src/main/java/org/acme/neo4j/FruitResource.java

  1. @POST
  2. public CompletionStage<Response> create(Fruit fruit) {
  3. AsyncSession session = driver.asyncSession();
  4. return session
  5. .writeTransactionAsync(tx -> tx
  6. .runAsync("CREATE (f:Fruit {name: $name}) RETURN f", Values.parameters("name", fruit.name))
  7. .thenCompose(fn -> fn.singleAsync())
  8. )
  9. .thenApply(record -> Fruit.from(record.get("f").asNode()))
  10. .thenCompose(persistedFruit -> session.closeAsync().thenApply(signal -> persistedFruit))
  11. .thenApply(persistedFruit -> Response
  12. .created(URI.create("/fruits/" + persistedFruit.id))
  13. .build()
  14. );
  15. }

As you can see, we are now using a Cypher statement with named parameters (The $name of the fruit). The node is returned, a Fruit entity created and then mapped to a 201 created response.

A curl request against this path may look like this:

  1. curl -v -X "POST" "http://localhost:8080/fruits" \
  2. -H 'Content-Type: application/json; charset=utf-8' \
  3. -d $'{
  4. "name": "Banana"
  5. }'

The response contains an URI that shall return single nodes.

Read single nodes

This time, we ask for a read-only transaction. We also add some exception handling, in case the resource is called with an invalid id:

src/main/java/org/acme/neo4j/FruitResource.java

  1. @GET
  2. @Path("{id}")
  3. public CompletionStage<Response> getSingle(@PathParam("id") Long id) {
  4. AsyncSession session = driver.asyncSession();
  5. return session
  6. .readTransactionAsync(tx -> tx
  7. .runAsync("MATCH (f:Fruit) WHERE id(f) = $id RETURN f", Values.parameters("id", id))
  8. .thenCompose(fn -> fn.singleAsync())
  9. )
  10. .handle((record, exception) -> {
  11. if(exception != null) {
  12. Throwable source = exception;
  13. if(exception instanceof CompletionException) {
  14. source = ((CompletionException)exception).getCause();
  15. }
  16. Status status = Status.INTERNAL_SERVER_ERROR;
  17. if(source instanceof NoSuchRecordException) {
  18. status = Status.NOT_FOUND;
  19. }
  20. return Response.status(status).build();
  21. } else {
  22. return Response.ok(Fruit.from(record.get("f").asNode())).build();
  23. }
  24. })
  25. .thenCompose(response -> session.closeAsync().thenApply(signal -> response));
  26. }

A request may look like this:

  1. curl localhost:8080/fruits/42
In case Neo4j has been setup as a cluster, the transaction mode is used to decide whether a request is routed to a leader or a follower instance. Write transactions must be handled by a leader, whereas read-only transactions can be handled by followers.

Deleting nodes

Finally, we want to get rid of fruits again and we add the DELETE method:

src/main/java/org/acme/neo4j/FruitResource.java

  1. @DELETE
  2. @Path("{id}")
  3. public CompletionStage<Response> delete(@PathParam("id") Long id) {
  4. AsyncSession session = driver.asyncSession();
  5. return session
  6. .writeTransactionAsync(tx -> tx
  7. .runAsync("MATCH (f:Fruit) WHERE id(f) = $id DELETE f", Values.parameters("id", id))
  8. .thenCompose(fn -> fn.consumeAsync()) (1)
  9. )
  10. .thenCompose(response -> session.closeAsync())
  11. .thenApply(signal -> Response.noContent().build());
  12. }
1There is no result for us, only a summary of the query executed.

A request may look like this

  1. curl -X DELETE localhost:8080/fruits/42

And that’s already the most simple CRUD application with one type of nodes. Feel free to add relationships to the model. One idea would be to model recipes that contain fruits. The Cypher manual linked in the introduction will help you with modelling your queries.

Next steps

Packaging

Packaging your application is as simple as ./mvnw clean package. It can be run with java -jar target/neo4j-quickstart-1.0-SNAPSHOT-runner.jar.

With GraalVM installed, you can also create a native executable binary: ./mvnw clean package -Dnative. Depending on your system, that will take some time.

Connection Health Check

If you are using the quarkus-smallrye-health extension, quarkus-neo4j will automatically add a readiness health check to validate the connection to Neo4j.

So when you access the /health/ready endpoint of your application you will have information about the connection validation status.

This behavior can be disabled by setting the quarkus.neo4j.health.enabled property to false in your application.properties.

Explore Cypher and the Graph

There are tons of options to model your domain within a Graph. The Neo4j docs, the sandboxes and more are a good starting point.

Going reactive

If you have access to Neo4j 4.0, you can go fully reactive.

To make life a bit easier, we will use Mutiny for this.

Mutiny

The following example uses Mutiny reactive types, if you’re not familiar with them, read the Getting Started with Reactive guide first.

Add the following dependency to your pom.xml:

  1. <dependencies>
  2. <dependency>
  3. <groupId>io.quarkus</groupId>
  4. <artifactId>quarkus-resteasy-mutiny</artifactId>
  5. </dependency>
  6. </dependencies>

The reactive fruit resources streams the name of all fruits:

src/main/java/org/acme/neo4j/ReactiveFruitResource.java

  1. package org.acme.neo4j;
  2. import javax.inject.Inject;
  3. import javax.ws.rs.Consumes;
  4. import javax.ws.rs.GET;
  5. import javax.ws.rs.Path;
  6. import javax.ws.rs.Produces;
  7. import javax.ws.rs.core.MediaType;
  8. import io.smallrye.mutiny.Multi;
  9. import io.smallrye.mutiny.Uni;
  10. import org.neo4j.driver.Driver;
  11. import org.neo4j.driver.reactive.RxResult;
  12. import org.reactivestreams.Publisher;
  13. @Path("reactivefruits")
  14. @Produces(MediaType.APPLICATION_JSON)
  15. @Consumes(MediaType.APPLICATION_JSON)
  16. public class ReactiveFruitResource {
  17. @Inject
  18. Driver driver;
  19. @GET
  20. @Produces(MediaType.SERVER_SENT_EVENTS)
  21. public Publisher<String> get() {
  22. // Create a stream from a resource we can close in a finalizer:
  23. return Multi.createFrom().resource(driver::rxSession,
  24. session -> session.readTransaction(tx -> {
  25. RxResult result = tx.run("MATCH (f:Fruit) RETURN f.name as name ORDER BY f.name");
  26. return Multi.createFrom().publisher(result.records())
  27. .map(record -> record.get("name").asString());
  28. })
  29. ).withFinalizer(session -> {
  30. return Uni.createFrom().publisher(session.close());
  31. });
  32. }
  33. }

driver.rxSession() returns a reactive session. It exposes its API based on Reactive Streams, most prominently, as org.reactivestreams.Publisher. Those can be used directly, but we found it easier and more expressive to wrap them in reactive types such as the one provided by Mutiny. Typically, in the previous code, the session is closed when the stream completes, fails or the subscriber cancels.

Configuration Reference

About the Duration format

The format for durations uses the standard java.time.Duration format. You can learn more about it in the Duration#parse() javadoc.

You can also provide duration values starting with a number. In this case, if the value consists only of a number, the converter treats the value as seconds. Otherwise, PT is implicitly prepended to the value to obtain a standard java.time.Duration format.