0% found this document useful (0 votes)
7 views

Neo 4 J

Uploaded by

priyasaanvi81
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Neo 4 J

Uploaded by

priyasaanvi81
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 62

Neo4j

Neo4j is the world's leading open source


Graph Database which is developed using
Java technology. It is highly scalable and
schema free (NoSQL).
What is a Graph Database?

• 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).
Popular Graph Databases
• Neo4j is a popular Graph Database.

Other Graph Databases are


Oracle NoSQL Database,
OrientDB,
HypherGraphDB,
GraphBase,
InfiniteGraph, and
AllegroGraph.
RDBMS Vs Graph Database

Sr.N
RDBMS Graph Database
o
1 Tables Graphs
2 Rows Nodes
3 Columns and Data Properties and its values
4 Constraints Relationships
5 Joins Traversal
Advantages of Neo4j

• Flexible data model − Neo4j provides a


flexible simple and yet powerful data model,
which can be easily changed according to the
applications and industries.

• Real-time insights − Neo4j provides results


based on real-time data.
• High availability − Neo4j is highly available for
large enterprise real-time applications with
transactional guarantees.

• Connected and semi structures data − Using


Neo4j, you can easily represent connected and
semi-structured data.
• Easy retrieval − Using Neo4j, you can not only
represent but also easily retrieve
(traverse/navigate) connected data faster
when compared to other databases.

• No joins − Using Neo4j, it does NOT require


complex joins to retrieve connected/related
data as it is very easy to retrieve its adjacent
node or relationship details without joins or
indexes.
• Cypher query language − Neo4j provides a
declarative query language to represent the
graph visually, using an ascii-art syntax.

• The commands of this language are in human


readable format and very easy to learn.
Neo4j Property Graph Data Model

• Neo4j Graph Database follows the Property


Graph Model to store and manage its data.
1. Nodes are represented using circle Nodes
and Relationships are represented Relationships
using arrow keys.
Properties

2. Relationships have directions:


Unidirectional and Bidirectional

3. Each Relationship contains "Start


Node" or "From Node" and "To
Node" or "End Node"

4. Both Nodes and Relationships


contain properties
Relationships connects nodes

With this above dig have represented each Node's Id


property within the Node's Circle.
Neo4j Graph Database has the following
building blocks −
• Nodes
• Properties
• Relationships
• Labels
• Data Browser
Node
• Node is a fundamental
unit of a Graph. It
contains properties
with key-value pairs as
shown in the following
image.

• Node Name =
"Employee" and it
contains a set of
properties as key-value
pairs.
Properties

• Property is a key-value pair to describe Graph


Nodes and Relationships.

• Key = Value

• Where Key is a String and Value may be


represented using any Neo4j Data types.
Relationships
• Relationships are
another major
building block of a
Graph Database. It
connects two
nodes.

Emp and Dept are two different nodes.


"WORKS_FOR" is a relationship between Emp
and Dept nodes.
• As it denotes, the arrow mark from Emp to Dept,
this relationship describes − Emp WORKS_FOR
Dept

• Each relationship contains one start node and


one end node.
nodes, relationships also can
contain properties as key-
value pairs.

As this relationship arrow mark represents a


relationship from "Emp" node to "Dept" node, this
relationship is known as an "Incoming
Relationship" to "Dept" Node and "Outgoing
Relationship" to "Emp" node.

"WORKS_FOR" relationship has one property as


key-value pair.
Id = 123 It represents an Id of this relationship.
Labels
• Label associates a common name to a set of nodes
or relationships.

• A node or relationship can contain one or more


labels.

• We can create new labels to existing nodes or


relationships.

• We can remove the existing labels from the


existing nodes or relationships.
• From the previous diagram, we can observe
that there are two nodes.

• Left side node has a Label: "Emp" and the


right side node has a Label: "Dept".
• Relationship between those two nodes also
has a Label: "WORKS_FOR".

• Note − Neo4j stores data in Properties of


Nodes or Relationships
Neo4j CQL - Introduction

• CQL stands for Cypher Query Language.

• Like Oracle Database has query language SQL,


Neo4j has CQL as query language.
Neo4j CQL Clauses
Sr.No Read Clauses Usage

This clause is used to search the data with


1 MATCH
a specified pattern.

This is the same as match, the only


2 OPTIONAL MATCH difference being it can use nulls in case of
missing parts of the pattern.

This clause id is used to add contents to


3 WHERE
the CQL queries.

This clause is used to find the starting


4 START
points through the legacy indexes.

This clause is used to import data from CSV


5 LOAD CSV
files.
Following are the write clauses of Neo4j Cypher Query Language −

Sr.No Write Clause Usage


This clause is used to create
1 CREATE nodes, relationships, and
properties.
This clause verifies whether
the specified pattern exists in
2 MERGE the graph. If not, it creates
the pattern.
This clause is used to update
3 SET labels on nodes, properties
on nodes and relationships.
This clause is used to delete
4 DELETE nodes and relationships or
paths etc. from the graph.
Sr.No Write Clause Usage

This clause is used to remove


5 REMOVE properties and elements from
nodes and relationships.

This class is used to update the


6 FOREACH
data within a list.

Using the clauses CREATE and


MATCH, you can get a unique
7 CREATE UNIQUE pattern by matching the existing
pattern and creating the missing
one.

Importing CSV files Using Load CSV you can import


8
with Cypher data from .csv files.
Neo4j CQL Functions
CQL
Sr.No Usage
Functions
1 String They are used to work
with String literals.
They are used to
2 Aggregation perform some
aggregation operations
on CQL Query results.
They are used to get
details of relationships
3 Relationship such as startnode,
endnode, etc.
CQL Operators
Sr.No Type Operators
1 Mathematical +, -, *, /, %, ^
2 Comparison +, <>, <, >, <=, >=
3 Boolean AND, OR, XOR, NOT
4 String +
5 List +, IN, [X], [X…..Y]
6 Regular Expression =-
STARTS WITH, ENDS
7 String matching WITH, CONSTRAINTS
Boolean Operators in Neo4j CQL
Sr.
Boolean Operators Description
No
It is a Neo4j CQL keyword to
1 AND support AND operation. It is
like SQL AND operator.
It is a Neo4j CQL keyword to
2 OR support OR operation. It is like
SQL AND operator.
It is a Neo4j CQL keyword to
3 NOT support NOT operation. It is
like SQL AND operator.

It is a Neo4j CQL keyword to


4 XOR support XOR operation. It is
like SQL AND operator.
Comparison Operators in Neo4j CQL
Sr.No Boolean Operators Description
It is a Neo4j CQL "Equal
1 =
To" operator.

2 <> It is a Neo4j CQL "Not


Equal To" operator.

It is a Neo4j CQL "Less


3 <
Than" operator.

4 > It is a Neo4j CQL "Greater


Than" operator.
It is a Neo4j CQL "Less
5 <= Than Or Equal To"
operator.
It is a Neo4j CQL "Greater
6 >= Than Or Equal To"
operator.
Neo4j CQL - Creating Nodes

• Neo4j using the CREATE clause.

• 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
Creating a Single node

• Syntax for creating a node using Cypher Query


Language.
• CREATE (node_name);

• Note − Semicolon (;) is optional.

• CREATE (sample)

• Verification
• To verify the creation of the node type, execute the
following query in the dollar prompt.
• MATCH (n) RETURN n
Creating Multiple Nodes
• CREATE (node1),(node2)

• Ex . CREATE (sample1),(sample2)

• Verification
• To verify the creation of the node, type and
execute the following query in the dollar
prompt.
• MATCH (n) RETURN n This query returns all
the nodes in the database
Creating a Node with a Label

• CREATE (node:label) - Ex. CREATE


(Dhawan:player)

• Verification - MATCH (n) RETURN n


Creating a Node with Multiple Labels

• Syntax to create a node with multiple labels.

CREATE (node:label1:label2:. . . . labeln)

Ex - CREATE (Dhawan:person:player)
Create Node with Properties

• Syntax
CREATE (node:label { key1: value, key2: value, . . . .
. . . . . })

CREATE (Dhawan:player{name: "Shikar Dhawan",


YOB: 1985, POB: "Delhi"})

MATCH (n) RETURN n


Neo4j - Merge Command

• MERGE command is a combination of CREATE


command and MATCH command.

• Merge a node with label


• Merge a node with properties
• OnCreate and OnMatch
• Merge a relationship
syntax for the MERGE command.
- MERGE (node: label {properties . . . . . . . })

- MERGE (Jadeja:player) RETURN Jadeja


Example 2

• MERGE (CT2013:Tournament{name: "ICC


Champions Trophy 2013"}) RETURN CT2013,
labels(CT2013)

• since there is no node with the given label


(Tournament). Neo4j creates and returns the
specified node
Merging a Node with Properties

• Following is the syntax to merge a node using


properties.
• MERGE (node:label {key1:value, key2:value,
key3:value . . . . . . . . })

• Ex.
• MERGE (Jadeja:player {name: "Ravindra
Jadeja", YOB: 1988, POB: "NavagamGhed"})
RETURN Jadeja
OnCreate and OnMatch

• we execute a merge query, a node is either


matched or created.

• Using on create and on match, you can set


properties for indicating whether the node is
created or matched.
• Following is the syntax
of OnCreate and OnMatch clauses.

• MERGE (node:label {properties . . . . . . . . . . .})


ON CREATE SET property.isCreated ="true" ON
MATCH SET property.isFound ="true"
• OnCreate and OnMatch clauses in Neo4j. If
the specified node already exists in the
database, then the node will be matched and
the property with key-value pair isFound =
"true" will be created in the node.

• If the specified node doesn’t exist in the


database, then the node will be created, and
within it a property with a key-value pair
isCreated ="true" will be created.
Ex
• MERGE (Jadeja:player {name: "Ravindra
Jadeja", YOB: 1988, POB: "NavagamGhed"})
ON CREATE SET Jadeja.isCreated = "true" ON
MATCH SET Jadeja.isFound = "true" RETURN
Jadeja
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 " ? "
Syntax:

• CREATE (node1)-[:RelationshipType]-
>(node2)
Ex
• CREATE (Raul:player{name: "Raul Vinci", YOB:
1973, POB: "Milan"})
• CREATE (It:Country {name: "Itly"})
• RETURN Raul, It
• -------------------------------------------------------
• CREATE (Raul)-[r:PLAYER_OF]->(It)
• RETURN Raul, It
Create a Relationship between existing Nodes

• MATCH statement is used to create


relationship between the existing Nodes.

• Syntax:
• MATCH (a:LabeofNode1), (b:LabeofNode2)
• WHERE a.name = "nameofnode1" AND b.nam
e = " nameofnode2"
• CREATE (a)-[: Relation]->(b)
• RETURN a,b
Create a relationship using MATCH statement.

• MATCH (a:player), (b:Country) WHERE a.name


= "Raul Vinci" AND b.name = "Itly"
• CREATE (a)-[r: FOOTBALLER_OF]->(b)
• RETURN a,b
Create a Relationship with Label and Properties

• Syntax:

• CREATE (node1)-
[label:Rel_Type {key1:value1, key2:value2, . . .
n}]-> (node2)
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"})
• RETURN Kohli
Create another node "Ind":
• CREATE (Ind:Country {name: "India"})

• Now create a relationship with label and


properties:

• MATCH (a:player), (b:Country) WHERE a.name


= "Virat Kohli" AND b.name = "India"
• CREATE (a)-
[r:BATSMAN_OF {Matches:5, Avg:90.75}]-
>(b)
• RETURN a,b
Creating a complete path

• A path is formed using continuous


relationship.

• Syntax:
• CREATE p = (Node1 {properties})-
[:Relationship_Type]->
• (Node2 {properties})[:Relationship_Type]-
>(Node3 {properties})
• RETURN p
First create a node3 name
"Champions_Trophy" to do further operations.

• CREATE p = (Kohli {name:"Virat Kohli"})-


[:TOPSCORRER_OF]->
• (Ind {name: "India"})-[: WINNER_OF]-
>(Node3 {CT: "Champions_Trophy"})
• RETURN p
Neo4j - Delete Clause

• Deleting All Nodes and Relationships

• Query
• MATCH (n) DETACH DELETE n
Deleting a Particular Node

• MATCH (Jadeja:player {name: "Jadeja


Sharma", YOB: 1988, POB: "Delhi"}) DETACH
DELETE Jadeja
Neo4j - Remove Clause
• The REMOVE clause is used to remove
properties and labels from graph elements
(Nodes or Relationships).

• DELETE operation is used to delete nodes and


associated relationships.
• REMOVE operation is used to remove labels
and properties.
Removing a Property

• MATCH (Jadeja:player {name: “Jadeja Singh ",


YOB: 1981, POB: "Ranchi"}) REMOVE
Jadeja.POB RETURN Jadeja
Removing a Label From a Node

• MATCH (Dhoni:player {name: "MahendraSingh


Dhoni", YOB: 1981, POB: "Ranchi"}) REMOVE
Dhoni:player RETURN Dhoni
Removing Multiple Labels

• MATCH (Ishant:player:person {name: "Ishant


Sharma", YOB: 1988, POB: "Delhi"}) REMOVE
Ishant:player:person RETURN Ishant
Neo4j - Foreach Clause

• The FOREACH clause is used to update data


within a list whether components of a path, or
result of aggregation.
• CREATE p = (Dhawan {name:"Shikar
Dhawan"})-[:TOPSCORRER_OF]->(Ind{name:
"India"})-[:WINNER_OF]->(CT2013{name:
"Champions Trophy 2013"}) RETURN p

• MATCH p = (Dhawan)-[*]->(CT2013) WHERE


Dhawan.name = "Shikar Dhawan" AND
CT2013.name = "Champions Trophy 2013"
FOREACH (n IN nodes(p)| SET n.marked =
TRUE)
Neo4j - Where Clause

• Syntax

• MATCH (label) WHERE label.country =


"property" RETURN label
• CREATE(Dhawan:player{name:"shikar
Dhawan", YOB: 1985, runs:363, country:
"India"}
• CREATE(Jonathan:player{name:"Jonathan
Trott", YOB:1981, runs:229, country:"South
Africa"}
• CREATE(Sangakkara:player{name:"Kumar
Sangakkara", YOB:1977, runs:222,
country:"Srilanka"})
• CREATE(Rohit:player{name:"Rohit Sharma",
YOB: 1987, runs:177, country:"India"})
• CREATE(Virat:player{name:"Virat Kohli", YOB:
1988, runs:176, country:"India"})
• CREATE(Ind:Country {name: "India", result:
"Winners"})
write Cypher Query which returns all the players (nodes) that belongs to the country
India using WHERE clause.

• MATCH (player) WHERE player.country =


"India" RETURN player

You might also like