A Talk on the Graph Database with tutorials
Introduction to the Graph databases and Cypher Query Language
Comparison of the SQL and the Cypher implementations
2. Overview
Goal
Understand the benefits of Cypher query language
Graph Data Models and Query Languages
Basics of Cypher Query Language
Features and Execution Structure
3. Who am I
Ph.D Kisung Kim - Chief Technology Officer of Bitnine Global Inc.
Developed a distributed relational database engine in TmaxSoft
Lead the development of a new graph database, Agens Graph in Bitnine
Global
Graph Enthusiast !
4. Graph Data Model and Query Languages
● Property graph model
○ Cypher query language from Cypher
○ AQL from ArangoDB
○ OrientDB’s SQL dialect
○ Tinkerpop API, a domain specific language, from Titan
● RDF graph model
○ W3C standard recommendation for the Semantic Web
○ SPARQL query language
● Facebook’s GraphQL (?)
5. Property Graph Model
Terminology:
Node(vertex) - Entity
Relationships(Edge)
Property - Attribute
Label(type) - Group nodes and relationships
person company
works_for
Name: Kisung Kim
Email: kskim@bitnine.net
Name: Bitnine Global
Homepage: http://bitnine.net
title: CTO
Team: agens graph
Property
Node
Relationship
Very intuitive and easy
to model E-R diagram to property graphs
6. Cypher
● Declarative query language for the property graph model
○ Inspired by SQL and SPARQL
○ Designed to be human-readable query language
● Developed by Neo technology Inc. since 2011
● Cypher is now evolving
○ Current version is 3.0
● OpenCypher.org
○ Participate in developing the query language
8. Cypher is Human-Readable
Finding all ancestor-descendant pairs in the graph
with recursive
as (
select
parent, child as descendant,
1 as level from source
union all
select
d.parent, s.child, d.level + 1
from descendants as d
join source s on d.descendant = s.parent
)
select * from descendants
order by parent, level, descendant ;
SQL
MATCH
p=(descendant)-[:Parent*]->(ancestor)
RETURN
(ancestor), (descendant), length(p)
ORDER BY (ancestor), (descendant), length(p)
Cypher
descendant ancestor
10. Graph Pattern Matching
● Graph pattern matching is at the heart of Cypher
● Find subgraphs which are matched to the specified graph pattern
○ Subgraph isomorphism
Query Pattern
Graph Data
11. Graph Pattern
● How can we represent graph patterns in a query?
○ Use ASCII art to represent the graph pattern easily
○ Like a diagram
● Node : ( )
● Relationship : --> or <-- (with direction),
-- (without direction)
● Node label : ( :LABEL_NAME )
● Relationship type : -[ :TYPE_NAME ]->
:Actor :Movie
ACTS_IN
(:Actor)-[:ACTS_IN]->(:Movie)
14. MATCH Clause
● Find the specified patterns
● Return matched variables to the next clause
MATCH
(a:Person)-[:RATED]->( m:Movie)<-[:RATED]-( c:Person),(a)-[:FRIEND]-(c
)
a (node) m (node) c (node)
Results of MATCH clause
15. Cypher Clause
● For reading
○ MATCH / OPTIONAL MATCH
● For updating
○ CREATE
○ MERGE
○ SET
● For filtering
○ WHERE
● For handling results
○ WITH, RETURN
○ And ORDER BY, LIMIT, SKIP https://s3.amazonaws.com/artifacts.opencypher.org/M02/railroad/Cypher.html
16. Cypher Query Structure
Pipelined Execution
Clauses are provided results from the former clause
MATCH
MATCH
RETURN
tom movie
tom movie nicole
tom.name movie.title nicole.name
Clause Chain Result Format
17. Uniqueness in Pattern Matching
● Cypher defines that pattern matching does not match a relationship to be
matched to several relationships in a pattern
● If we want multiple matching, then separate into multiple MATCH clauses
b
a
c
Graph Data Query Pattern
Friend r1:Friend r2:Friend
MATCH (a)-[r1:Friend]->(b), (a)-[r2:Friend]->(c) MATCH (a)-[r1:Friend]->(b)
MATCH (a)-[r2:Friend]->(c)
18. Variable Length Relationship Matching
● One of the important features of the Cypher
● Finding all people links between two specified persons
Kisung Kim Joshua
MATCH
(:person {name: “Kisung Kim”})- [:friends*]->(:person {name: “Joshua”})
MATCH
(:person {name: “Kisung Kim”})- [:friends*3..5]->(:person {name:
“Joshua”})
Specify the path length 3 to 5
?
19. Summary
Query database using graph patterns using Cypher
Cyper features
● Graph pattern syntax
● Uniqueness restriction of relationship matching
● Pipelined execution structure
● Variable length path matching
Graph query is much easier than SQL query