DEV Community

lucasnscr
lucasnscr

Posted on

Implementing Graphs with Spring and Neo4j

Introduction
We will start our journey in the world of graphs in the following way: What are graphs, applicability, advantages and the database for graphs.

Here we have a repository with an example implementation

What are graphs?
Graphs are nothing more than a mathematical abstraction of real situations, such situations are composed of points and lines. A graph is composed as follows according to mathematical definitions, G(V,E). This structure is called V representing the vertices and E the edges.

What are vertices?
Vertex or node are called everything that can be represented by an element of a situation.

What are edges?
Everything here that represents a relationship between two vertices or nodes. The edges are characterized by having two ends in the graphical representation.

There are some expressions for this type of element, but a representation from the point of view of mathematical sets, but the best known are: G(V,E), V= {v1,v2,v3} and E= {e1,e2, e3}. Defining this agreement we reach the conclusion that, The points of a graph will be called vertices and lines as edges. Below is a representation of a graph.

Image description

When we look at the vertices and edges, we can see that they form a way. The waythat is traversed between vertices can have multiple combinations within a given scenario. We can represent the path of the image above as follows: P= v1-e2-v3-e6-v2-e2... In short, a path is a sequence of verties and edges arranged in a random way.

Graph Database
Different from relational databases, the Graph-oriented Database has other forms of data persistence like NoSQL.

The idea is to create a less generic model than the relational model, providing a simpler modeling, seeking to obtain greater performance, both for its implementation free of costly operations such as JOINs, and for the use of graph algorithms. Being much simpler to design it doesn't need a complex table design to start including the data. Imagine creating a student entity, just create a node and its properties without initially worrying about what relationships it will have. The big difference is in the representation of the relationship between the data. We have entities called vertices or nodes that are connected by edges or relationships, each of which can hold data between relationships and each relationship can have a direction.

Spring Data
The Spring Data Neo4j project applies Spring Data concepts to the development of solutions using the Neo4j graph data store. We provide repositories as a high-level abstraction for storing and querying documents as well as templates and clients for generic domain access or generic query execution. All of them are integrated with Spring’s application transactions.

Necessary Steps
For the optimal execution of microservices, you need upload Neo4j, for that access the project's root folder, in it run the command docker compose up -d. If you haven't changed docker compose the address is: http://localhost:7474/browser/

In Docker compose we disable the authentication functionality, but the default password and username are neo4j for both.

After connecting to Neo4j browser, run the script below to create Nodes and relationships.

CREATE (Interestelar:Movie {title: 'Interestelar', director: 'Christopher Nolan'})
CREATE (DarkKnight:Movie {title: 'The Dark Knight', director: 'Christopher Nolan'})
CREATE (Tenet:Movie {title: 'Tenet', director: 'Christopher Nolan'})
CREATE (Lucas:User {name: 'Lucas Nascimento', age: 27})
CREATE (Brenda:User {name: 'Brenda', age: 23})
CREATE (Mel:User {name: 'Mel', age: 30})

CREATE
(Interestelar)-[:RATED {rating: 10}]->(Lucas),
(Interestelar)-[:RATED {rating: 8}]->(Brenda),
(Interestelar)-[:RATED {rating: 7}]->(Mel),
(DarkKnight)-[:RATED {rating: 10}]->(Lucas),
(DarkKnight)-[:RATED {rating: 8}]->(Brenda),
(DarkKnight)-[:RATED {rating: 10}]->(Mel),
(Tenet)-[:RATED {rating: 6}]->(Lucas),
(Tenet)-[:RATED {rating: 7}]->(Brenda),
(Tenet)-[:RATED {rating: 0}]->(Mel)

;
Enter fullscreen mode Exit fullscreen mode

Image description

Here we have a representation of our graph database.

Technologies
The following technologies were used to carry out the project:

Java 11
Maven
SpringBoot
Spring Web
Spring Data Neo4j
Neo4j
Docker

Top comments (0)