Transcript
Page 1: Football graph - Neo4j and the Premier League

In a League of their Own: Neo4j and Premiership Football

Mark Needham@markhneedham

Page 2: Football graph - Neo4j and the Premier League

Outline

• Intro to graphs• When do we need a graph?• Property graph model• Neo4j’s query language• The football graph• Using Neo4j from .NET

Page 3: Football graph - Neo4j and the Premier League

Let’s talk graphs

Page 4: Football graph - Neo4j and the Premier League

You mean these?

Eating Brains

Dancing WithMichael Jackson

Page 5: Football graph - Neo4j and the Premier League

Nope!

Eating Brains

Dancing WithMichael JacksonThese are Charts!

NOT Graphs!

Page 6: Football graph - Neo4j and the Premier League

Ok so what’s a graph then?

Node

Relationship

Page 7: Football graph - Neo4j and the Premier League

The tube

Page 8: Football graph - Neo4j and the Premier League

The social network (graph)

Page 9: Football graph - Neo4j and the Premier League

Complexity

What are graphs good for?

Page 10: Football graph - Neo4j and the Premier League

complexity = f(size, semi-structure, connectedness)

Data Complexity

Page 11: Football graph - Neo4j and the Premier League

Size

Page 12: Football graph - Neo4j and the Premier League

complexity = f(size, semi-structure,

connectedness)

The Real Complexity

Page 13: Football graph - Neo4j and the Premier League

Semi-Structure

Page 14: Football graph - Neo4j and the Premier League

Email: [email protected]: [email protected]: @markhneedhamSkype: mk_jnr1984

USER

CONTACT

CONTACT_TYPE

FIRST_NAME LAST_NAMEUSER_ID EMAIL_1 EMAIL_2 TWITTERFACEBOOK SKYPE

Mark Needham315 [email protected]

[email protected] @markhneedhamNULL mk_jnr1984

Semi-Structure

Page 15: Football graph - Neo4j and the Premier League

complexity = f(size, semi-structure,

connectedness)

The Real Complexity

Page 16: Football graph - Neo4j and the Premier League

Connectedness

Page 17: Football graph - Neo4j and the Premier League

Connectedness

Page 18: Football graph - Neo4j and the Premier League

Connectedness

Page 19: Football graph - Neo4j and the Premier League

When do we need a graph?

Densely Connected

Semi Structured

Page 20: Football graph - Neo4j and the Premier League

Densely connected?

Lots of join tables

Page 21: Football graph - Neo4j and the Premier League

Semi-Structured?

Lots of sparse tables

Page 22: Football graph - Neo4j and the Premier League

Properties of graph databases

• Millions of ‘joins’ per second• Consistent query times as dataset

grows• Join Complexity and

Performance• Easy to evolve data model• Easy to ‘layer’ different types

of data together

Page 23: Football graph - Neo4j and the Premier League

Property Graph Data Model

Page 24: Football graph - Neo4j and the Premier League

Nodes

Page 25: Football graph - Neo4j and the Premier League

Nodes can have properties

• Used to represent entity attributes and/or metadata (e.g. timestamps, version)

• Key-value pairs• Java primitives• Arrays• null is not a valid value

• Every node can have different properties

Page 26: Football graph - Neo4j and the Premier League

What’s a node?

Page 27: Football graph - Neo4j and the Premier League

Relationships

Page 28: Football graph - Neo4j and the Premier League

Relationships

• Relationships are first class citizens • Every relationship has a name and a direction– Add structure to the graph– Provide semantic context for nodes

• Properties used to represent quality or weight of relationship, or metadata

• Every relationship must have a start node and end node

Page 29: Football graph - Neo4j and the Premier League

Relationships

Nodes can have more than one relationship

Self relationships are allowed

Nodes can be connected by more than one relationship

Page 30: Football graph - Neo4j and the Premier League

Labels

Page 31: Football graph - Neo4j and the Premier League

Think Gmail labels

Page 32: Football graph - Neo4j and the Premier League

• Nodes– Entities

• Relationships– Connect entities and structure domain

• Properties– Entity attributes, relationship qualities, and

metadata• Labels– Group nodes by role

Four Building Blocks

Page 33: Football graph - Neo4j and the Premier League

Purposeful abstraction of a domain designed to satisfy particular application/end-user goals

Models

Page 34: Football graph - Neo4j and the Premier League

ModelQuery

Design for Queryability

Page 35: Football graph - Neo4j and the Premier League

ModelModel

Design for Queryability

Page 36: Football graph - Neo4j and the Premier League

ModelQuery

Design for Queryability

Page 37: Football graph - Neo4j and the Premier League

Introducing Cypher

• Declarative Pattern-Matching language• SQL-like syntax• Designed for graphs

Page 38: Football graph - Neo4j and the Premier League

Patterns, patterns, everywhere

A

B C

Page 39: Football graph - Neo4j and the Premier League

(a) --> (b)

a b

It’s all about the ASCII art!

Page 40: Football graph - Neo4j and the Premier League

a b

The most basic query

MATCH (a)-->(b)RETURN a, b

Page 41: Football graph - Neo4j and the Premier League

(a)–[:ACTED_IN]->(m)

a m

Adding in a relationship type

ACTED IN

Page 42: Football graph - Neo4j and the Premier League

a m

Adding in a relationship type

MATCH (a)-[:ACTED_IN]->(m)RETURN a.name, m.name

ACTED IN

Page 43: Football graph - Neo4j and the Premier League

The football graph

Page 44: Football graph - Neo4j and the Premier League

The football graph

Page 45: Football graph - Neo4j and the Premier League

Find Arsenal’s away matches

Page 46: Football graph - Neo4j and the Premier League

Find Arsenal’s away matches

Page 47: Football graph - Neo4j and the Premier League

Find Arsenal’s away matches

MATCH (team:Team)<-[:away_team]-(game)

WHERE team.name = "Arsenal"

RETURN game

Page 48: Football graph - Neo4j and the Premier League

Graph Pattern

MATCH (team:Team)<-[:away_team]-(game)

WHERE team.name = "Arsenal"

RETURN game.name

Page 49: Football graph - Neo4j and the Premier League

Anchor pattern in graph

MATCH (team:Team)<-[:away_team]-(game)

WHERE team.name = "Arsenal"

RETURN game.name

Page 50: Football graph - Neo4j and the Premier League

Create projection of results

MATCH (team:Team)<-[:away_team]-(game)

WHERE team.name = "Arsenal"

RETURN game.name

Page 51: Football graph - Neo4j and the Premier League

Find Arsenal’s away matches

Page 52: Football graph - Neo4j and the Premier League

Evolving the football graph

Page 53: Football graph - Neo4j and the Premier League

Find the top away goal scorers

Page 54: Football graph - Neo4j and the Premier League

Find the top away goal scorers

MATCH (team)<-[:away_team]-(game:Game),

(game)<-[:contains_match]-(season:Season),

(team)<-[:for]-(stats)<-[:played]-(player),

(stats)-[:in]->(game)

WHERE season.name = "2012-2013"

RETURN player.name,

COLLECT(DISTINCT team.name),

SUM(stats.goals) as goals

ORDER BY goals DESC

LIMIT 10

Page 55: Football graph - Neo4j and the Premier League

Multiple graph patterns

MATCH (team)<-[:away_team]-(game:Game),

(game)<-[:contains_match]-(season:Season),

(team)<-[:for]-(stats)<-[:played]-(player),

(stats)-[:in]->(game)

WHERE season.name = "2012-2013"

RETURN player.name,

COLLECT(DISTINCT team.name),

SUM(stats.goals) as goals

ORDER BY goals DESC

LIMIT 10

Page 56: Football graph - Neo4j and the Premier League

Anchor pattern in the graph

MATCH (team)<-[:away_team]-(game:Game),

(game)<-[:contains_match]-(season:Season),

(team)<-[:for]-(stats)<-[:played]-(player),

(stats)-[:in]->(game)

WHERE season.name = "2012-2013"

RETURN player.name,

COLLECT(DISTINCT team.name),

SUM(stats.goals) as goals

ORDER BY goals DESC

LIMIT 10

Page 57: Football graph - Neo4j and the Premier League

Group by player

MATCH (team)<-[:away_team]-(game:Game),

(game)<-[:contains_match]-(season:Season),

(team)<-[:for]-(stats)<-[:played]-(player),

(stats)-[:in]->(game)

WHERE season.name = "2012-2013"

RETURN player.name,

COLLECT(DISTINCT team.name),

SUM(stats.goals) as goals

ORDER BY goals DESC

LIMIT 10

Page 58: Football graph - Neo4j and the Premier League

Find the top away goal scorers

Page 59: Football graph - Neo4j and the Premier League

Other football queries

• Goals scored in each month by Michu• Tottenham results when Gareth Bale

scores• What did Wayne Rooney do in April?• Which players only score when a

game is televised?

Page 60: Football graph - Neo4j and the Premier League

Graph Query Design

Page 61: Football graph - Neo4j and the Premier League

The relational version

Page 62: Football graph - Neo4j and the Premier League

Graph vs Relational

Relational GraphsTables- assume records all have the same structure

Nodes- no need to set a property if it doesn’t exist

Foreign keys between tables- joins calculated at run time- the more tables you join to a query the slower the query gets

Relationships- stored as a ‘Pre-computed index’ at write time- very easy to do lots of ‘hops’ between relationships

Page 63: Football graph - Neo4j and the Premier League

.NET and Neo4j

REST Client

Application

HTTP

Neo4j Server

Page 64: Football graph - Neo4j and the Premier League

Neo4jClient

.NET and Neo4j

Application

HTTP

Neo4j Server

REST Client

Page 65: Football graph - Neo4j and the Premier League

.NET and Neo4j

Page 66: Football graph - Neo4j and the Premier League

.NET and Neo4j

Page 67: Football graph - Neo4j and the Premier League

.NET and Neo4j

Page 68: Football graph - Neo4j and the Premier League

.NET and Neo4j

Page 69: Football graph - Neo4j and the Premier League

.NET and Neo4j

Page 70: Football graph - Neo4j and the Premier League

Thinking in graphs

Page 71: Football graph - Neo4j and the Premier League

Graphs should be fun!

Page 72: Football graph - Neo4j and the Premier League

Ask for help if you get stuck

Last Wednesday of the month

Page 73: Football graph - Neo4j and the Premier League

Come take a copy, it’s free!www.graphdatabases.com

Page 74: Football graph - Neo4j and the Premier League

Questions?

Mark [email protected]@neotechnology.com


Top Related