Giter Club home page Giter Club logo

quarkus-neo4j's Introduction

Movies Example Application

How to use Quarkus and Neo4j OGM together. Neo4j OGM enables convenient integration and object-graph mapping (OGM) in your Quarkus application.

The example project is described in detail on the Neo4j Developer Site

The project uses Java 17 and Quarkus 2.14.2.

The Stack

These are the components of our Web Application:

  • Application Type: Quarkus

  • Persistence Access: Quarkus-Neo4j-OGM

  • Database: Neo4j-Server 4.x

  • Frontend: jquery, bootstrap, d3.js

Provision a database quickly with Neo4j Sandbox or Neo4j Aura.

Building & Running

The project is built via Maven:

mvn package

Additionally, you can execute the integration test, which fires up a local Neo4j instance via Testcontainers:

mvn test-compile failsafe:integration-test failsafe:verify

You run the Quarkus application via executable JAR:

java -jar target/quarkus-app/quarkus-run.jar

Then, the application is available under http://localhost:8080/

Alternatively, you can use Quarkus' development mode:

mvn quarkus:dev

This enables you to edit the Java code while the application keeps running.

Running Neo4j Database Locally

Per default, the example runs with a generally-available database under https://demo.neo4jlabs.com:7473

You can also run a local Neo4j database with the example project as Docker container. For this, execute the ./run-graph-db.sh script, and change the Quarkus application properties under src/main/resources/application.properties as follows:

quarkus.neo4j.uri=bolt://localhost:7687
quarkus.neo4j.authentication.username=neo4j
quarkus.neo4j.authentication.password=test

If you want to run your Neo4j database in a different way, you can also execute the :guide movies in your database console, and execute the long Cypher statement to fill your database with the movie data from the second slide of that guide.

Understanding the Examples

Once the application is running, you can examine the UI under http://localhost:8080

The different scenarios of finding movies with certain queries and advanced example can be found under the “Scenarios” dropdown.

In order to comprehend how the queries are executed, you can follow the code in the MovieResource, SearchResource, ActorsResource, and GraphResource classes.

All JAX-RS resource classes access application-scoped beans, such as Searches, which access the Neo4j database via OGM Session. The OGM session is created via SessionFactory which can be injected in the classes since we’re using the org.neo4j:neo4j-ogm-quarkus dependency in our project:

@ApplicationScoped
public class Searches {

    @Inject
    SessionFactory sessionFactory;

    public List<Movie> searchMoviesByTitle(String title) {
        Session session = sessionFactory.openSession();
        Iterable<Movie> iterable = session.query(Movie.class, "MATCH (movie:Movie) WHERE movie.title CONTAINS $title RETURN movie", Map.of("title", title));

        // [...]
    }
}

 

Another example is the mapping of arbitrary structures to DTO classes or Java records, as shown in Persons:

public List<ActorRecommendation> recommendCoActor(String name) {
    Session session = sessionFactory.openSession();
    return session.queryDto(" MATCH (actor:Person {name: $name})- [...] " +
        " [...] " +
        " RETURN cocoActors.name AS actor, count(*) AS strength ORDER BY strength DESC",
        Map.of("name", name), ActorRecommendation.class);
}

The return type of queryDto, here ActorRecommendation can either be a simple class, that doesn’t need any annotations but does need a public, no-args constructor, or a Java record as it is the case in our example.

quarkus-neo4j's People

Contributors

sdaschner 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.