Unit-5
Graph Databases
What is a Graph Database?
A graph database (GDB) is a database that uses graph
structures for storing data. It uses nodes, edges, and properties
instead of tables or documents to represent and store data. The
edges represent relationships between the nodes. This helps in
retrieving data more easily and, in many cases, with one
operation. Graph databases are commonly referred to as
NoSQL. Ex: Neo4j, Amazon Neptune, ArangoDB etc.
Representation:
The graph database is based on graph theory. The data is stored in the
nodes of the graph and the relationship between the data are
represented by the edges between the nodes.
Introduction to graph databases:
A database is an integral part of any application.
Not only does a database store information, it also impacts
the overall performance of software. So selecting a database
suitable for your project is crucial. Lots of applications rely
on a relational database such as MySQL or PostgreSQL.
Despite the many advantages of relational databases,
however, they aren’t efficient at coping with ever-growing
amounts of connected data.
To handle a growing volume of connected data, you can go for
Neo4j, a non-relational graph database that’s optimized for
managing relationships. The Neo4j database can help you
build high-performance and scalable applications that use
large volumes of connected data.
Many software developers know little about the capabilities of
graph databases and Neo4j in particular. In this article, we
explain the essence of this graph database, show when you
can use it, and give examples of how to implement Neo4j in
your project.
Graph databases are the best solution for handling
connected data
If you’ve worked only with relational databases in your career as a
developer, you might be asking whether there’s any point in going
for a non-relational model. Everything seems clear and familiar in
the relational databases you’re used to, doesn’t it? Yet relational
databases have several substantial drawbacks:
•Volume limitations − Relational data stores aren’t optimized to
handle large amounts of data.
•Velocity − The performance of relational stores suffers when
they need to deal with huge numbers of read/write operations.
•Lack of relationships − Relational data stores can’t describe
relationships other than standard one-to-one, one-to-many, and many-to-
many.
•Variety − Relational databases lack flexibility when dealing with types of
data that can’t be described using the database schema. They also
aren’t efficient when it comes to handling big binary and semi-structured
data (JSON and XML).
•Scalability − Horizontal scaling is inefficient for relational data stores.
To overcome these limitations, a number of different non-relational
databases have been created. Most of them lack relationships,
however, because they often associate pieces of data with each
other through references (just like foreign keys in the relational
model). References make it difficult to query data (particularly,
connected data) as they struggle to describe relationships between
entities.
Unlike all other data storage and management technologies, graph
databases are focused on relationships and store already connected
data. That’s why graph databases prove the most efficient for
handling large amounts of connected data.
Neo4j as a graph database
Graph databases are based on graph theory from mathematics.
Graphs are structures that contain vertices (which represent
entities, such as people or things) and edges (which represent
connections between vertices). Edges can have numerical values
called weight.
This structure enables developers to model any scenario defined
by relationships. For instance, a graph database allows you to
model a social network where nodes are users and relationships
are connections between them. Or you can build a road network
where vertices are cities, towns, or villages, while edges are roads
that connect them with weights indicating distances.
Neo4j provides its own implementation of graph theory concepts.
Let’s take an in-depth look at the Labeled Property Graph Model
in the Neo4j database. It has the following components:
•Nodes (equivalent to vertices in graph theory). These are the
main data elements that are interconnected through
relationships. A node can have one or more labels (that describe
its role) and properties (i.e. attributes).
•Relationships (equivalent to edges in graph theory). A
relationship connects two nodes that, in turn, can have multiple
relationships. Relationships can have one or more properties.
•Labels. These are used to group nodes, and each node can be assigned
multiple labels. Labels are indexed to speed up finding nodes in a graph.
•Properties. These are attributes of both nodes and relationships. Neo4j allows
for storing data as key-value pairs, which means properties can have any value
(string, number, or boolean).
A graph model is intuitive and easy for people to interpret. After
all, the human brain doesn’t think in terms of tables and rows but
in terms of abstract objects and connections. In fact, anything
you can draw on a blackboard can be displayed with a graph.
How Neo4j compares to relational and other NoSQL
databases
Relational
property Neo4j NoSQL databases
databases
Fixed, predefined Connected data not
Graph storage
Data storage tables with rows and supported at the
structure
columns database level
Database model Not suitable for
Data modeling Flexible data model must be developed enterprise
from a logical model architectures
Great performance Data processing
Relationships must
Query regardless of speed slows with
be created at the
performance number and depth growing number of
application level
of connections joins
SQL: complexity Different languages
Cypher: native
grows as the are used but none is
Query language graph query
number of joins tailored to express
language
increases relationships
Retational NoSQL
property Neo4j
Databases Databases
BASE
transactions
Transaction Retains ACID ACID transaction
prove unreliable
support transactions support
for data
relationships
Inherently
Scales through Scalable, but
Processing at scalable for
replication, but data integrity
scale pattern-based
it’s costly isn’t trustworthy
queries
Advantages of Neo4j databases
Designed specifically to deal with huge amounts of
connected data, the Neo4j database provides the
following advantages:
•Performance : In relational databases, performance
suffers as the number and depth of relationships
increases. In graph databases like Neo4j, performance
remains high even if the amount of data grows
significantly.
•Flexibility : Neo4j is flexible, as the structure and
schema of a graph model can be easily adjusted to the
changes in an application. Also, you can easily upgrade
the data structure without damaging existing
functionality.
•Agility : The structure of a Neo4j database is easy-to-
upgrade, so the data store can evolve along with your
application.
Neo4j CQL Data Types:
The Neo4j CQL data types are similar to Java language data types. They are
used to define properties of a node or a relationship.
A list of Neo4j CQL data types:
CQL Data
Index Usage
Type
1. Boolean It is used to represent Boolean literals: True, False.
2. byte It is used to represent 8-bit integers.
3. short It is used to represent 16-bit integers.
4. int It is used to represent 32-bit integers.
5. long It is used to represent 64-bit integers.
Float is used to represent 32-bit floating-point
6. float
numbers.
Double is used to represent 64-bit floating-point
7. double
numbers.
8. char Char is used to represent 16-bit characters.
Neo4j CQL Operators
Neo4j CQL Operators can be categorized in following types:
•Mathematical Operators: i.e. +, -, *, /, %, ^
•Comparison Operators: i.e. +, <>, <, >, <=, >=
•Boolean Operators: i.e. AND, OR, XOR, NOT
•String Operators: i.e. +
•List Operators: i.e. +, IN, [X], [X?..Y]
•Regular Expression: i.e. =-
•String matching: i.e. STARTS WITH, ENDS WITH, CONSTRAINTS
Boolean Operators
Following is a list of Boolean operators which are used in Neo4j CQL
WHERE clause to support multiple conditions:
Index Boolean operators Description
It is a neo4j CQL keyword to support
1. AND AND operation. It is like SQL AND
operator.
It is a Neo4j CQL keyword to support OR
2. OR
operation. It is like SQL AND operator.
It is a Neo4j CQL keyword to support
3. NOT NOT operation. It is like SQL AND
operator.
It is a Neo4j CQL keyword to support
4. XOR XOR operation. It is like SQL AND
Comparison Operators:
A list of Neo4j CQL Comparison Operators used with WHERE clause:
Index Boolean operators Description
1. = It is a Neo4j CQL "equal to" operator.
2. <> It is a Neo4j CQL "not equal to" operator.
3. < It is a Neo4j CQL "less than" operator.
4. > It is a Neo4j CQL "greater than" operator.
It is a Neo4j CQL "less than or equal to"
5. <=
operator.
It is a Neo4j CQL"greater than or equal to"
6. >=
operator.
Neo4j Create Nodes
Node is a data or record in a graph database. In Neo4j, the
CREATE statement is used to create a node. You can create the
following things by using CREATE statement:
•Create a single node
•Create multiple nodes
•Create a node with a label
•Create a node with multiple labels
•Create a node with properties
•Returning the created node
Create a Single Node
To create a single node in Neo4j, specify the name of the node
along with CREATE statement.
Syntax:
CREATE (node_name);
Note: You can add or ignore semicolon (;). It is optional.
Example1:
CREATE (single);
Input
Output
Verification
Execute the following code to verify the creation of the node
type:
MATCH (n) RETURN n
Create Multiple Nodes:
To create multiple nodes in Neo4j, use CREATE statement with the name
of nodes separated by a comma.
Syntax:
CREATE (node1),(node2), (node1),???..
Example2:
Let's create 2 nodes: primary_node
and secondary_node.
CREATE (primary_node),( secondary_node);
Verification:
MATCH (n) RETURN n
Note: It is displaying 3
nodes because we have
created a node already
in example1.
Create a node with a label
In Neo4j, a label is used to classify the nodes using labels. CREATE
statement is used to create a label for a node in Neo4j.
Syntax:
CREATE (node:label)
Example3:
CREATE (Kalam:scientist)
Verification:
MATCH (n) RETURN n
Output:
Create a Node with Multiple Labels:
To create multiple labels with a single node, you have to specify the
labels for the node by separating them with a colon " : ".
Syntax:
CREATE (node:label1:label2:. . . . labeln)
Example:
Create a node "Kalam" with label
"person", "president", and "scientist".
CREATE (Kalam:person:president:scientist)
Verification:
Create Node with Properties
In Neo4j, properties are the key-value pairs which are used by nodes to
store data. CREATE statement is used to create node with properties,
you just have to specify these properties separated by commas within
the curly braces "{ }".
Syntax:
CREATE (node:label { key1: value, key2: value, . . . . . . . . . })
Example:
Let's create a node "Ajeet", having the following properties:
CREATE (Ajeet:Developer{name: "Ajeet Kumar", YOB: 1989, POB: "Mau"
})
Verification
MATCH (n) RETURN n
Returning the created node
MATCH (n) RETURN n command is used to view the created nodes.
This query returns all the existing nodes in the database.
But if you want to return the newly created node use the RETURN
command with CREATE command:
Syntax:
CREATE (Node:Label{properties. . . . }) RETURN Node
Example:
Create a node "Sonoo" with following properties and return that
node.
CREATE (Sonoo:trainer{name: "Sonoo Jaiswal", YOB: 1987, POB: "Faizab
ad"}) RETURN Sonoo
Create Relationship:
CREATE statement is used to create relationship between
nodes. These relationships define direction, type and form
patterns of the data.
It defines mainly three things:
•Creating Relationships
•Creating Relationships between existing nodes
•Creating relationships with label and properties
Creating Relationship
While creating a relationship, relationship should be specified within
square braces "[ ]", depending on the direction of the relationship it is
placed between hyphen " - " and arrow " ? " as shown in the following
syntax.
Syntax:
CREATE (node1)-[:RelationshipType]->(node2)
Example:
Let's create two nodes "Raul" and "It" first and then specify the
relationship between them.
CREATE (Raul:player{name: "Raul Vinci", YOB: 1973, POB: "Milan"})
CREATE (It:Country {name: "Itly"})
RETURN Raul, It
Now create a relationship "PLAYER_OF between these two nodes as ?
CREATE (Raul)-[r:PLAYER_OF]->(It)
RETURN Raul, It
Output:
You can also see it in tabular or text form:
You can also download (export) the graph in which format you want to save
it. Click on the download button, see the example:
Create a Relationship between existing Nodes:
MATCH statement is used to create relationship between the existing
Nodes.
Syntax:
MATCH (a:LabeofNode1), (b:LabeofNode2)
WHERE [Link] = "nameofnode1" AND [Link] = " nameofnode2"
CREATE (a)-[: Relation]->(b)
RETURN a,b
Example
Create a relationship using MATCH statement.
MATCH (a:player), (b:Country) WHERE [Link] = "Raul Vinci" AND [Link] = "Itl
y"
CREATE (a)-[r: FOOTBALLER_OF]->(b)
RETURN a,b
Create a Relationship with Label and Properties
CREATE statement is used to create a relationship with label and
properties.
Syntax:
CREATE (node1)-[label:Rel_Type {key1:value1, key2:value2, . . . n}]-
> (node2)
Example:
Let's take an example to create a relationship for a node with label and
properties using the CREATE statement.
First create a node "Kohli", having multiple labels
CREATE (Kohli:person:player)
Then create some properties with the same node:
CREATE (Kohli:player
{name: "Virat Kohli", YOB: 1988, POB: "Delhi"}
Create another node "Ind":
CREATE (Ind:Country {name: "India"})
Now create a relationship with
label and properties:
MATCH (a:player), (b:Country) WH
ERE [Link] = "Virat Kohli" AND b.
name = "India"
CREATE (a)-
[r:BATSMAN_OF {Matches:5, Avg:9
0.75}]->(b)
RETURN a,b
Creating a complete path
In Neo4j, CREATE statement is used to create a path. A path is formed
using continuous relationship.
Syntax:
CREATE p = (Node1 {properties})-[:Relationship_Type]->
(Node2 {properties})[:Relationship_Type]->(Node3 {properties})
RETURN p
Example:
First create a node3 name
"Champions_Trophy" to do
further operations.
Now execute the following code:
CREATE p = (Kohli {name:"Virat Kohli"})-[:TOPSCORRER_OF]->
(Ind {name: "India"})-[: WINNER_OF]-
>(Node3 {CT: "Champions_Trophy"})
RETURN p
Neo4j Create Index
In Neo4j, index is a data structure which is used to improve the
speed of data retrieval operations in a database.
An index can be created over a property on any node that has
been given a label. Once an index is created, Neo4j will
manage it and keep it up to date whenever the database is
changed.
CREATE INDEX ON statement is used to create an index.
Example:
CREATE INDEX ON :player(Goals)
Output:
Note: Every time you create an index Neo4j will create the index
in the background. If your database is large, this may take some
time. Only when Neo4j has finished creating the index, will it be
brought online, and it can be used in queries.
Verification
The created schema and constraints become the part of database
schema. You can check all indexes and constraints by using
the :schema command.
:schema
Output:
Neo4j Create Constraints
In Neo4j, a constraint is used to place restrictions over the data
that can be entered against a node or a relationship.
There are two types of constraints in Neo4j:
Uniqueness Constraint: It specifies that the property must
contain a unique value. (For example: no two nodes with an player
label can share a value for the Goals property.)
Property Existence Constraint: It makes ensure that a property
exists for all nodes with a specific label or for all relationships with
a specific type.
Create a Uniqueness Constraint
CREATE CONSTRAINT ON statement is used to create a uniqueness
constraint in Neo4j.
CREATE CONSTRAINT ON (Kalam:president) ASSERT [Link] IS UNIQU
E
Output:
Verification
Use the :schema command to check the
constraints.
:schema
Output:
Property Existence Constraint
Property existence constraint is used to make ensure
that all nodes with a certain label have a certain
property.
Note: exists property constraint are only
available in the Neo4j Enterprise Edition.
Select Data with MATCH
MATCH statement is used to fetch the data which matches a
given criteria. MATCH statement is also used to perform some
operation and return the all nodes.
Fetch a single Node
Create a node:
CREATE (a:Actors { Name : "Sonu Nigam" })
Fetch single record:
MATCH (a:Actors)
WHERE [Link] = "Sonu Nigam"
RETURN a
Output:
Fetch all Nodes
If you want to retrieve all nodes from a database, just avoid the
filters.
Use the following code to retrieve all nodes from a database:
MATCH (n) RETURN n
Output:
You can see it also in tabular form:
Tabular form:
Import data from CSV
CSV stands for Comma Separated Value.
LOAD CSV statement is used to import data from a
CSV file into a Neo4j database. It facilitates you to load
CSV files into Neo4j form other database i.e. relational
database.
You can load a CSV file from a local or remote URL.
Use a [Link] URL, to access a local stored file (on the
database server), Otherwise, you can import remote
files using any of the HTTPS, HTTP, and FTP protocols.
Load a CSV file
Let's load a CSV file named "[Link]" using the HTTP protocol. It
contains a list of 10 names, so it will create 10 nodes.
Example
Import the CSV file from local
storage:
LOAD CSV FROM '[Link]
[Link]/[Link]’
AS line
CREATE (:Name { NameId: line[0],
Name: line[1]})
You can use the following query to
see the newly created nodes:
MATCH (n:Name) RETURN n
Output:
Neo4j Drop an Index
In Neo4j, "DROP INDEX ON" statement is used to drop an index
from database. It will permanently remove the index from the
database.
Example:
DROP INDEX ON :player(Goals)
As you see in the above Image, the index is removed.
Verification
:schema command is used to verify that the applicable index has
been removed
from the schema.
You can see that index is
no longer in the schema.
Neo4j Drop a Constraint
DROP CONSTRAINT statement is used to drop or remove a constraint
from the database as well as its associated index.
Example:
Use the following statement to
drop the previously created
constraint and its associated
index,
DROP CONSTRAINT ON
(Kalam:president) ASSERT
[Link] IS UNIQUE
The constraint is removed successfully.
Verification
:schema statement is used to verify that the applicable constraint
and its associated index has been removed from the schema.
Output:
Neo4j Delete a Node
In Neo4j, DELETE statement is always used with MATCH statement to
delete whatever data is matched. The DELETE command is used in the
same place we used the RETURN clause in our previous examples.
Example
MATCH (Kohli:person
{
Name: "Virat Kohli“
}
) DELETE Kohli
Delete Multiple Nodes
You can delete multiple nodes by using MATCH and DELETE commands
in a single statement. You just have to put the different nodes separated
by a column.
MATCH (a:Student {Name: "Chris Grey"}), (b:Employee {Name: "Mark
Twin"}) DELETE a,b
Delete All Nodes
To delete all nodes from the database, don?t use any filter criteria.
Use the DETACH DELETE statement:
Example:
MATCH (Kohli:player{name: "Virat Kohli"}) DETACH DELETE Kohli
Neo4j Delete a Relationship
Deleting relationship is as simple as deleting nodes. Use the MATCH
statement to match the relationships you want to delete.
You can delete one or many relationships or all relationships by using
one statement.
Example:
Delete the relationship named "PLAYER_OF" from the database:
MATCH (Raul)-[r:PLAYER_OF]->(It)
DELETE r
Output:
Delete All Relationships related to a Node
There is a method to delete a node and all relationships related to that
node.
Use the DETACH DELETE statement:
Example:
MATCH (Kohli:player{name: "Virat Kohli"}) DETACH DELETE Kohli
Output:
Delete Whole Database
To delete all database use the DETACH
DELETE statement without using a filter.
DETACH DELETE;
Network Mapping
Infrastructure mapping and inventory is a natural fit for
representation as a graph. In particular when mapping relationships
between connected physical/virtual hardware and the services that
they support. An enterprise will use CMDBs (configuration
management databases) and/or service catalogs to keep inventory
of their systems. They are used to keep track of components, their
purpose, software versions and the interdependencies between
them.
Dependency Management: Identify single points of failure and
simulate the impact of their failure on services, to identify cascading
failures before they happen.
Graph Database Use Cases
Fraud Detection
Business events and customer data, such as new accounts, loan
applications and credit card transactions can be modelled in a
graph in order to detect fraud. By looking for suspicious patterns
of customer activity metadata and cross-referencing with
previously identified fraud, we can flag up potential fraud that
may be ongoing.
360-Degree Customer View
A compelling use of graph databases is in integrating a business’s
data from across its estate of data silos. This generates a
considered view of the overall landscape, and can be used to
improve insight, for example by enabling a “360-degree customer
view.”
Bottleneck Identification: Find weak links in
network routing that could cause bottlenecks at
times of high network utilisation.
Latency Evaluation: Estimate latency across
paths in the network, and the impact on services
accessed from various geographic regions.
When not to use graph databases
:•Simple Data Models: If your data is structured and doesn't involve
complex relationships, a relational database (RDBMS) is a better fit for
straightforward queries.
•High Write Throughput: Graph databases may not perform well with
high-frequency write-heavy operations. Alternatives like NoSQL or time-
series databases are better suited for such use cases.
•Limited Relationships: If your data has minimal interconnections, using
a graph database might add unnecessary complexity.
•Scalability Issues: Graph databases can struggle with scaling
horizontally, making them less suitable for large, distributed systems
compared to NoSQL databases.
•Lack of Advanced Tooling: The ecosystem around graph databases is
less mature compared to relational databases, which offer more robust
reporting, analytics, and integration tools.