graph database using neo4j

29
Graph Database Using Neo4J By Harmeet Singh(Taara) (Java EE Developer) Email: [email protected] Website: http://programmers-nest.com Blog: http://harmeetsingh13.blogspot.com Skype: harmeetsingh0013

Upload: harmeet-singhtaara

Post on 15-Aug-2015

315 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Graph Database Using Neo4J

Graph DatabaseUsing Neo4J

By Harmeet Singh(Taara)(Java EE Developer)

Email: [email protected]: http://programmers-nest.com

Blog: http://harmeetsingh13.blogspot.comSkype: harmeetsingh0013

Page 2: Graph Database Using Neo4J

Contents

➔ Introduction➔ Big Data➔ Graph Databases➔ Graph DB Vs RDBMS➔ Journey: RDBMS To Graph DB Modeling➔ Neo4J➔ Cypher Query

◆ CREATE, MATCH, WHERE, SET, DELETE, RETURN, REMOVE◆ Relationship◆ ORDER BY, SKIP, LIMIT, DISTINCT◆ Aggregation

➔ Spring-Data-Neo4J Sample➔ Leftover: The things we didn't cover

Page 3: Graph Database Using Neo4J

Acknowledgement

➔ Thanks To My Parents.

➔ Thanks To All Who Support Me or Not.

➔ Dedicated To My Teacher “Mr. Kapil Sakhuja”.

Page 4: Graph Database Using Neo4J

Introduction

➔ Today, we discuss about Graph Database and Why Graph Databases involved.

➔ How we use Graph Database using Neo4J.

➔ Cypher Query Language for Neo4J.

➔ Spring-Data-Neo4J Sample Application.

Page 5: Graph Database Using Neo4J

BigData

Page 6: Graph Database Using Neo4J

Graph Database

➔ A Graph Database is a set of vertices and edges.

➔ Graph Databases is to view the data as an arbitrary set of objects connected by one or more kinds of relationships.

Page 7: Graph Database Using Neo4J

Graph DB Vs RDBMS

➔ RDBMS limitation on How a relationship is defined within a relational database?

➔ In RDBMS creating a join table that brings together two disparate tables is a common practice, doing so adds a layer of complexity.

Page 8: Graph Database Using Neo4J

Graph DB Vs RDBMS

➔ A join table is created in order to have metadata that provides properties about relationships between two tables. When a similar relationship needs to be created among other tables, yet another join table must be created.

➔ Graph databases over relational database is to avoid what might be referred to as “join hell”

Page 9: Graph Database Using Neo4J

Journey: RDBMS to Graph DB Modeling

➔ In RDBMS, the data is collected in form of Tables, and the Tables are define with Rows And Columns.

➔ The single table contains Multiple Records and these records are represent to real world Entity.

Page 10: Graph Database Using Neo4J

Journey: RDBMS to Graph DB Modeling

➔ Now, in Graph Database the data represent in the form of Nodes and One node is compared to one record in table.

➔ The Node type is compared to Entity.➔ In Graph DB, we can create easy relationships with

nodes.

Page 11: Graph Database Using Neo4J

Neo4J

Necessity Is The Mother Of Invention

➔ Neo4j began its life in 2000, when Emil Eifrem, Johan Svensson, and Peter Naubauer.

➔ World’s Best And First Graph Database.

Page 12: Graph Database Using Neo4J

Neo4J

➔ The “j” in Neo4j stands for Java, and the Java Development Kit (JDK) is required to run it.

➔ Neo aimed to introduce a database that offered a better way to model, store, and retrieve data while keeping all of the core concepts—such as ACIDity, transactions, and so forth—that made relational databases into a proven commodity.

Page 13: Graph Database Using Neo4J

Cypher Query Language

➔ Cypher is the Declarative Query Language used for data manipulation in Neo4j.

➔ A Declarative Language is a high-level type of language in which the purpose is to instruct the application on what needs to be done or what you want from the application, as opposed to how to do it.

➔ Cypher is a Case Sensitive Language.

Page 14: Graph Database Using Neo4J

Cypher Query Language

➔ Cypher is a declarative, SQL-inspired language for describing patterns in graphs. It allows us to describe what we want to select, insert, update or delete from a Graph Database without requiring us to describe exactly how to do it.

➔ Cypher is not yet a standard graph database language that can interact with other graph database platforms.

Page 15: Graph Database Using Neo4J

CREATE

➔ SQL◆ INSERT INTO User (name, age) values

(“James”, 26)

➔ Cypher◆ CREATE (u:User {name:"James",age:"26"})

RETURN u

Page 16: Graph Database Using Neo4J

MATCH

➔ SQL◆ SELECT * FROM User◆ SELECT u.name FROM USER u

➔ Cypher◆ MATCH (u:User) RETURN u◆ MATCH (u:User) RETURN u.name

Page 17: Graph Database Using Neo4J

WHERE

➔ SQL◆ SELECT * FROM User u WHERE u.age = 26

➔ Cypher◆ MATCH (u:User {age:26}) RETURN u◆ MATCH (u:User) WHERE u.age = 26 RETURN u

Page 18: Graph Database Using Neo4J

SET

➔ SQL◆ UPDATE User u SET u.age = 26 WHERE u.name

= “James”◆ ALTER TABLE User ADD address varchar(45)

➔ Cypher◆ MATCH (u:User {name:"James"}) SET u.age =

26 RETURN u◆ MATCH (u:User {name:"James"}) SET u.

address = "Moga" RETURN u

Page 19: Graph Database Using Neo4J

DELETE

➔ SQL◆ DELETE FROM User u WHERE u.name IS NULL

➔ Cypher◆ MATCH(u:User) WHERE u.name IS NULL DELETE

u

➔ NOTE: If you delete a node that has relationships, you need to be sure to remove the relationships as well

Page 20: Graph Database Using Neo4J

RETURN

➔ The RETURN is similar to the SELECT statement found in SQL

➔ SQL◆ SELECT u.name AS UserName, u.age AS Age

FROM User u

➔ Cypher◆ MATCH(u:User) RETURN u.name AS UserName,

u.age AS Age

Page 21: Graph Database Using Neo4J

REMOVE

➔ SQL◆ ALTER TABLE User u DROP COLUMN u.address

WHERE u.name = “James”

➔ Cypher◆ MATCH(u:User {name:"James"}) REMOVE u.

address RETURN u

Page 22: Graph Database Using Neo4J

Relationships

➔ CREATE Relation◆ Match (u:User {name:"James"}), (c:Company

{name:"Netsol"}) create (u)-[:EMP]-> (c)

◆ Match (u:User {id:1}), (u1:User {id:2}) create (u)-[:FRIEND {type:"Brothers"}]-> (u1) RETURN u, u1

➔ NOTE: By convention those relationship-types are written all upper case using underscores between words.

Page 23: Graph Database Using Neo4J

Relationships

➔ MATCH◆ (node1)-[rel:TYPE]->(node2)

◆ MATCH(u:User) -[rel:FRIEND]-> (u1:User) RETURN u.name, u1.name, rel.type

◆ MATCH(u:User) -[:FRIEND]-> (u1:User) -[:FRIEND]-> (u2:User) RETURN u, u1, u2

Page 24: Graph Database Using Neo4J

Relationships

➔ MATCH◆ MATCH(u:User) -[*]-> (u1:User) RETURN u,

u1◆ MATCH(u:User) -[*1..5]-> (u1:User) RETURN

u, u1◆ MATCH(u:User) -[*2]-> (u1:User) RETURN u,

u1◆ MATCH(u:User) -[:FRIEND*2]-> (u1:User)

RETURN u, u1

Page 25: Graph Database Using Neo4J

DISTINCT, ORDER BY, SKIP, LIMIT

➔ SQL◆ SELECT DISTINCT u.name FROM User u WHERE

u.age = 25 ORDER BY u.name DESC

OFFSET 0 LIMIT 5

➔ Cypher◆ MATCH(u:User {age:25}) RETURN

DISTINCT u.name ORDER BY u.name DESC

SKIP 0 LIMIT 5

Page 26: Graph Database Using Neo4J

Aggregation

➔ COUNT◆ MATCH(u:User {age:25}) RETURN COUNT(u.

name)

➔ COLLECT◆ MATCH(u:User {age:25}) RETURN COLLECT(u.

name)

➔ NOTE: There are more aggregation functions like min(), max(), avg() etc.

Page 27: Graph Database Using Neo4J

Spring-Data-Neo4j Sample

➔ Please access below link for Spring-Data-Neo4j Sample Application. ◆ https://github.com/harmeetsingh0013/Spring-Data-

Neo4j-Example

Page 28: Graph Database Using Neo4J

Leftover: The things we didn’t cover

➔ Graph Theory➔ Database ACID Operations➔ Graph DB Modeling➔ Advance Cypher Query Language➔ Neo4j Aggregation Functions➔ Neo4J Native Libraries With Java➔ Neo4J Rest API➔ Indexing

Page 29: Graph Database Using Neo4J

References

➔ Practical Neo4J By Gregory Jordan Foreword By Jim Webber

➔ http://neo4j.com/top-ten-reasons/

➔ http://neo4j.com/developer/get-started/