December 01, 2019

Neo4j Tutorial, Interview Questions & Answers

What is Neo4j?
Neo4j is an open source NOSQL graph database, implemented in Java. It saves data structured in graphs rather than in tables.

What is the difference between Neo4j graph database and MySQL?
  • Neo4j consists of vertices and edges. Each vertex or node represent a key value or attribute. In relational databases, attributes are appended in plain table format
  • It is possible to store dynamic content like images, videos, audio in Neo4j, where as relational databases, such as MySQL, it’s difficult to store them.
  • Neo4j has the capability for deep search into the database without affecting the performance along with efficient timing. Relational Database takes longer time for database search and also inconvenient compared to neo4j
  • We can relate any two objects in neo4j by the mean of making relationship between any two nodes. Relational Database lacks relationship and difficult to use them for connected graphs and data
How files are stored in Neo4j?
Neo4j stores graph data in a number of different store files, and each store file consists of the data for a specific part of the graph for example relationships, nodes, properties etc. for example Neostore.nodestore.db, neostore.propertystore.db and so on.

How Neo4j stores primitive array?
Neo4j stores primitive array in a compressed way in order to save the space on disk, to do that it uses a “bit saving” algorithm.

Which are the several popular Graph Databases?
Neo4J is a very popular Graph Database. Other Graph Databases are Oracle NoSQL Database, OrientDB, HypherGraphDB, GraphBase, InfiniteGraph, AllegroGraph etc.

What is CQL? How to execute CQL commands in Neo4J?
CQL stands for Cypher Query Language. The "$" prompt is used to execute all CQL commands in Neo4j.

Read Clauses
  • MATCH : This clause is used to search the data with a specified pattern.
  • OPTIONAL MATCH : This is the same as match, the only difference being it can use nulls in case of missing parts of the pattern.
  • WHERE : This clause id is used to add contents to the CQL queries.
  • START : This clause is used to find the starting points through the legacy indexes.
  • LOAD CSV : This clause is used to import data from CSV files.
Write Clause
  • CREATE : This clause is used to create nodes, relationships, and properties.
  • MERGE : This clause verifies whether the specified pattern exists in the graph. If not, it creates the pattern.
  • SET : This clause is used to update labels on nodes, properties on nodes and relationships.
  • DELETE : This clause is used to delete nodes and relationships or paths etc. from the graph.
  • REMOVE : This clause is used to remove properties and elements from nodes and relationships.
  • FOREACH : This class is used to update the data within a list.
  • CREATE UNIQUE : Using the clauses CREATE and MATCH, you can get a unique pattern by matching the existing pattern and creating the missing one.
  • Importing CSV files with Cypher : Using Load CSV you can import data from .csv files.
General Clauses
  • RETURN : This clause is used to define what to include in the query result set.
  • ORDER BY : This clause is used to arrange the output of a query in order. It is used along with the clauses RETURN or WITH.
  • LIMIT : This clause is used to limit the rows in the result to a specific value.
  • SKIP : This clause is used to define from which row to start including the rows in the output.
  • WITH : This clause is used to chain the query parts together.
  • UNWIND : This clause is used to expand a list into a sequence of rows.
  • UNION : This clause is used to combine the result of multiple queries.
  • CALL : This clause is used to invoke a procedure deployed in the database.
CQL Functions
  • String : They are used to work with String literals.
  • Aggregation : They are used to perform some aggregation operations on CQL Query results.
  • Relationship : They are used to get details of relationships such as startnode, endnode, etc.
Neo4j CQL Data Types
  • Boolean : It is used to represent Boolean literals: true, false.
  • byte : It is used to represent 8-bit integers.
  • short : It is used to represent 16-bit integers.
  • int : It is used to represent 32-bit integers.
  • long : It is used to represent 64-bit integers.
  • float : It is used to represent 32-bit floating-point numbers.
  • double : It is used to represent 64-bit floating-point numbers.
  • char : It is used to represent 16-bit characters.
  • String : It is used to represent Strings.
CQL Operators
  • Mathematical : +, -, *, /, %, ^
  • Comparison : +, <>, <, >, <=, >=
  • Boolean : AND, OR, XOR, NOT
  • String : +
  • List : +, IN, [X], [X…..Y]
  • Regular Expression : =-
  • String matching : STARTS WITH, ENDS WITH, CONSTRAINTS
Explain the role of building blocks like Nodes, Relationships, Properties and Labels in Neo4j?
  • Nodes: They are entities
  • Relationship: It connects entities and structure domain
  • Properties: It consists of meta-data and attributes
  • Labels: It group nodes by role

Creating the nodes in Neo4j

Creating a Single node: We can create a single node by executing below query. Semicolon is optional.
CREATE (node_name); 

Creating Multiple Nodes: We can create multiple nodes at the same time by using below query:
CREATE (node1),(node2),(node3)

Creating a Node with a Label: In Neo4j a label in Neo4j is used to group the nodes. We can create a node with label by using below command:
CREATE (node:label) 

Creating a Node with Multiple Labels: We can also create multiple labels for a single node by using below command:
CREATE (node:label1:label2:...labeln)

Create Node with Properties: Properties are the key-value pairs using which a node stores data. We can create a node with properties using the CREATE clause by specifying the properties separated by commas within the {}.
CREATE (node:label { key1: value, key2: value,..})
e.g CREATE(Employee:Developer{name:"himanshu",surname:"shukla",id:1})

Returning the Created Node: We can use the RETURN clause with CREATE to view the newly created node.
CREATE (Node:Label{properties. . . . }) RETURN Node
e.g CREATE(Employee:Developer{name:"Andrew",surname:"Hudson",id:1}) RETURN Employee;

Relationships in Neo4j

Creating the Relationships : To create a relationship using the CREATE clause we need to specify relationship within the square braces '[ ]' depending on the direction of the relationship it is placed between hyphen ' - ' and arrow '→' .
CREATE (node1)-[:RelationshipType]->(node2) 
e.g
CREATE(Employee:Developer{name:"himanshu shukla",id:1})
CREATE (Ind:Country {name: "India"})
CREATE (Employee)-[r:STAYS_IN]->(Ind) 

Creating a Relationship Between the Existing Nodes: We can also create a relationship between the existing nodes using the MATCH clause. e.g:
MATCH (a:LabeofNode1), (b:LabeofNode2) 
WHERE a.name = "nameofnode1" AND b.name = " nameofnode2" 
CREATE (a)-[: Relation]->(b) 
RETURN a,b

Lets say we created two nodes:
CREATE(Employee:Developer{name:"himanshu shukla",id:1})
CREATE (Ind:Country {name: "India"})

Now we want to add a relationship between the two.
MATCH (a:Developer), (b:Country) WHERE a.name = "himanshu shukla" AND b.name = "India" 
CREATE (a)-[r: LIVES_IN]->(b) 
RETURN a,b 


Creating a Relationship with Label and Properties: We can create a relationship with label and properties using the CREATE clause.
CREATE (node1)-[label:Rel_Type {key1:value1, key2:value2, . . . n}]-> (node2)

Lets say we created two nodes:
CREATE(Employee:Developer{name:"himanshu shukla",id:1})
CREATE (Ind:Country {name: "India"})

Now we want to add a relationship between the two.
MATCH (a:Developer), (b:Country) WHERE a.name = "himanshu shukla" AND b.name = "India"
CREATE (a)-[r: LIVES_IN {Staying_Since:1990,Region:"North"}]->(b)
RETURN a,b

Merge Command in Neo4j

Its a combination of CREATE command and MATCH command. It searches for a given pattern in the graph. If it exists, then it returns the results else it creates a new node/relationship and returns the results.
MERGE (node: label {properties . . . . . . . }) 

Lets create two nodes in the database with labels Employee and Ind. Create a relationship of type 'LIVES_IN'
CREATE(Employee:Developer{name:"himanshu shukla",id:1});
CREATE (Ind:Country {name: "India"});
MATCH (a:Developer), (b:Country) WHERE a.name = "himanshu shukla" AND b.name = "India" 
CREATE (a)-[r: LIVES_IN {Staying_Since:1990,Region:"North"}]->(b) 
RETURN a,b;

Merging a Node with a Label: We can merge a node in the database based on the label using the MERGE clause. If we try to merge a node based on the label, then Neo4j verifies whether there exists any node with the given label. If not, the current node will be created.
MERGE (node:label) RETURN node

e.g: MERGE (Andrew:Developer) RETURN Andrew 

Merging a Node with Properties: We can also merge a node with a set of properties. If we do so, Neo4j searches for an equal match for the specified node, including the properties. If it doesn’t find any, it creates one.
MERGE (node:label {key1:value, key2:value, key3:value . . . . . . . . }) 

e.g: Below query will return the existing node
MERGE (Employee:Developer{name:"himanshu shukla",id:1}) RETURN Employee

If you execute below query, it will create a new Employee with name 'andy shukla' and return it
MERGE (Employee:Developer{name:"andy shukla",id:1}) RETURN Employee

OnCreate and OnMatch : A node is either matched or created when we execute Merge query. With on create and on match, we can set properties for indicating whether the node is created or matched.
MERGE (node:label {properties . . . . . . . . . . .}) 
ON CREATE SET property.isCreated ="true" 
ON MATCH SET property.isFound ="true"

e.g: Since the employee with himanshu shukla is already present, thats why isFound will be set as true.
MERGE (Employee:Developer{name:"himanshu shukla",id:1}) 
ON CREATE SET Employee.isCreated = "true" 
ON MATCH SET Employee.isFound = "true" 
RETURN Employee

Merge a Relationship: Like nodes we can also merge the relationships using the MERGE clause

Set Clause In Neo4j
We can add new properties to an existing Node or Relationship, and also add or update existing Properties values.

Setting a Property: We can create a new property in a node by using the SET clause.
MATCH (node:label{properties . . . . . . . . . . . . . . }) 
SET node.property = value 
RETURN node

Let's create a node:
CREATE(Employee:Developer{name:"seba",id:2})
RETURN Employee;

Now lets add another property 'age' in it.
MATCH(Employee:Developer{name:"seba",id:2})
SET Employee.age=27
RETURN Employee;
Removing a Property: To remove an existing property we need to pass value as NULL.
MATCH (node:label {properties}) 
SET node.property = NULL 
RETURN node 

e.g Let's remove the property 'age' which we added in for an Employee with name 'seba'
MATCH(Employee:Developer{name:"seba",id:2})
SET Employee.age=NULL
RETURN Employee;

Setting Multiple Properties: We can create multiple properties in a node using the Set clause, for this we need to specify these key value pairs with commas.
MATCH (node:label {properties}) 
SET node.property1 = value, node.property2 = value 
RETURN node

e.g:
MATCH(Employee:Developer{name:"seba",id:2})
SET Employee.age=NULL, Employee.maritalstatus=single
RETURN Employee;

Setting a Label on a Node: We can also set a label to an existing node using the SET clause.
MATCH (n {properties . . . . . . . }) 
SET n :label 
RETURN n 

e.g:
MATCH(Employee:Developer{name:"seba",id:2})
SET Employee:Lead
RETURN Employee;

Setting Multiple Labels on a Node: We can set multiple labels to an existing node using the SET clause, for this we specify the labels by separating them with colons “:”.
MATCH (n {properties . . . . . . . }) 
SET n :label1:label2 
RETURN n

e.g:
MATCH(Employee:Developer{name:"seba",id:2})
SET Employee:Lead:Head:Tester
RETURN Employee;

Delete Clause
In Neo4j we can delete nodes and relationships from a database by using the DELETE clause.

Delete a single node
MATCH (n:Person { name: 'UNKNOWN' })  
DELETE n  

Deleting All Nodes and Relationships: Below query will delete all the nodes and relationships from Neo4j.
MATCH (n) DETACH DELETE n

Deleting a Particular Node With Its Relationship: To delete a particular node, we need to mention the details of the node in the place of 'n' in the above mentioned query.
MATCH (node:label {properties . . . . . . . . . .  }) 
DETACH DELETE node

e.g:
MATCH (Employee:Developer{name:"seba",id:2})
DETACH DELETE Employee

Delete relationships only:
MATCH(x)-[r:RELATES_TO]->(y)
DELETE r

e.g:
MATCH (n { name: 'himanshu' })-[r:KNOWS]->()  

DELETE r  

Remove Clause
We can remove properties and labels from Nodes or Relationships using the Remove clause. The main difference between Neo4j CQL DELETE and REMOVE commands is, the DELETE operation is used to delete nodes and associated relationships, where as REMOVE is used to remove labels and properties.

Removing a Property
MATCH (node:label{properties . . . . . . . }) 
REMOVE node.property 
RETURN node

e.g:
MATCH(Employee:Developer{name:"seba",id:2})
REMOVE Employee.id
RETURN Employee;

Removing a Label From a Node
MATCH (node:label {properties . . . . . . . . . . . }) 
REMOVE node:label 
RETURN node 

e.g:
MATCH(Employee:Developer{name:"seba",id:2})
REMOVE Employee:Lead
RETURN Employee;

Removing Multiple Labels: We can remove multiple labels as well.
MATCH (node:label1:label2 {properties . . . . . . . . }) 
REMOVE node:label1:label2 
RETURN node

e.g:
MATCH(Employee:Developer{name:"seba",id:2})
REMOVE Employee:Lead:Tester
RETURN Employee;

ForEach Clause
It is used to update data within a list whether components of a path, or result of aggregation.
MATCH p = (start node)-[*]->(end node) 
WHERE start.node = "node_name" AND end.node = "node_name" 
FOREACH (n IN nodes(p)| SET n.marked = TRUE) 

Lets create a path in Neo4j
CREATE e=(Employee:Employee{name:"himanshu shukla",id:1})-[:WORKS_IN]->(Department:Department{name:"development center"})-[:LOCATED_IN]->(City:City{name:"munich"})
RETURN e

Following is a sample Cypher Query which adds a property to all the nodes along the path using the FOREACH clause.
MATCH x=(Employee)-[*]->(City)
WHERE Employee.name="himanshu shukla" AND City.name="munich"
FOREACH (n IN nodes(x)| SET n.marked = TRUE)
RETURN x;


Match Clause

Get All Nodes Using Match: With Match clause we can retrieve all nodes in the Neo4j database. Query to return all the nodes in Neo4j database.
MATCH (n) RETURN n 

Getting All Nodes Under a Specific Label: We can get all the nodes under a specific label.
MATCH (node:label) RETURN node 

Match by Relationship: We can retrieve nodes based on relationship using the MATCH clause.
MATCH (node:label)<- i="" n="" nbsp="" relationship="" return="">

Delete All Nodes: We can delete all the nodes using the MATCH clause.
MATCH (n) detach delete n 

Optional Match Clause

It is used to search for the pattern described in it, while using nulls for missing parts of the pattern. The only difference OPTIONAL MATCH and a MATCH clause is that it returns null as a result of the missing parts of the pattern.
MATCH (node:label {properties. . . . . . . . . . . . . .}) 
OPTIONAL MATCH (node)-->(x) 
RETURN x

e.g:
MATCH (a:Employee {name:"himanshu shukla"}) 
OPTIONAL MATCH (a)-->(x) 
RETURN x;

Where Clause

Neo4j CQL's where clause is similar to SQL's where, and is used to filter the results of a MATCH Query.
MATCH (label)  
WHERE label.country = "property" 
RETURN label 

e.g:
MATCH (Employee)  
WHERE Employee.name = "himanshu shukla" 
RETURN Employee 

WHERE Clause with Multiple Conditions
e.g:
MATCH (Employee)  
WHERE Employee.name = "himanshu shukla" AND Employee.id="1"
RETURN Employee 

Using Relationship with Where Clause: We can also use Where clause to filter the nodes using the relationships.
MATCH (n) 
WHERE (n)-[: WORKS_IN]->( {name:"development center"}) 
RETURN n 

Count

The count() function is used to count the number of rows.
MATCH (n { name: 'A' })-->(x) 
RETURN n, count(*)

e.g:
MATCH (n{name:"himanshu shukla"})-->(x)
RETURN count(*)

Group Count

The count() clause is also used to count the groups of relationship types.

e.g: Below Cypher Query which counts and returns the number of nodes participating in each relation.
MATCH (n{name:"himanshu shukla"})-[r]-(x)
RETURN type (r), count(*)

Return Clause

We can return  nodes, relationships, and properties in Neo4j by using Return clause.

Returning Nodes:
Create (node:label {properties}) RETURN node 

e.g:
CREATE (him:Employee {name:"himanshu shukla"} return him;

Returning Multiple Nodes: We can also return multiple nodes, e.g:
CREATE (him:Employee {name:"himanshu shukla"}
CREATE (India: Country {name:"India"}
return him, India;

Returning Relationships: We can also return relationships using the Return clause.
CREATE (node1)-[Relationship:Relationship_type]->(node2) 
RETURN Relationship 

e.g
CREATE (him:Employee {name:"himanshu shukla"}
CREATE (India: Country {name:"India"}
CREATE(him)-[r:STAYS_IN]->(India) 
return r;

Returning Properties: Return clause can be used to return properties as well.
Match (node:label {properties....}) 
Return node.property 

e.g:
MATCH (him:Employee {name:"himanshu shukla"})
return him.name;

Returning All Elements: We can also return all the elements in the Neo4j database using the RETURN clause.
Match m=(n{name:"India"})-[r]-(x)
return *;

Returning a Variable With a Column Alias: 
MATCH (him:Employee {name:"himanshu shukla"})
return him.name as Name Of The Employee;

Order By Clause

With the help of ORDER BY clause, we can arrange the result data in order
MATCH (n)  RETURN n.property1, n.property2 .. 
ORDER BY n.property

e.g:
CREATE (him:Employee {name:"himanshu", age:34});
CREATE (him:Employee {name:"sebastian", age:26});
CREATE (him:Employee {name:"ronaldo", age:29});

Following is a sample Cypher Query which returns the above created nodes in the order of there age using the ORDERBY clause.
Match(Employee) 
RETURN Employee.name, Employee.age
ORDER BY Employee.age

Ordering Nodes by Multiple Properties: We can arrange the nodes based on multiple properties using ORDEYBY clause.
MATCH (n) 
RETURN n 
ORDER BY n.age, n.name 

Ordering Nodes by Descending Order: We can arrange the nodes in a database in a descending order using the ORDERBY clause.
MATCH (n) 
RETURN n 
ORDER BY n.name DESC 

Limit Clause

We can limit the number of rows in the output by using the limit clause.
MATCH (n) 
RETURN n 
ORDER BY n.name 
LIMIT 3;

e.g:
Match(Employee) 
RETURN Employee.name, Employee.age
LIMIT 2;

Skip Clause

It is used to define from which row to start including the rows in the output.
MATCH (n) 
RETURN n 
ORDER BY n.name DESC 
SKIP 3;

With Clause

We can chain the queries together using the WITH clause.
MATCH (n) 
WITH n 
ORDER BY n.name DESC LIMIT 3 
RETURN collect(n.name)

Unwind Clause

The unwind clause is used to unwind a list into a sequence of rows.
UNWIND [a, b, c, d] AS x 
RETURN x 

-K Himaanshu Shuklaa..

No comments:

Post a Comment

RSSChomp Blog Directory