Neo4j Graph Data Modeling

108
(graphs)-[:are]->(everywhere) Graph Data Modeling Neo4j © All Rights Reserved 2014 | Neo Technology, Inc. with @kennybastani

description

Kenny demonstrates how to build a flexible and expressive graph model and related queries that map closely to your domain needs, and which can be evolved as your application evolves.

Transcript of Neo4j Graph Data Modeling

Page 1: Neo4j Graph Data Modeling

(graphs)-[:are]->(everywhere)

Graph����������� ������������������  Data����������� ������������������  ModelingNeo4j

© All Rights Reserved 2014 | Neo Technology, Inc.

with����������� ������������������  @kennybastani

Page 2: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Agenda

•What����������� ������������������  is����������� ������������������  a����������� ������������������  graph����������� ������������������  database?����������� ������������������  

•Introduction����������� ������������������  to����������� ������������������  graphs����������� ������������������  

•Introduction����������� ������������������  to����������� ������������������  the����������� ������������������  labeled����������� ������������������  property����������� ������������������  graph����������� ������������������  data����������� ������������������  model����������� ������������������  

•Introduction����������� ������������������  to����������� ������������������  Cypher����������� ������������������  (Neo4j’s����������� ������������������  query����������� ������������������  language)����������� ������������������  

•Tour����������� ������������������  of����������� ������������������  the����������� ������������������  Neo4j����������� ������������������  browser����������� ������������������  

•Introduction����������� ������������������  to����������� ������������������  graph-based����������� ������������������  modeling����������� ������������������  

•Graph����������� ������������������  data����������� ������������������  modeling����������� ������������������  guidelines����������� ������������������  

•Authoring����������� ������������������  a����������� ������������������  GraphGist

2

Page 3: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

(What����������� ������������������  is����������� ������������������  a����������� ������������������  graph����������� ������������������  database?)

Page 4: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

What����������� ������������������  is����������� ������������������  a����������� ������������������  graph����������� ������������������  database?

•Graph����������� ������������������  databases����������� ������������������  are����������� ������������������  a����������� ������������������  kind����������� ������������������  of����������� ������������������  NoSQL����������� ������������������  database����������� ������������������  

•It����������� ������������������  lets����������� ������������������  you����������� ������������������  store����������� ������������������  and����������� ������������������  manage����������� ������������������  your����������� ������������������  data����������� ������������������  as����������� ������������������  a����������� ������������������  graph����������� ������������������  

•Graphs����������� ������������������  are����������� ������������������  a����������� ������������������  set����������� ������������������  of����������� ������������������  nodes����������� ������������������  and����������� ������������������  relationships����������� ������������������  that����������� ������������������  describe����������� ������������������  some����������� ������������������  arbitrary����������� ������������������  domain����������� ������������������  

•Neo4j����������� ������������������  is����������� ������������������  a����������� ������������������  graph����������� ������������������  database,����������� ������������������  using����������� ������������������  the����������� ������������������  labeled����������� ������������������  graph����������� ������������������  property����������� ������������������  data����������� ������������������  model

4

Page 5: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

(Introduction����������� ������������������  to����������� ������������������  graphs)

Page 6: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

What����������� ������������������  is����������� ������������������  a����������� ������������������  graph?

Page 7: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

Zombie Pastimes

Eating Brains

Dancing With Michael Jackson

% of People Who Hate Michael Jackson by Date

0

25

50

75

100

June 25, 2009 June 26,2009

7

Page 8: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

Zombie Pastimes

Eating Brains

Dancing WithMichael Jackson

% of People Who Hate Michael Jackson by Date

0

25

50

75

100

June 25, 2009 June 26,2009

These

����������� ������������������  are����������� ������������������  C

harts!

NOT����������� ������������������  G

raphs!

8

Page 9: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

So����������� ������������������  what����������� ������������������  about����������� ������������������  data?

Page 10: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

John

Sally

Graph Databases Book

Friend Of

Friend Of

Has Rea

d

Has Read

10

Page 11: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

name: John age: 27

name: Sally age: 32

title: Graph Databases authors: Ian Robinson, Jim Webber

FRIEND_OF since: 01/09/2013

HAS_READ on: 2/03/2013 rating: 5

HAS_READ on: 02/09/2013 rating: 4

FRIEND_OF since: 01/09/2013

11

Page 12: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

The����������� ������������������  Relational����������� ������������������  Table����������� ������������������  Model

Page 13: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

Customers AccountsCustomer_Accounts

143 Alice 326 $100

725 $632

981 $212

143 981

143 725

143 326

13

Page 14: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

(Introduction����������� ������������������  to����������� ������������������  the����������� ������������������  labeled����������� ������������������  property����������� ������������������  graph����������� ������������������  data����������� ������������������  model)

Page 15: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Four����������� ������������������  Building����������� ������������������  Blocks

•Nodes����������� ������������������  

•Relationships����������� ������������������  

•Properties����������� ������������������  

•Labels

15

Page 16: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Nodes

16

Page 17: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Nodes

•Nodes����������� ������������������  are����������� ������������������  used����������� ������������������  to����������� ������������������  represent����������� ������������������  en##es  and  complex  value  types  in����������� ������������������  your����������� ������������������  domain����������� ������������������  

•Nodes����������� ������������������  contain����������� ������������������  properties����������� ������������������  

•Key-value����������� ������������������  pairs����������� ������������������  

• Java����������� ������������������  primitives����������� ������������������  

• Arrays����������� ������������������  

• null����������� ������������������  is����������� ������������������  not����������� ������������������  a����������� ������������������  valid����������� ������������������  value����������� ������������������  

•Every����������� ������������������  node����������� ������������������  can����������� ������������������  have����������� ������������������  different����������� ������������������  properties����������� ������������������  

•Nodes����������� ������������������  do����������� ������������������  not����������� ������������������  have����������� ������������������  to����������� ������������������  have����������� ������������������  the����������� ������������������  same����������� ������������������  keys

17

Page 18: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Relationships

18

Page 19: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Relationships����������� ������������������  (continued)

19

Nodes  can  have  more  than  one  relationship

Self  relationships  are  allowed

Nodes  can  be  connected  by  more  than  one  relationship

Page 20: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Relationships

•Every����������� ������������������  relationship����������� ������������������  has����������� ������������������  a����������� ������������������  name����������� ������������������  and����������� ������������������  a����������� ������������������  direc#on  •Add����������� ������������������  structure����������� ������������������  to����������� ������������������  the����������� ������������������  graph����������� ������������������  

•Provide����������� ������������������  semantic����������� ������������������  context����������� ������������������  for����������� ������������������  nodes����������� ������������������  

•Can����������� ������������������  contain����������� ������������������  properties����������� ������������������  

•Used����������� ������������������  to����������� ������������������  represent����������� ������������������  quality����������� ������������������  or����������� ������������������  weight  of����������� ������������������  relationship,����������� ������������������  or����������� ������������������  metadata����������� ������������������  

•Every����������� ������������������  relationship����������� ������������������  must����������� ������������������  have����������� ������������������  a����������� ������������������  start  node����������� ������������������  and����������� ������������������  end  node  •No����������� ������������������  dangling����������� ������������������  relationships

20

Page 21: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Labels

21

Page 22: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Labels

•Every����������� ������������������  node����������� ������������������  can����������� ������������������  have����������� ������������������  zero  or  more  labels����������� ������������������  

•Used����������� ������������������  to����������� ������������������  represent����������� ������������������  roles����������� ������������������  (e.g.����������� ������������������  user,����������� ������������������  product,����������� ������������������  company)����������� ������������������  

•Group����������� ������������������  nodes����������� ������������������  

•Allow����������� ������������������  us����������� ������������������  to����������� ������������������  associate����������� ������������������  indexes����������� ������������������  and����������� ������������������  constraints����������� ������������������  with����������� ������������������  groups����������� ������������������  of����������� ������������������  nodes

22

Page 23: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Four����������� ������������������  Building����������� ������������������  Blocks

•Nodes����������� ������������������  

•Entities����������� ������������������  

•Relationships����������� ������������������  

•Connect����������� ������������������  entities����������� ������������������  and����������� ������������������  structure����������� ������������������  domain����������� ������������������  

•Properties����������� ������������������  

•Entity����������� ������������������  attributes,����������� ������������������  relationship����������� ������������������  qualities,����������� ������������������  and����������� ������������������  metadata����������� ������������������  

•Labels����������� ������������������  

•Group����������� ������������������  nodes����������� ������������������  by����������� ������������������  role

23

Page 24: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

(Introduction����������� ������������������  to����������� ������������������  Cypher)

Page 25: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Cypher����������� ������������������  is����������� ������������������  Neo4j's����������� ������������������  graph����������� ������������������  query����������� ������������������  language����������� ������������������  

•Declarative����������� ������������������  Pattern-Matching����������� ������������������  language����������� ������������������  

•SQL-like����������� ������������������  syntax����������� ������������������  

•Designed����������� ������������������  for����������� ������������������  graphs

25

Page 26: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

It’s����������� ������������������  all����������� ������������������  about����������� ������������������  Patterns

26

A

B C

Page 27: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.27

Page 28: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.28

Page 29: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

How?����������� ������������������  

Page 30: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

(a) --> (b)

a b

Text����������� ������������������  Art

30

Page 31: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Either����������� ������������������  direction

31

MATCH (a)--(b)

RETURN a, b;

a b

Page 32: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Direction����������� ������������������  specific

32

MATCH (a)-->(b)

RETURN a, b;

a b

Page 33: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.33

b

a

b

a

b

a

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

Page 34: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

(a) -[r]-> ()

a

Relationships

34

r

Page 35: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Two����������� ������������������  nodes,����������� ������������������  one����������� ������������������  relationship

35

MATCH (a)-[r]->()

RETURN a.name, type(r);

ar

Page 36: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

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

a m

Actors����������� ������������������  and����������� ������������������  movies

36

ACTED IN

Page 37: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Return����������� ������������������  properties����������� ������������������  of����������� ������������������  the����������� ������������������  matched����������� ������������������  pattern

37

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

RETURN a.name, m.title;

a mACTED IN

Page 38: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Also����������� ������������������  return����������� ������������������  properties����������� ������������������  of����������� ������������������  the����������� ������������������  relationship

38

MATCH (a)-[r:ACTED_IN]->(m)

RETURN a.name, r.roles, m.title;

a mACTED IN

Page 39: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Paths

39

(a)-->(b)-->(c)

a b c

Page 40: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.40

b

c

a

b

c

a

b

a

(a)-->(b)-->(c)

Page 41: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Paths

41

(a)-->(b)-->(c)

a b c

Page 42: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Paths

42

(a)-->(b)<--(c)

a b c

Page 43: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.43

c

b

a

c

b

c

b

a

a(a)-->(b)<--(c)

Page 44: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Who����������� ������������������  acted����������� ������������������  in����������� ������������������  a����������� ������������������  movie����������� ������������������  and����������� ������������������  who����������� ������������������  was����������� ������������������  the����������� ������������������  director����������� ������������������  of����������� ������������������  that����������� ������������������  movie?

44

MATCH (a)-[:ACTED_IN]->(m)<-[:DIRECTED]-(d)

RETURN a.name, m.title, d.name;

a

mACTED IN

dDIRECTED

Page 45: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.45

MATCH (a)-[:ACTED_IN]->(m)<-[:DIRECTED]-(d) RETURN a.name, m.title, d.name;

d

m

a

d

m

d

m

a

a

Page 46: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Table����������� ������������������  of����������� ������������������  results

46

MATCH (a)-[:ACTED_IN]->(m)<-[:DIRECTED]-(d)

RETURN a.name, m.title, d.name;

a.name m.title d.name“Keanu Reeves” “The Matrix” “Andy Wachowski”“Keanu Reeves” “The Matrix Reloaded” “Andy Wachowski”“Noah Wyle” “A Few Good Men” “Rob Reiner”“Tom Hanks” “Cloud Atlas” “Andy Wachowski”

... ... ...

Page 47: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Many����������� ������������������  aggregation����������� ������������������  functions����������� ������������������  are����������� ������������������  supported

47

count(x) - add up the number of occurrencesmin(x) - get the lowest valuemax(x) - get the highest valueavg(x) - get the average of a numeric valuesum(x) - add up all values

collect(x) - collected all the occurrences into an array

Page 48: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

MATCH (a)-[:ACTED_IN]->(m)<-[:DIRECTED]-(d)

RETURN a.name, d.name, count(*) AS count

ORDER BY count DESC

LIMIT 5;

Sort����������� ������������������  &����������� ������������������  Limit

48

Which����������� ������������������  actor����������� ������������������  and����������� ������������������  director����������� ������������������  worked����������� ������������������  together����������� ������������������  the����������� ������������������  most?

Page 49: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Graph����������� ������������������  Traversal����������� ������������������  (Getting����������� ������������������  from����������� ������������������  point����������� ������������������  A����������� ������������������  to����������� ������������������  point����������� ������������������  ����������� ������������������  B)

Page 50: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

All-nodes����������� ������������������  Query

50

MATCH (n) RETURN n;

Matches every node in the graph

Page 51: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Find����������� ������������������  a����������� ������������������  specific����������� ������������������  node����������� ������������������  (within����������� ������������������  all-nodes)

51

MATCH (n)

WHERE n.name = "Tom Hanks"

RETURN n;

WHERE - filter the results

n.name = "Tom Hanks" - by the name property value

Page 52: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Find����������� ������������������  a����������� ������������������  specific����������� ������������������  node����������� ������������������  (concise����������� ������������������  syntax)

52

MATCH (n {name:"Tom Hanks"})

RETURN n;

MATCH (n) - find patterns in graph

{name:"Tom Hanks"} - with the specified properties

Page 53: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.53

MATCH (n {n.name:"Tom Hanks"}) RETURN n;

t

Page 54: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Find����������� ������������������  a����������� ������������������  specific����������� ������������������  node����������� ������������������  (with����������� ������������������  a����������� ������������������  label)

54

MATCH (tom:Person)

WHERE tom.name="Tom Hanks"

RETURN tom;

:Person - Matches only nodes labeled as Person

Page 55: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.55

MATCH (tom:Person {name:"Tom Hanks"}) RETURN tom;

t

Page 56: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Start����������� ������������������  with����������� ������������������  a����������� ������������������  specific����������� ������������������  (labeled)����������� ������������������  node

56

MATCH (tom:Person)-[:ACTED_IN]->(movie:Movie)

WHERE tom.name="Tom Hanks"

RETURN movie.title;

(Movies featuring Tom Hanks)

MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(movie:Movie)

RETURN movie.title;

Page 57: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.57

m

m

m

t

m

MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(movie:Movie) RETURN movie.title;

Page 58: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Start����������� ������������������  with����������� ������������������  a����������� ������������������  specific����������� ������������������  labeled����������� ������������������  node

58

MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(movie),

(director)-[:DIRECTED]->(movie)

RETURN director.name;

(Directors who worked with Tom Hanks)

Page 59: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.59

MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(movie:Movie) (director:Person)-[:DIRECTED]->(movie)

RETURN director.name;

t

d

d

Page 60: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

(Tour����������� ������������������  of����������� ������������������  the����������� ������������������  Neo4j����������� ������������������  browser)

Page 61: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

http://localhost:7474/

Neo4j����������� ������������������  Browser����������� ������������������  -����������� ������������������  finding����������� ������������������  help

61

Page 62: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Neo4j����������� ������������������  Browser����������� ������������������  Execute����������� ������������������  Cypher,����������� ������������������  Visualize

62

Page 63: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Neo4j����������� ������������������  Browser����������� ������������������  -����������� ������������������  importing����������� ������������������  sample����������� ������������������  data

63

Page 64: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Neo4j����������� ������������������  Data����������� ������������������  browser����������� ������������������  -����������� ������������������  display����������� ������������������  more����������� ������������������  data

64

Page 65: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

(Graph����������� ������������������  search����������� ������������������  and����������� ������������������  Neo4j)

Page 66: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Page 67: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.67

MATCH (me:Person {name:’Philip’})-[:FRIEND_OF]->(friends), (friends)-[:LIKES]->(restaurant), (restaurant)-[:LOCATED_IN]->(city {Location = ‘New York’}),

(restaurant)-[:SERVES]->(cuisine {cuisine = ‘Sushi’}) RETURN restaurant.name

* Cypher query language example

Page 68: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.68

Page 69: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

(Introduction����������� ������������������  to����������� ������������������  graph-based����������� ������������������  modeling)

Page 70: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

Start����������� ������������������  with����������� ������������������  a����������� ������������������  whiteboard

Page 71: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.71

Page 72: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

Adam

LOL Cat

FRIEND_OFSHARED

COMMENTED

SarahFUNNY

ON

LIKES

72

Page 73: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

Adam

LOL Cat

FRIEND_OFSHARED

COMMENTED

SarahFUNNY

ON

LIKES

73

Page 74: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.74

Page 75: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

Tom Hanks Hugo Weaving

Cloud AtlasThe Matrix

Lana Wachowski

ACTED_IN

ACTED_INACTED_IN

DIRECTED

DIRECTED75

Page 76: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

Model����������� ������������������  Incrementally

Page 77: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

Tom Hanks Hugo Weaving

Cloud AtlasThe Matrix

Lana Wachowski

ACTED_IN

ACTED_INACTED_IN

DIRECTED

DIRECTED77

Page 78: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.© All Rights Reserved 2013 | Neo Technology, Inc.© All Rights Reserved 2014 | Neo Technology, Inc.

name: Tom Hanks nationality: USA won: Oscar, Emmy

name: Hugo Weaving nationality: Australia won: MTV Movie Award

title: Cloud Atlas genre: drama, sci-fi

title: The Matrix genre: sci-fi

name: Lana Wachowski nationality: USA won: Razzie, Hugo

ACTED_IN role: Zachry

ACTED_IN role: Bill Smoke

DIRECTED

DIRECTED

ACTED_IN role: Agent Smith

78

Page 79: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

(Graph����������� ������������������  data����������� ������������������  modeling����������� ������������������  guidelines)

Page 80: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Symmetric����������� ������������������  Relationships

80

OR

Page 81: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Infer����������� ������������������  Symmetric����������� ������������������  Relationship

Find  child:MATCH (parent{name:'Sarah'})����������� ������������������   -[:PARENT_OF]->(child)����������� ������������������  RETURN child����������� ������������������  Find  parent:  MATCH (parent)-[:PARENT_OF]->����������� ������������������   (child{name:'Eric'})����������� ������������������  RETURN parent

81

Page 82: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Bi-Directional����������� ������������������  Relationships

82

Page 83: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Use����������� ������������������  Single����������� ������������������  Relationship����������� ������������������  and����������� ������������������  Ignore����������� ������������������  Relationship����������� ������������������  Direction����������� ������������������  in����������� ������������������  Queries

MATCH (p1{name:'Eric'}) -[:KNOWS]-(p2)RETURN p2

83

Page 84: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Qualified����������� ������������������  Bi-Directional����������� ������������������  Relationships

84

OR

Page 85: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Properties����������� ������������������  Versus����������� ������������������  Relationships

85

Page 86: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Use����������� ������������������  Relationships����������� ������������������  When

•You����������� ������������������  need����������� ������������������  to����������� ������������������  specify����������� ������������������  the����������� ������������������  weight,����������� ������������������  strength,����������� ������������������  or����������� ������������������  some����������� ������������������  other����������� ������������������  quality����������� ������������������  of����������� ������������������  the����������� ������������������  rela#onship  and����������� ������������������  the����������� ������������������  nodes����������� ������������������  it����������� ������������������  connects  

•The����������� ������������������  property����������� ������������������  value����������� ������������������  comprises����������� ������������������  a����������� ������������������  complex����������� ������������������  value����������� ������������������  type����������� ������������������  (e.g.����������� ������������������  address)����������� ������������������  

•Examples:����������� ������������������  

•Find����������� ������������������  all����������� ������������������  my����������� ������������������  colleagues����������� ������������������  who����������� ������������������  are����������� ������������������  level  2  or  above  in����������� ������������������  a����������� ������������������  skill  (property����������� ������������������  value)����������� ������������������  we����������� ������������������  have����������� ������������������  in����������� ������������������  common����������� ������������������  

•Find����������� ������������������  all����������� ������������������  recent����������� ������������������  orders����������� ������������������  delivered����������� ������������������  to����������� ������������������  the����������� ������������������  same����������� ������������������  delivery  address����������� ������������������  (complex����������� ������������������  value����������� ������������������  type)

86

Page 87: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Example:����������� ������������������  Find����������� ������������������  Expert����������� ������������������  Colleagues

87

Page 88: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Use����������� ������������������  Properties����������� ������������������  When

•There’s����������� ������������������  no����������� ������������������  need����������� ������������������  to����������� ������������������  qualify/weight����������� ������������������  the����������� ������������������  relationship����������� ������������������  

•The����������� ������������������  property����������� ������������������  value����������� ������������������  comprises����������� ������������������  a����������� ������������������  simple����������� ������������������  value����������� ������������������  type����������� ������������������  (e.g.����������� ������������������  color)����������� ������������������  

•Examples:����������� ������������������  

•Find����������� ������������������  those����������� ������������������  projects����������� ������������������  written����������� ������������������  by����������� ������������������  contributors����������� ������������������  to����������� ������������������  my����������� ������������������  projects����������� ������������������  that����������� ������������������  use����������� ������������������  the����������� ������������������  same����������� ������������������  language����������� ������������������  (property����������� ������������������  value)����������� ������������������  as����������� ������������������  my����������� ������������������  projects

88

Page 89: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Relationship����������� ������������������  Granularity

89

Page 90: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

General����������� ������������������  Relationships

•Qualified����������� ������������������  by����������� ������������������  property

90

Page 91: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Specific����������� ������������������  Relationships

91

Page 92: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Best����������� ������������������  of����������� ������������������  Both����������� ������������������  Worlds

92

Page 93: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

(The����������� ������������������  Neo4j����������� ������������������  GraphGist����������� ������������������  Project)

Page 94: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

The����������� ������������������  Neo4j����������� ������������������  GraphGist����������� ������������������  Project

94

GraphGists let you demonstrate a Neo4j graph proof of concept for your use cases

Page 95: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Authoring����������� ������������������  a����������� ������������������  GraphGist

•Start����������� ������������������  by����������� ������������������  articulating����������� ������������������  your����������� ������������������  problem

95

Page 96: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Authoring����������� ������������������  a����������� ������������������  GraphGist

•Introduction����������� ������������������  to����������� ������������������  Problem����������� ������������������  

!

•Banks����������� ������������������  and����������� ������������������  Insurance����������� ������������������  companies����������� ������������������  lose����������� ������������������  billions����������� ������������������  of����������� ������������������  dollars����������� ������������������  every����������� ������������������  year����������� ������������������  to����������� ������������������  fraud.����������� ������������������  Traditional����������� ������������������  methods����������� ������������������  of����������� ������������������  fraud����������� ������������������  detection����������� ������������������  play����������� ������������������  an����������� ������������������  important����������� ������������������  role����������� ������������������  in����������� ������������������  minimizing����������� ������������������  these����������� ������������������  losses.����������� ������������������  However����������� ������������������  increasingly����������� ������������������  sophisticated����������� ������������������  fraudsters����������� ������������������  have����������� ������������������  developed����������� ������������������  a����������� ������������������  variety����������� ������������������  of����������� ������������������  ways����������� ������������������  to����������� ������������������  elude����������� ������������������  discovery,����������� ������������������  both����������� ������������������  by����������� ������������������  working����������� ������������������  together,����������� ������������������  and����������� ������������������  by����������� ������������������  leveraging����������� ������������������  various����������� ������������������  other����������� ������������������  means����������� ������������������  of����������� ������������������  constructing����������� ������������������  false����������� ������������������  identities.

96

Page 97: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Authoring����������� ������������������  a����������� ������������������  GraphGist

•Explanation����������� ������������������  of����������� ������������������  Solution����������� ������������������  

!

•Graph����������� ������������������  databases����������� ������������������  offer����������� ������������������  new����������� ������������������  methods����������� ������������������  of����������� ������������������  uncovering����������� ������������������  fraud����������� ������������������  rings����������� ������������������  and����������� ������������������  other����������� ������������������  sophisticated����������� ������������������  scams����������� ������������������  with����������� ������������������  a����������� ������������������  high-level����������� ������������������  of����������� ������������������  accuracy,����������� ������������������  and����������� ������������������  are����������� ������������������  capable����������� ������������������  of����������� ������������������  stopping����������� ������������������  advanced����������� ������������������  fraud����������� ������������������  scenarios����������� ������������������  in����������� ������������������  real-time.

97

Page 98: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Authoring����������� ������������������  a����������� ������������������  GraphGist

•Design����������� ������������������  a����������� ������������������  graph����������� ������������������  model����������� ������������������  ����������� ������������������  

•Describe����������� ������������������  the����������� ������������������  entities����������� ������������������  and����������� ������������������  relationships����������� ������������������  sufficient����������� ������������������  enough����������� ������������������  to����������� ������������������  express����������� ������������������  your����������� ������������������  domain����������� ������������������  and����������� ������������������  solve����������� ������������������  your����������� ������������������  problem

98

Page 99: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Authoring����������� ������������������  a����������� ������������������  GraphGist

99

Page 100: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Authoring����������� ������������������  a����������� ������������������  GraphGist

•Translate����������� ������������������  your����������� ������������������  graph����������� ������������������  model����������� ������������������  design����������� ������������������  into����������� ������������������  Cypher

100

Page 101: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Authoring����������� ������������������  a����������� ������������������  GraphGist

101

Page 102: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Authoring����������� ������������������  a����������� ������������������  GraphGist

•Formulate����������� ������������������  your����������� ������������������  questions����������� ������������������  to����������� ������������������  solve����������� ������������������  the����������� ������������������  problem

•Which����������� ������������������  account����������� ������������������  holders����������� ������������������  share����������� ������������������  more����������� ������������������  than����������� ������������������  one����������� ������������������  piece����������� ������������������  of����������� ������������������  legitimate����������� ������������������  contact����������� ������������������  information?����������� ������������������  

•What’s����������� ������������������  the����������� ������������������  financial����������� ������������������  risk����������� ������������������  of����������� ������������������  a����������� ������������������  possible����������� ������������������  fraud����������� ������������������  ring?

102

Page 103: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Authoring����������� ������������������  a����������� ������������������  GraphGist

•Translate����������� ������������������  your����������� ������������������  questions����������� ������������������  into����������� ������������������  Cypher����������� ������������������  queries����������� ������������������  on����������� ������������������  your����������� ������������������  graph����������� ������������������  data����������� ������������������  model

103

Page 104: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Authoring����������� ������������������  a����������� ������������������  GraphGist

•Which����������� ������������������  account����������� ������������������  holders����������� ������������������  share����������� ������������������  more����������� ������������������  than����������� ������������������  one����������� ������������������  piece����������� ������������������  of����������� ������������������  legitimate����������� ������������������  contact����������� ������������������  information?

104

Page 105: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Authoring����������� ������������������  a����������� ������������������  GraphGist

•Summary����������� ������������������  

•Introduction����������� ������������������  to����������� ������������������  the����������� ������������������  problem����������� ������������������  

•Explanation����������� ������������������  of����������� ������������������  the����������� ������������������  solution����������� ������������������  

•Design����������� ������������������  a����������� ������������������  graph����������� ������������������  data����������� ������������������  model����������� ������������������  

•Translate����������� ������������������  your����������� ������������������  graph����������� ������������������  data����������� ������������������  model����������� ������������������  into����������� ������������������  a����������� ������������������  Cypher����������� ������������������  dataset����������� ������������������  

•Formulate����������� ������������������  your����������� ������������������  questions����������� ������������������  to����������� ������������������  solve����������� ������������������  the����������� ������������������  problem����������� ������������������  

•Translate����������� ������������������  your����������� ������������������  questions����������� ������������������  into����������� ������������������  Cypher����������� ������������������  queries����������� ������������������  on����������� ������������������  your����������� ������������������  graph����������� ������������������  data����������� ������������������  model����������� ������������������  

•Iterate����������� ������������������  design����������� ������������������  until����������� ������������������  the����������� ������������������  graph����������� ������������������  data����������� ������������������  model����������� ������������������  answers����������� ������������������  the����������� ������������������  questions

105

Page 106: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Authoring����������� ������������������  a����������� ������������������  GraphGist

•Get����������� ������������������  started����������� ������������������  at����������� ������������������  http://gist.neo4j.org

106

Page 107: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

(Thank����������� ������������������  You)

Page 108: Neo4j Graph Data Modeling

© All Rights Reserved 2014 | Neo Technology, Inc.

Get����������� ������������������  in����������� ������������������  touch

Twitter: @kennybastani

LinkedIn: /in/kennybastani

Email: [email protected]