Microsoft PowerPoint – 25- Neo4J_Part2
© 2018 A. Alawini
Neo4j: Graph Database
Abdu Alawini
University of Illinois at Urbana-Champaign
CS411: Database Systems
December 5, 2018
1
© 2018 A. Alawini
Announcements
• HW 5 is due on 12/9
• 2nd Midterm review session: Mon. 12/10
• 2nd Midterm Exam on Wednesday 12/12
•1404 Siebel Center
•1024 Chemistry Annex
2
© 2018 A. Alawini
Last lecture!
•Map-Reduce
•Introduction to Neo4J
•Cypher: A Graph Query Language
• Querying Nodes and Relationships using Patterns
3
© 2018 A. Alawini
•Cypher: A Graph Query Language
•Manipulating Graph Data
•Aggregation
•Composing Statements
•Indexing and Constraints
•From Relational to Graph Databases
Today’s Lecture
4
© 2018 A. Alawini
Manipulating Graph Data
5
Create statement creates nodes and relationships specified in the
pattern
© 2018 A. Alawini
FOREACH Statement
6
https://neo4j.com/docs/cypher-manual/current/clauses/foreach/
© 2018 A. Alawini
Connecting Nodes
7
© 2018 A. Alawini
8
© 2018 A. Alawini
DELETE, REMOVE and SET Commands
9
To delete a node:
To delete a node and
all its relationships:
To remove a property:
To update or add
a property:
MATCH p =(begin)-[*]->(END )
WHERE begin.name = ‘A’ AND END .name = ‘D’
FOREACH (n IN nodes(p)| SET n.marked = TRUE )
Update a property
using FOREACH
© 2018 A. Alawini
•acts like a combination of MATCH or CREATE
•checks for the existence of data first before creating it
Completing patterns: Merge
10
© 2018 A. Alawini
11
© 2018 A. Alawini
ON CREATE, ON MATCH
12
Running the above command for the first time
Running the same command for the second time
Source: https://neo4j.com/developer/cypher-query-language/
© 2018 A. Alawini
•Cypher: A Graph Query Language
Manipulating Graph Data
•Aggregation
•Composing Statements
•Indexing and Constraints
•From Relational to Graph Databases
Outline
13
© 2018 A. Alawini
Aggregation
14
common aggregation functions are supported:
count, sum, avg, min, and max
© 2018 A. Alawini
• Collect() function collects all aggregated values into a list
COLLECT
15
© 2018 A. Alawini
•Cypher: A Graph Query Language
Manipulating Graph Data
Aggregation
•Composing Statements
•Indexing and Constraints
•From Relational to Graph Databases
Outline
16
© 2018 A. Alawini
•UNION combines the results of two statements that
have the same result structure
Composing Statements: UNION
17
Equivalent
Query
Source: https://neo4j.com/developer/cypher-query-language/
© 2018 A. Alawini
•WITH clause combines individual parts of a query and
declare which data flows from one to the other.
•WITH is like RETURN with the difference that it
doesn’t finish a query but prepares the input for the
next part.
Composing Statements: WITH
18
© 2018 A. Alawini
WITH Example
19
Source: https://neo4j.com/developer/cypher-query-language/
© 2018 A. Alawini
•Cypher: A Graph Query Language
Manipulating Graph Data
Aggregation
Composing Statements
•Indexing and Constraints
•From Relational to Graph Databases
Outline
20
© 2018 A. Alawini
Indexing and Constraints
21
•Goal of indexing: find the starting point in the graph
as fast as possible
•Unique constraints guarantee uniqueness of a certain
property on nodes with a specific label.
Interested in DB Tuning?
http://neo4j.com/docs/developer-manual/current/cypher/query-tuning/using/
© 2018 A. Alawini
•Listing database indexes
•Dropping an Index
Index Management
22
© 2018 A. Alawini
Cypher: A Graph Query Language
Manipulating Graph Data
Aggregation
Composing Statements
Indexing and Constraints
•From Relational to Graph Databases
Outline
23
© 2018 A. Alawini
•Graph databases store relationships and connections as
first-class entities: “Property Graph Model”
From Relational to Graph Databases
24
RDBMS Graph Databases
Tables Set of Nodes/Relationships
Rows Nodes
Columns and data Data properties and values
Constraints Relationships
Joins Traversals
© 2018 A. Alawini
From Relational to Graph Databases
25
Relational
Model
Graph Model
Source: https://neo4j.com/developer/graph-db-vs-rdbms/#_from_relational_to_graph_databases
© 2018 A. Alawini
DB=>Graph Data Model Transformation
26
Source: https://neo4j.com/developer/graph-db-vs-rdbms/#_from_relational_to_graph_databases
© 2018 A. Alawini
Northwind Example
27
Source: https://neo4j.com/developer/cypher-query-language/
© 2018 A. Alawini
Northwind: Graph Model
28
Source: https://neo4j.com/developer/cypher-query-language/
Customer
PURCHASED
© 2018 A. Alawini
•Select everything from the products table
Querying Northwind DB: SQL vs. Neo4J
29
SELECT p.*
FROM products as p;
SQL
MATCH (p:Product)
RETURN p;
Cypher
Source: https://neo4j.com/developer/cypher-query-language/
© 2018 A. Alawini
•Select Product Name and Price from products table
SQL vs. Neo4J: Projection
30
SELECT p.ProductName, p.UnitPrice
FROM products as p
ORDER BY p.UnitPrice DESC
LIMIT 10;
SQL
Cypher MATCH (p:Product)
RETURN p.productName, p.unitPrice
ORDER BY p.unitPrice DESC
LIMIT 10;
Source: https://neo4j.com/developer/cypher-query-language/
© 2018 A. Alawini
•Select Product Name and Price for ”Chocolate”
SQL vs. Neo4J: Filtering
31
SELECT p.ProductName, p.UnitPrice
FROM products AS p
WHERE p.ProductName = ‘Chocolate’;
SQL
Cypher
MATCH (p:Product {productName:”Chocolate”})
RETURN p.productName, p.unitPrice;
OR
MATCH (p:Product)
WHERE p.productName = “Chocolate”
RETURN p.productName, p.unitPrice;
Source: https://neo4j.com/developer/cypher-query-language/
© 2018 A. Alawini
•List expensive products that starts with C.
SQL vs. Neo4J: Filtering
32
SELECT p.ProductName, p.UnitPrice
FROM products AS p
WHERE p.ProductName LIKE ‘C%’
AND p.UnitPrice > 100;
SQL
Cypher MATCH (p:Product)
WHERE p.productName STARTS WITH “C”
AND p.unitPrice > 100
RETURN p.productName, p.unitPrice;
Source: https://neo4j.com/developer/cypher-query-language/
© 2018 A. Alawini
•Who bought Chocolate?
SQL vs. Neo4J: Joining vs. Traversing
33
SQL
Cypher MATCH (p:Product {productName:”Chocolade”})<- [:PRODUCT]-(:Order)<-[:PURCHASED]-(c:Customer) RETURN distinct c.companyName; SELECT DISTINCT c.CompanyName FROM customers AS c JOIN orders AS o ON (c.CustomerID = o.CustomerID) JOIN order_details AS od ON (o.OrderID = od.OrderID) JOIN products AS p ON (od.ProductID = p.ProductID) WHERE p.ProductName = 'Chocolade'; Source: https://neo4j.com/developer/cypher-query-language/ © 2018 A. Alawini •Find top-selling employees SQL vs. Neo4J: Aggregation 34 SELECT e.EmployeeID, count(*) AS Count FROM Employee AS e JOIN Order AS o ON (o.EmployeeID = e.EmployeeID) GROUP BY e.EmployeeID ORDER BY Count DESC; SQL Cypher MATCH (:Order)<-[:SOLD]-(e:Employee) RETURN e.name, count(*) AS cnt ORDER BY cnt DESC; Source: https://neo4j.com/developer/cypher-query-language/ © 2018 A. Alawini Cypher: A Graph Query Language Manipulating Graph Data Aggregation Composing Statements Indexing and Constraints From Relational to Graph Databases Outline 35 © 2018 A. Alawini •Graph databases store relationships and connections as first-class entities •Graph databases have good performance when dealing with connected data •Cypher is a declarative pattern-matching graph query language •Querying connected data is easier with Cypher Summary 36