An Introduction to Graph Databases with Neo4j

The future of Database is Graph
 

         (By: Nirmal V) - [Date: 21 June 2015] -> (Location: Kerala Javascript User Group meetup - June 2015)

The future of Database is Graph

Not Only SQL

NOSQL

The future of Database is Graph

Different categories of NoSQL

CAP Theroem

The future of Database is Graph

  • Consistency (all nodes see the same data at the same time)

  • Availability (a guarantee that every request receives a response about whether it succeeded or failed)

  • Partition tolerance (the system continues to operate despite arbitrary partitioning due to network failures)

Four Building Blocks

  1. Nodes
  2. Relationships
  3. Properties
  4. Labels

The future of Database is Graph

The future of Database is Graph

The future of Database is Graph

 

RDBMS                                   Graph Database

Tables                                     Graphs
Rows                                        Nodes

Columns and data                Properties and its values. 

Constraints                            Relationships

Joins                                         Traversals

The future of Database is Graph

The future of Database is Graph

The future of Database is Graph

The future of Database is Graph

Google Plus

The future of Database is Graph

Facebook

The future of Database is Graph

Neo4j Advantages

Neo4j Drawbacks or Limitations

It is very easy to represent connected data.

It is very easy and faster to retrieve/traversal/navigation of more Connected data.

Neo4j CQL query language commands are in human readable format and very easy to learn.

It uses simple and powerful data model.

It does NOT require complex Joins to retrieve connected/related data as it is very easy to retrieve it's adjacent node or relationship details without Joins or Indexes.

AS of Neo4j 2.1.3 latest version, it has a limitation of supporting number of Nodes, Relationships and Properties.

It does not support Sharding.

The future of Database is Graph

Neo4j CQL Data types

CQL Data Type                    Usage

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.

double                                     It is used to represent 64-bit floating-point numbers.

char                                          It is used to represent 16-bit characters.

String                                       It is used represent Strings.

 

                         

The future of Database is Graph

Neo4j CQL - CREATE

To create Nodes without properties

To create Nodes with Properties

To create Relationships between Nodes without Properties

To create Relationships between Nodes with Properties

To create single or multiple labels to a Node or a Relationship

Neo4j CQL CREATE A Node Without Properties

The future of Database is Graph

Neo4j CQL CREATE A Node With Properties

CREATE (<node-name>:<label-name>)
CREATE (emp:Employee)
CREATE (
   <node-name>:<label-name>
   { 	
      <Property1-name>:<Property1-Value>
      ........
      <Propertyn-name>:<Propertyn-Value>
   }
)
CREATE (dept:Dept { deptno:10,dname:"Accounting",location:"Hyderabad" })
CREATE (dept:Dept)
CREATE (emp:Employee{id:123,name:"Lokesh",sal:35000,deptno:10})

Neo4j CQL - MATCH & RETURN Commands

The future of Database is Graph

MATCH 
(
   <node-name>:<label-name>
)
RETURN 
   <node-name>.<property1-name>,
   ...
   <node-name>.<propertyn-name>
MATCH (dept: Dept)
RETURN dept.deptno,dept.dname,dept.location
MATCH (dept: Dept)
RETURN dept

Neo4j CQL CREATE a Node Label

The future of Database is Graph

CREATE (<node-name>:<label-name>)
CREATE (google1:GooglePlusProfile)

Multiple Labels to a Node

CREATE (<node-name>:<label-name1>:<label-name2>.....:<label-namen>)
CREATE (m:Movie:Cinema:Film:Picture)

Single Label to a Relationship

CREATE (<node1-name>:<label1-name>)-
	[(<relationship-name>:<relationship-label-name>)]
	->(<node2-name>:<label2-name>)
CREATE (p1:Profile1)-[r1:LIKES]->(p2:Profile2)

Neo4j CQL - WHERE Clause

The future of Database is Graph

MATCH (emp:Employee) 
WHERE emp.name = 'Abc'
RETURN emp
MATCH (emp:Employee) 
WHERE emp.name = 'Abc' OR emp.name = 'Xyz'
RETURN emp
MATCH (<node1-label-name>:<node1-name>),(<node2-label-name>:<node2-name>) 
WHERE <condition>
CREATE (<node1-label-name>)-[<relationship-label-name>:<relationship-name>
       {<relationship-properties>}]->(<node2-label-name>) 
MATCH (cust:Customer),(cc:CreditCard) 
WHERE cust.id = "1001" AND cc.id= "5001" 
CREATE (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014",price:55000}]->(cc) 
RETURN r

Create Relationship with WHERE clause

Neo4j CQL - DELETE

The future of Database is Graph

MATCH (e: Employee) DELETE e
MATCH (cc: CreditCard)-[rel]-(c:Customer) 
DELETE cc,c,rel

Neo4j CQL - Remove

The future of Database is Graph

MATCH (book { id:122 })
REMOVE book.price
RETURN book
ALTER TABLE BOOK REMOVE COLUMN PRICE;
SELECT * FROM BOOK WHERE ID = 122;

It is similar to the below SQL Command.

MATCH (dc:DebitCard) 
REMOVE dc.cvv
RETURN dc
MATCH (m:Movie) 
REMOVE m:Picture

Remove a label clause syntax

Neo4j CQL - SET

The future of Database is Graph

MATCH (dc:DebitCard)
SET dc.atm_pin = 3456
RETURN dc
MATCH (emp:Employee)
RETURN emp.empid,emp.name,emp.salary,emp.deptno
ORDER BY emp.name

Order-By

MATCH (emp:Employee)
RETURN emp.empid,emp.name,emp.salary,emp.deptno
ORDER BY emp.name DESC

Neo4j CQL - UNION

The future of Database is Graph

<MATCH Command1>
   UNION
<MATCH Command2>
MATCH (cc:CreditCard)
RETURN cc.id as id,cc.number as number,cc.name as name,
   cc.valid_from as valid_from,cc.valid_to as valid_to
UNION
MATCH (dc:DebitCard)
RETURN dc.id as id,dc.number as number,dc.name as name,
   dc.valid_from as valid_from,dc.valid_to as valid_to

UNION ALL Clause

<MATCH Command1>
UNION ALL
<MATCH Command2>
MATCH (cc:CreditCard)
RETURN cc.id as id,cc.number as number,cc.name as name,
   cc.valid_from as valid_from,cc.valid_to as valid_to
UNION ALL
MATCH (dc:DebitCard)
RETURN dc.id as id,dc.number as number,dc.name as name,
   dc.valid_from as valid_from,dc.valid_to as valid_to

Neo4j Limit and skip clauses

The future of Database is Graph

MATCH (emp:Employee) 
RETURN emp
LIMIT 2
MATCH (emp:Employee) 
RETURN emp
SKIP 2

Merge

MERGE (gp2:GoogleProfile2{ Id: 201402,Name:"Nokia"})
MATCH (e:Employee) 
WHERE e.id IS NOT NULL
RETURN e.id,e.name,e.sal,e.deptno

Null keyword

In Operator

MATCH (e:Employee) 
WHERE e.id IN [123,124]
RETURN e.id,e.name,e.sal,e.deptno

The future of Database is Graph

Function                                  Description

Upper                                        It is used to change all letters into uppercase letters.
Lower                                         It is used to change all letters into lowercase letters.

Substring                                  It is used to get substring of a given string.

Replace                                      It is used to replace a substring with given substring of a string.

 

 

                         

Neo4J CQL - String Functions

MATCH (e:Employee) 
RETURN e.id,UPPER(e.name),e.sal,e.deptno

The future of Database is Graph

Function                                  Description

COUNT                                     It returns the number of rows returned by MATCH command.
MAX                                           It returns the maximum value from a set of rows returned by MATCH command.

MIN                                            It returns the minimum value from a set of rows returned by MATCH command.

SUM                                           It returns the summation value of all rows returned by MATCH command. 

AVG                                            It returns the average value of all rows returned by MATCH command.

 

 

                         

Neo4J CQL - AGGREGATION Functions List

MATCH (e:Employee) 
RETURN MAX(e.sal),MIN(e.sal)

The future of Database is Graph

Function                                  Description

STARTNODE                           It is used to know the start node of a relationship.
ENDNODE                               It is used to know the end node of a relationship.

ID                                               It is used to know the ID of a relationship.

TYPE                                         It is used to know the TYPE of a relationship in string representation.

 

 

 

                         

Neo4J CQL - Relationship Functions List

MATCH (a)-[movie:ACTION_MOVIES]->(b) 
RETURN STARTNODE(movie)
MATCH (a)-[movie:ACTION_MOVIES]->(b) 
RETURN ID(movie),TYPE(movie)

Neo4J CQL - Index

The future of Database is Graph

CREATE INDEX ON :<label_name> (<property_name>)
CREATE INDEX ON :Customer (name)
DROP INDEX ON :Customer (name)
DROP INDEX ON :<label_name> (<property_name>)

Drop Index syntax:

Create Index syntax:

Neo4J CQL - Unique Constraint

The future of Database is Graph

CREATE CONSTRAINT ON (<label_name>)
ASSERT <property_name> IS UNIQUE
CREATE CONSTRAINT ON (cc:CreditCard)
ASSERT cc.number IS UNIQUE
DROP CONSTRAINT ON (cc:CreditCard)
ASSERT cc.number IS UNIQUE
DROP CONSTRAINT ON (<label_name>)
ASSERT <property_name> IS UNIQUE

Drop Index syntax:

Create unique constraint syntax:

Questions?

nirmal@ndimensionz.com
nirmalkamath@gmail.com

The future of Database is Graph

Thank you :-)

@nirmalkamath

The future of Database is Graph

neo4j

By phpnirmal

neo4j

  • 2,541