The 2nd graph database in sv meetup

20
Introduction to Cypher Graph Query Language October 6, 2016 Graph Databases in Silicon Valley Meetup

Transcript of The 2nd graph database in sv meetup

Page 1: The 2nd graph database in sv meetup

Introduction to Cypher Graph Query Language

October 6, 2016Graph Databases in Silicon Valley Meetup

Page 2: The 2nd graph database in sv meetup

OverviewGoal

Understand the benefits of Cypher query language

Graph Data Models and Query Languages

Basics of Cypher Query Language

Features and Execution Structure

Page 3: The 2nd graph database in sv meetup

Who am IPh.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 !

Page 4: The 2nd graph database in sv meetup

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 (?)

Page 5: The 2nd graph database in sv meetup

Property Graph ModelTerminology: Node(vertex) - Entity

Relationships(Edge)Property - AttributeLabel(type) - Group nodes and relationships

person companyworks_for

Name: Kisung KimEmail: [email protected]

Name: Bitnine GlobalHomepage: http://bitnine.net

title: CTOTeam: agens graph

Property

Node

Relationship

Very intuitive and easy

to model E-R diagram to property graphs

Page 6: The 2nd graph database in sv meetup

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

Page 7: The 2nd graph database in sv meetup

OpenCypher.org

http://www.slideshare.net/neo4j/the-opencypher-project-an-open-graph-query-language

Page 8: The 2nd graph database in sv meetup

Cypher is Human-ReadableFinding all ancestor-descendant pairs in the graph

with recursiveas ( 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

Page 9: The 2nd graph database in sv meetup

Cypher Query ExampleGraph Pattern

Results

Page 10: The 2nd graph database in sv meetup

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

Page 11: The 2nd graph database in sv meetup

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 :MovieACTS_IN

(:Actor)-[:ACTS_IN]->(:Movie)

Page 12: The 2nd graph database in sv meetup

Graph Pattern● Property

○ Node property: ( { field : value, … } )○ Relationship property: -[ { field : value, … } ]->

● Assign nodes and relations to variables

:Actorname=’TOM’ :Movie (:Actor {name: “TOM”})-[:ACTS_IN]->(:Movie)

ACTS_IN

(a:Actor {name: “TOM”})-[ r:ACTS_IN]->(b:Movie)

Page 13: The 2nd graph database in sv meetup

Graph PatternRepresent more complicated patterns

:Person :MovieRATED

:PersonRATED

FRIEND

(a:Person)-[:RATED]->(m:Movie)<-[:RATED]-( c:Person),(a)-[:FRIEND]-(c)

Path1

Path2

Path1

Path2

Page 14: The 2nd graph database in sv meetup

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

Page 15: The 2nd graph database in sv meetup

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

Page 16: The 2nd graph database in sv meetup

Cypher Query StructurePipelined ExecutionClauses 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

Page 17: The 2nd graph database in sv meetup

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)

Page 18: The 2nd graph database in sv meetup

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?

Page 19: The 2nd graph database in sv meetup

SummaryQuery 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

Page 20: The 2nd graph database in sv meetup

Thank youHow do you feel about Graph Database?