Neo4j Simple Introduction :

YOUSSEF ALOUANI
6 min readJul 21, 2022

when the cost of joins are high , when the relation between data is so important then you should think about this great DB Neo4J .

Graph :

A graph is a pictorial representation of a set of objects where some pairs of objects are connected by links. It is composed of two elements — nodes (vertices) and relationships (edges).

Graph Database :

Graph database is a database used to model the data in the form of graph. In here, the nodes of a graph depict the entities while the relationships depict the association of these nodes.Nowadays, most of the data exists in the form of the relationship between different objects and more often, the relationship between the data is more valuable than the data itself. Relational databases store highly structured data which have several records storing the same type of data so they can be used to store structured data and, they do not store the relationships between the data. Unlike other databases, graph databases store relationships and connections as first-class entities. The data model for graph databases is simpler compared to other databases and, they can be used with OLTP systems. They provide features like transactional integrity and operational availability.

Keywords :

graphs is a regroupement of nodes .

the keywords between SQLDBs and graphDBs

each element “document” in mongodb or “row” in mysql in a node in neo4j .

Graph Data Model :

check this : http://graphdatamodeling.com/

neo4j representation

Neo4j Graph Database follows the Property Graph Model to store and manage its data.

Following are the key features of Property Graph Model :

The model represents data in Nodes, Relationships and Properties

Properties are key-value pairs : that helps us to define and uniquely identify the relationships between nodes like for example a Propertie can be an Id

Nodes are represented using circle and Relationships are represented using arrow keys

Relationships have directions: Unidirectional and Bidirectional !relationships should be directional

Each Relationship contains “Start Node” or “From Node” and “To Node” or “End Node”

Both Nodes and Relationships contain properties

Relationships connects nodes

Modelisation :

Data model :

The data model describes the labels, relationships, and properties for the graph. It does not have specific data that will be created in the graph.

  • A label is a single identifier that begins with a capital letter and can be CamelCase.Examples: Person, Company, GitHubRepo
  • A relationship type is a single identifier that is in all capital letters with the underscore character. Examples: FOLLOWS, MARRIED_TO . relationships fields

Id

Type

Map (of properties)

Id of the start node

Id of the end node

Path, an alternating sequence of nodes and relationships

A property key for a node or a relationship is a single identifier that begins with a lower-case letter and can be camelCase. Examples: deptId, firstName .

Instance model

An important part of the graph data modeling process is to test the model against the use cases. To do this, you need to have a set of sample data that you can use to see if the use cases can be answered with the model.

Rules :

labels should be always a noun so they can be general between nodes or relationships and to be more specific .

properties should be used to identify nodes an relations also to store elements data this data is what we are going to reterieve .

The property types:

Number, an abstract type, which has the subtypes Integer and Float

String

Boolean

The spatial type Point

Temporal types: Date, Time, LocalTime, DateTime, LocalDateTime and Duration

Cypher Query Language Pratice :

MERGE (:Movie {title: ‘Apollo 13’, tmdbId: 568, released: ‘1995–06–30’, imdbRating: 7.6, genres: [‘Drama’, ‘Adventure’, ‘IMAX’]}) : create a node label Movie and with the following properties as title , geners …

MATCH (apollo:Movie {title: ‘Apollo 13’}) : create an alias called apollo of the node with title is ‘Apollo 13’ .

MERGE (tom)-[:ACTED_IN {role: ‘Jim Lovell’}]->(apollo) : create a relation with the name of ACTED_IN and the properities of role is ‘Jim Lovell’ between tom and apollo .

Instalation guide :

#!/bin/bash

# file of instalation of neo4j in centos or rockylinux

rpm — import https://debian.neo4j.com/neotechnology.gpg.key

cat <<EOF > /etc/yum.repos.d/neo4j.repo

[neo4j]

name=Neo4j RPM Repository

baseurl=https://yum.neo4j.com/stable

enabled=1

gpgcheck=1

EOF

# install the compatiple java version in the case of neo4j version 4.4 its requires java 11 runtime check this link to install https://phoenixnap.com/kb/install-java-on-centos

# install the neo4j adapter

yum install https://dist.neo4j.org/neo4j-java11-adapter.noarch.rpm

# install the neo4j Kit

yum install neo4j-4.4.8

# enable neo4j as executable package

systemctl enable neo4j

# run neo4j

systemctl restart neo4j

# check the the status

systemctl status neo4j

# chec version using

rpm -qa | grep neo

# you can use a docker container instead of the instalatin

# nginx config /neo4j-ui

# username and password neo4j : password Connected to Neo4j using Bolt protocol version 4.4 at neo4j://localhost:7687 as user neo4j.

# neo4j visualisation part run this cmd in yyour terminal :

ssh -L 7474:localhost:7474 -L 7687:localhost:7687 user@ip-address -N : navigate in port http://localhost:7474/browser/

# for more details check https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-neo4j-on-ubuntu-20-04

Clients For NodeJS :

neo4j-driver for nodejs .

How to use Neo4J with NodeJS :

# neo4j.jsconst neo4j = require('neo4j-driver')
const config = require('./config') const driver = neo4j.driver(config.neo4j.url, neo4j.auth.basic(config.neo4j.username, config.neo4j.password)) module.exports = {
read: (cypher, params = {}, database = config.neo4j.database) => { const session = driver.session({ defaultAccessMode: neo4j.session.READ, database, }) return session.run(cypher, params) .then(res => { session.close() return res }) .catch(e => { session.close() throw e }) }, write: (cypher, params = {}, database = config.neo4j.database) => {
const session = driver.session({ defaultAccessMode: neo4j.session.WRITE, database, }) return session.run(cypher, params) .then(res => { session.close() return res }) .catch(e => { session.close() throw e }) },}
# create-user.cypherCREATE (u:User { id: randomUUID(), username: $username, password: $password, email: $email, bio: $bio, image: $image})RETURN u
# getUserByemail.cypher
MATCH (u:User {email: $email}) RETURN u
#getUserById.cypher
MATCH (u:User {id: $id}) RETURN u# index.js to exports cypher files and execute them
const fs = require('fs') module.exports = file => { const buffer = fs.readFileSync(`${__dirname}/${file}.cypher`) return buffer.toString()}

Neo4J Use Cases :

  • Fraud Detection & Analytics .
  • Network and Database Infrastructure Monitoring for IT Operations .
  • Recommendation Engine & Product Recommendation System .
  • Social Media and Social Network Graphs .
  • Supply Chain Management

thanks for reading .

--

--

YOUSSEF ALOUANI

junior software engineer , the performance if it was a person , intrested in microservices architecture and System Design . I believe in sharing Knowledge