This document discusses how property graphs and Cypher queries will be brought to Apache Spark through the Spark Graph project. It provides an overview of property graphs and Cypher, the graph query language. It then demonstrates how a Cypher query would be executed in Spark Graph, including parsing and translating the query, optimizing the logical and physical plans, and executing it using Spark SQL and DataFrames. This will allow graph queries and algorithms to be run efficiently at scale using Spark's distributed processing capabilities.
6. #UnifiedDataAnalytics #SparkAISummit
Property Graphs & Big Data
The Property Graph data model is becoming increasingly mainstream
Cloud graph data services like Azure CosmosDB or Amazon Neptune
Simple graph features in SQLServer 2017, multiple new graph DB products
New graph query language to be standardized by ISO
Neo4j becoming common operational store in retail, finance, telcos … and more
Increasing interest in graph algorithms over graph data as a basis for AI
Apache® Spark is the leading scale-out clustered memory solution for Big Data
Spark 2: Data viewed as tables (DataFrames), processed by SQL,
in function chains, using queries and user functions,
transforming immutable tabular data sets
6
7. #UnifiedDataAnalytics #SparkAISummit
Graphs are coming to Spark
7
[SPARK-25994]
SPIP: Property Graphs, Cypher Queries, and Algorithms
Goal
● bring Property Graphs and the Cypher Query language to
Spark
● the SparkSQL for graphs
Status
● Accepted by the community
● Implementation still Work in Progress
11. #UnifiedDataAnalytics #SparkAISummit
REVIEW
S
name: “Dan”
born: May 29, 1970
twitter: “@dan”
name: “Ann”
born: Dec 5, 1975
date:
Jan 10, 2011
name: “Cars, Inc”
sector:
“automotive”
Property Graph Model Components
Nodes
• The objects in the graph
• Can have name-value
properties
• Can be labeled
KNOWS
KNOWS
FOLLOWS
REVIEW
S
User User
Relationships
• Relate nodes by type and
direction
• Can have name-value
properties Business
12. #UnifiedDataAnalytics #SparkAISummit
Relational Versus Graph Models
Relational Model Graph Model
REVIEWS
REVIEWS
REVIEWS
Alice
Burgers, Inc
Pizza, Inc
Pretzels
User BusinessUser-Business
Alice
Burgers, Inc
Pretzels
Pizza, Inc
14. #UnifiedDataAnalytics #SparkAISummit
Tables for Labels
• In Spark Graph, PropertyGraphs are represented by
– Node Tables and Relationship Tables
• Tables are represented by DataFrames
– Require a fixed schema
• Property Graphs have a Graph Type
– Node and relationship types that occur in the graph
– Node and relationship properties and their data type
Property Graph
Node Tables
Rel. Tables
Graph Type
15. #UnifiedDataAnalytics #SparkAISummit
Tables for Labels
:User:ProAccount
name: Alice
:Business
name: Burgers, Inc
:REVIEWS
id name
0 Alice
id name
1 Burgers, Inc
id source target
0 0 1
:User:ProAccount
:Business
:REVIEWS
Graph Type {
:User:ProAccount (
name: STRING
),
:Business (
name: STRING
),
:REVIEWS
}
16. #UnifiedDataAnalytics #SparkAISummit
Creating a graph
Property Graphs are created from a set of DataFrames.
There are two possible options:
- Using Wide Tables
- one DF for nodes and one for relationships
- column name convention identifies label and property
columns
- Using NodeFrames and RelationshipFrames
- requires a single DataFrame per node label combination and
relationship type
- allows mapping DF columns to properties
16
20. #UnifiedDataAnalytics #SparkAISummit
What is Cypher?
• Declarative query language for graphs
– "SQL for graphs"
• Based on pattern matching
• Supports basic data types for properties
• Functions, aggregations, etc
20
22. #UnifiedDataAnalytics #SparkAISummit
Basic Pattern: Alice's reviews?
(:User {name:'Alice'} ) -[:REVIEWS]-> (business:Business)
REVIEWS
User
Forrest
Gump
VAR LABEL
NODE NODE
?
LABEL PROPERTY
RELATIONSHIP
Type
23. #UnifiedDataAnalytics #SparkAISummit
Cypher query structure
• Cypher operates over a graph and returns a table
• Basic structure:
MATCH pattern
WHERE predicate
RETURN/WITH expression AS alias, ...
ORDER BY expression
SKIP ... LIMIT ...
25. #UnifiedDataAnalytics #SparkAISummit
Query Comparison: Colleagues of Tom Hanks?
SELECT co.name AS coReviewer, count(co) AS nbrOfCoReviews
FROM User AS user
JOIN UserBusiness AS ub1 ON (user.id = ub1.user_id)
JOIN UserBusiness AS ub2 ON (ub1.b_id = ub2.b_id)
JOIN User AS co ON (co.id = ub2.user_id)
WHERE user.name = "Alice"
GROUP BY co.name
MATCH
(user:User)-[:REVIEWS]->(:Business)<-[:REVIEWS]-(co:User)
WHERE user.name = 'Alice'
RETURN co.name AS coReviewer, count(*) AS nbrOfCoReviews
27. #UnifiedDataAnalytics #SparkAISummit
Aggregations
• Cypher supports a number of aggregators
– min(), max(), sum(), avg(), count(), collect(), ...
• When aggregating, non-aggregation projections form a grouping
key:
MATCH (u:User)
RETURN u.name, count(*) AS count
The above query will return the count per unique name
27
28. #UnifiedDataAnalytics #SparkAISummit
Projections
• UNWIND
UNWIND [‘a’, ‘b’, ‘c’] AS list
• WITH
– Behaves similar to RETURN
– Allows projection of values into new variables
– Controls scoping of variables
MATCH (n1)-[r1]->(m1)
WITH n1, collect(r1) AS r // r1, m1 not visible after this
RETURN n1, r
list
‘a’
‘b’
‘c’
31. #UnifiedDataAnalytics #SparkAISummit
In the previous session...
• Property Graph is a growing data model
• Spark Graph will bring Property Graphs
and Cypher to Spark 3.0
• Cypher is "SQL for graphs"
– Based on pattern matching
31
32. #UnifiedDataAnalytics #SparkAISummit
Now that you know Cypher...
val graph: PropertyGraph = …
graph.cypher(
"""
|MATCH (u:User)-[:REVIEWS]->(b:Business)
|WHERE u.name = 'Alice'
|RETURN u.name, b.name
""".stripMargin
).df.show
32
41. #UnifiedDataAnalytics #SparkAISummit
MATCH (u:User)-[:REVIEWS]->(b:Business)
WHERE u.name = 'Alice'
RETURN u.name, b.name
Select(u.name :: STRING, b.name :: STRING), RecordHeader with 2 entries)
╙─Alias(b.name :: STRING AS b.name :: STRING RecordHeader with 15 entries)
╙─Alias(u.name :: STRING AS u.name :: STRING, RecordHeader with 14 entries)
╙─Filter(u.name :: STRING = "Alice", RecordHeader with 13 entries)
╙─Join((target(UNNAMED18 :: RELATIONSHIP(:REVIEWS)) -> b :: NODE)), RecordHeader with 13 entries, InnerJoin)
╟─Join((u :: NODE -> source(UNNAMED18 :: RELATIONSHIP(:REVIEWS))), RecordHeader with 9 entries, InnerJoin)
║ ╟─NodeScan(u :: NODE(:User), RecordHeader 4 entries)
║ ║ ╙─Start(Some(CAPSRecords.unit), session.tmp#1)
║ ╙─RelationshipScan(UNNAMED18 :: RELATIONSHIP(:REVIEWS), RecordHeader with 5 entries)
║ ╙─Start(None)
╙─NodeScan(b :: NODE(:Business), RecordHeader with 4 entries)
╙─Start(Some(CAPSRecords.unit))
Translation of graph operations into relational operations
41
openCypher Frontend
Okapi + Spark
Cypher
Spark SQL
Spark Core
Intermediate
Language
Logical Planning
Relational Planning
Spark Cypher
42. #UnifiedDataAnalytics #SparkAISummit
● Describes the output table of a relational operator
● Maps query expressions (e.g. ‘n.name’ or ‘n:User’) to DataFrame / Table columns
● Used to access columns when evaluating expression during physical execution
● Supports relational operations to reflect data changes (e.g.
header.join(otherHeader))
42
43. #UnifiedDataAnalytics #SparkAISummit
Expression Column Name
NodeVar(n) :: CTNODE(:User) n
HasLabel(n, :User) :: CTBOOLEAN ____n:User
Property(n.name) :: CTSTRING ____n_dot_nameSTRING
43
Select(b, name)
╙─Project(b.name as name)
╙─Project(n as b)
╙─NodeScan(n:User)
44. #UnifiedDataAnalytics #SparkAISummit
Expression Column Name
NodeVar(n) :: CTNODE(:User) n
HasLabel(n, :User) :: CTBOOLEAN ____n:User
Property(n.name) :: CTSTRING ____n_dot_nameSTRING
NodeVar(b) :: CTNODE(:User) n
HasLabel(b, :User) :: CTBOOLEAN ____n:User
Property(b.name) :: CTSTRING ____n_dot_nameSTRING
44
Select(b, name)
╙─Project(b.name as name)
╙─Project(n as b)
╙─NodeScan(n:User)
45. #UnifiedDataAnalytics #SparkAISummit
Expression Column Name
NodeVar(n) :: CTNODE(:User) n
HasLabel(n, :User) :: CTBOOLEAN ____n:User
Property(n.name) :: CTSTRING ____n_dot_nameSTRING
NodeVar(b) :: CTNODE(:User) n
HasLabel(b, :User) :: CTBOOLEAN ____n:User
Property(b.name) :: CTSTRING ____n_dot_nameSTRING
SimpleVar(name) :: CTSTRING ____n_dot_nameSTRING
45
Select(b, name)
╙─Project(b.name as name)
╙─Project(n as b)
╙─NodeScan(n:User)
46. #UnifiedDataAnalytics #SparkAISummit
Expression Column Name
NodeVar(b) :: CTNODE(:User) n
HasLabel(b, :User) :: CTBOOLEAN ____n:User
Property(b.name) :: CTSTRING ____n_dot_nameSTRING
SimpleVar(name) :: CTSTRING ____n_dot_nameSTRING
46
Select(b, name)
╙─Project(b.name as name)
╙─Project(n as b)
╙─NodeScan(n:User)
52. #UnifiedDataAnalytics #SparkAISummit
Neo4j Morpheus
• Incubator for SparkCypher
• Extends Cypher language with
multiple-graph features
• Graph catalog
• Property graph data sources for
integration with Neo4j, SQL DBMS, etc.
https://github.com/opencypher/morpheus
52
53. #UnifiedDataAnalytics #SparkAISummit
Get Involved!
• SPIP was accepted in February
• Current status:
– Core development is poc-complete
– PRs in review
• We are not Spark committers
– Help us review / merge
– Contribute to documentation, Python API
53
54. DON’T FORGET TO RATE
AND REVIEW THE SESSIONS
SEARCH SPARK + AI SUMMIT