In this article in a nutshell I am describing the Neo4j. This article include topics:
- a short description of database,
- in what cases it is worth to consider use of graph database,
- what are advantages comparing to relational database,
- a short description of "graph SQL" - cypher,
- a few examples of queries in cypher,
- a shortcut how to run Java project with Spring Boot and Spring Data dependences.
Neo4j is a graph database. It is transactional and ACID compliant with native graph storage and processing. It use graph SQL language called Cypher dedicated for graph databases.
Graph databases can be used everywhere where there is a need to archive a graph dependency between objects, so I could say in most cases I know.
Comparing to relational databases, native storing and processing have advantage that matching queries are executed faster than relational queries with exponential cost.
Taking a simple case of customers using some services there is a relation many to many.
In relational database it is required to have a matching table where there are ids of services and using them customers. To connect all customers with single service it is required to find service id then in matching table find customer ids and then in third one find customers.
In graph database every service Node (every object is a node - equivalent of table) stores direct Relation to Node customer. This requires using more storage but it is much faster than matching table relations. Other advantages are:
- auto extending schema model as in other NoSQL databases - adding data of node, relation or property in node or relation schema is automagically extended,
- handle "graph SQL" called Cypher.Cypher is a dedicated language for graph database. Below I have placed a few examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | -- simple select from table MATCH (c:Human) WHERE id = 1 RETURN c; -- equivalent in SQL SELECT * FROM Human WHERE id = 1; -- simple relation MATCH (p:Person) - [r:ACTED_IN] -> (m:Movie) WHERE p.name = 'Tom' RETURN p, r, m; -- equivalent in SQL SELECT * FROM Person p JOIN Relation r on p.id = r.person_id JOIN Movie m on m.id = r.movie_id WHERE p.name = 'Tom' AND r.type = 'ACTED_IN' |
In more complicated case when there is a need to create chain of relations, ex. who is above employee. Is it typical graph case? In Oracle PL/SQL there is something called "CONNECT BY" query construction but how is in other databases, truly I don't know. In MySQL I saw a recurrent procedure storing each level in temporary table, so how is in Cypher?
1 2 3 4 5 6 7 8 9 10 11 12 13 | -- this returns supervisors and their supervisor MATCH path = (n:Person)-[r:REPORTS_TO*]->(s) WHERE n.name = 'Tom' RETURN s ORDER BY length(path) -- case with subordinates by supervisor id MATCH path = (n)-[r:REPORTS_TO*]->(s:Person) WHERE id(s) = 12 RETURN n ORDER BY length(path) |
and a few other useful queries
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | -- delete all schema MATCH (n) DETACH DELETE n -- create modes and data set CREATE (:Car:Vehicle { type: "Van"}) <-[:DRIVES ] - (:Human {name: "Basia"}) -- update data MATCH (h:Human) - [d:DRIVES] -> (c:Car:Vehicle) WHERE c.type = "Van" SET h.name = "Ula", c.productionYear = "1982" RETURN h,d,c -- constraint - unique field CREATE CONSTRAINT ON (h:Human) ASSERT h.name IS UNIQUE -- delete data matching query MATCH (c:Car:Vehicle) <-[d:DRIVES ] - (h:Human) DELETE c,d , h |
Spring Boot project.
In Spring Boot with Spring Data it is required only to add spring-boot-starter-data-neo4j artefact, neo4j properties in path spring.data.neo4j.*, @EnableNeo4jRepositories in configuration and it is possible to create node entities.
In background there is added dependency to org.neo4j:neo4j-orgm-* artefacts and spring-data-neo4j and also org.neo4j.driver artefact.
I was working on:
- docker image of neo4j v.: 4.1.1 without auth.
- JDK11
- Spring Boot v.:2.2.4
Added neo4j dependences was:
- org.neo4j.driver v.: 4.0.0
- org.neo4j:neo4j-ogm-* v.:3.2.6
- spring-data-neo4j v.: 5.2.4-RELEASE
Resources:
[1] Neo4j main page
No comments:
Post a Comment