9.12.2020

Neo4J Graph Database

Neo4J

https://neo4j.com/

Neo4J is a graph database implemented in Java with ACID compliant transaction management.

Run in docker

docker run -p7474:7474 -p7687:7687 neo4j:latest

Connect to localhost to get a REPL:

http://localhost:7474/

Default username is neo4j, default password is neo4j.

Setting up a default Movies database

:play movie-graph

Concepts

Neo4J uses Nodes and Relationships to model data in graph. Neo4j uses its custom CYPHER Query language to match Nodes and Relationships.

Nodes

Nodes have labels, which distinguish Nodes and are similar to "Tables" in SQL, a Node can have multiple labels and they are the main way to categorize your data. A node can also habe properties, they are the counterpart to "columns" in SQL. However, Neo4j is schemaless, so not every node has to have all properties. In that way, its similar to MongoDB.

CREATE (person:NODE_LABEL {propery: 'value'})

Relationships

What makes Neo4J interesting, is the concept of Relationships. A Node can be connected to an unlimited amount of other Nodes by a directional Relationship. These Relationships can also have labels and properties.

CREATE (g:ProductGroup {name: 'Hochdruckreiniger'})
CREATE (p:Product {name: 'Kärcher Super 3000'})
WITH g,p
CREATE (g)-[r:INCLUDES_PRODUCT]->(p)
RETURN g,p

Bildschirmfoto_2020-12-04_um_10.49.28

Cypher query language

Neo4j does not use SQL, but an SQL-like query language called Cypher. https://neo4j.com/developer/cypher/intro-cypher/

Matching Nodes

Matching any node

MATCH (node) RETURN node

Matching all nodes with a label of NODE_LABEL

MATCH (node:NODE_LABEL) RETURN node

Matching all nodes with a property of name that equals to `Ben``

MATCH (node {name: 'Ben'}) RETURN node

Matching all nodes with the labels LABEL_ONE and LABEL_TWO

MATCH (node:LABEL_ONE:LABEL_TWO) RETURN node

You can also use WHERE clauses similar to SQL, this comes in handy if the queries get more complex.

MATCH (actor:Person)-[:ACTED_IN]-(film:Movie),
     (director:Person)-[:DIRECTED]-(film:Movie) 
WHERE actor.name='Tom Hanks'
RETURN actor.name, film.title, director.name ;

Matching relationships

Relationships are represented by dashes -- and can either be directed -[]-> or undirected -[]-.

You can also filter by labels and properties like with nodes.

MATCH (actor:Person)-[rl:ACTED_IN {roles: ['Agent Smith']}]->(movie:Movie)
RETURN rl, actor, movie

Bildschirmfoto_2020-12-04_um_11.05.29

Integrations and tools

Local development and query REPL

Neo4J has a browser-based REPL and workbench that has a good support for autocompletion, visualizing data and Import/Export of data. You can save your most used queries and manage multiple databases.

Bildschirmfoto_2020-12-04_um_11.09.03

Spring Data Neo4j

Spring Data has a integration for Neo4J that is similar to Spring Data JPA in that it works with defining Entities which are queried by Repositories.

https://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#introduction

@Node("Movie")
public class MovieEntity {

	@Id @GeneratedValue
	private Long id;

	private String title;

	@Property("tagline")
	private String description;
}
public interface MovieRepository extends CrudRepository<MovieEntity, String> {

	Optional<MovieEntity> findOneByTitle(String title);
}

Migrations in Spring Data Neo4J

https://github.com/michael-simons/neo4j-migrations

Migrations are similar to flyway and verify their validity via checksums.

Zur Übersicht

Standort Hannover

newcubator GmbH
Bödekerstraße 22
30161 Hannover

Standort Dortmund

newcubator GmbH
Westenhellweg 85-89
44137 Dortmund