MODELING WITH GRAPHS Alistair Jones Neo Technology What is modeling? - - PowerPoint PPT Presentation

modeling with graphs
SMART_READER_LITE
LIVE PREVIEW

MODELING WITH GRAPHS Alistair Jones Neo Technology What is modeling? - - PowerPoint PPT Presentation

MODELING WITH GRAPHS Alistair Jones Neo Technology What is modeling? Model Complexity Simple Complex Key-Value Document Column Graph Relational This talk compares Relational Whats a graph? Social ( network


slide-1
SLIDE 1

MODELING WITH GRAPHS

Alistair Jones Neo Technology

slide-2
SLIDE 2

What is modeling?

slide-3
SLIDE 3

Relational Key-Value Document Column Graph Model Complexity Complex Simple

slide-4
SLIDE 4

This talk compares

Relational

slide-5
SLIDE 5

What’s a graph?

slide-6
SLIDE 6

Social ( network | graph )

slide-7
SLIDE 7

What language do they speak here?

Language ¡ Country ¡

slide-8
SLIDE 8

What language do they speak here?

Language ¡ Country ¡

slide-9
SLIDE 9

What language do they speak here?

Language ¡ Country ¡

slide-10
SLIDE 10

Tables

language_code ¡ language_name ¡ word_count ¡

Language ¡

country_code ¡ country_name ¡ flag_uri ¡

Country ¡

slide-11
SLIDE 11

Need to model the relationship

language_code ¡ language_name ¡ word_count ¡

Language ¡

country_code ¡ country_name ¡ flag_uri ¡ language_code ¡

Country ¡

slide-12
SLIDE 12

What if the cardinality changes?

language_code ¡ language_name ¡ word_count ¡ country_code ¡

Language ¡

country_code ¡ country_name ¡ flag_uri ¡ ¡

Country ¡

slide-13
SLIDE 13

Or we go many-to-many?

language_code ¡ language_name ¡ word_count ¡

Language ¡

country_code ¡ country_name ¡ flag_uri ¡ ¡

Country ¡

language_code ¡ country_code ¡

LanguageCountry ¡

slide-14
SLIDE 14

Or we want to qualify the relationship?

language_code ¡ language_name ¡ word_count ¡

Language ¡

country_code ¡ country_name ¡ flag_uri ¡ ¡

Country ¡

language_code ¡ country_code ¡ primary ¡

LanguageCountry ¡

slide-15
SLIDE 15

Explicit Relationship

name ¡ word_count ¡

Language ¡

name ¡ flag_uri ¡ ¡

Country ¡ IS_SPOKEN_IN ¡

slide-16
SLIDE 16 Argentina Argentina Argentina Spanish Spanish Spanish Australia Australia Australia English English English Austria Austria Austria German German German Belgium Belgium Belgium Dutch Dutch Dutch French French French Brazil Brazil Brazil Portuguese Portuguese Portuguese Canada Canada Canada China China China Simplified Chinese Simplified Chinese Simplified Chinese Mandarin Mandarin Mandarin Czech Czech Czech Czech Czech Czech Denmark Denmark Denmark Danish Danish Danish Egypt Egypt Egypt Arabic Arabic Arabic Finland Finland Finland Finnish Finnish Finnish France France France Germany Germany Germany Iceland Iceland Iceland Icelandic Icelandic Icelandic India India India Hindi Hindi Hindi Iraq Iraq Iraq Ireland Ireland Ireland Irish Irish Irish Israel Israel Israel Hebrew Hebrew Hebrew Italy Italy Italy Italian Italian Italian Japan Japan Japan Japanese Japanese Japanese Pakistan Pakistan Pakistan Urdu Urdu Urdu Romania Romania Romania Romanian Romanian Romanian South Africa South Africa South Africa Afrikaans Afrikaans Afrikaans Spain Spain Spain Catalan Catalan Catalan Sri Lanka Sri Lanka Sri Lanka Sinhala Sinhala Sinhala Tamil Tamil Tamil Switzerland Switzerland Switzerland Romansh Romansh Romansh Thailan Thailan Thailan Thai Thai Thai Turkey Turkey Turkey Turkish Turkish Turkish United Kingdom United Kingdom United Kingdom Welsh Welsh Welsh
slide-17
SLIDE 17
slide-18
SLIDE 18

Relationship Properties

name ¡ word_count ¡

Language ¡

name ¡ flag_uri ¡ ¡

Country ¡ IS_SPOKEN_IN ¡

as_primary ¡

slide-19
SLIDE 19

What’s different?

language_code ¡ language_name ¡ word_count ¡

Language ¡

country_code ¡ country_name ¡ flag_uri ¡ ¡

Country ¡

language_code ¡ country_code ¡ primary ¡

LanguageCountry ¡ IS_SPOKEN_IN ¡

slide-20
SLIDE 20

What’s different?

  • Implementation of maintaining

relationships is left up to the database

  • Surrogate keys disappear or are

unnecessary (with the exception of node id)

  • Relationships get an explicit name
slide-21
SLIDE 21

Relationship specialisation

name ¡ word_count ¡

Language ¡

name ¡ flag_uri ¡ ¡

Country ¡ IS_SPOKEN_IN ¡

as_primary ¡

slide-22
SLIDE 22

Weighted relationships

name ¡ word_count ¡

Language ¡

name ¡ flag_uri ¡ ¡

Country ¡ POPULATION_SPEAKS ¡

populaAon_fracAon ¡

slide-23
SLIDE 23

Bidirectional relationships

name ¡ word_count ¡

Language ¡

name ¡ flag_uri ¡ ¡

Country ¡ IS_SPOKEN_IN ¡ PRIMARY_LANGUAGE ¡

slide-24
SLIDE 24

Keep on adding relationships

name ¡ word_count ¡

Language ¡

name ¡ flag_uri ¡ ¡

Country ¡ POPULATION_SPEAKS ¡

populaAon_fracAon ¡

SIMILAR_TO ¡ ADJACENT_TO ¡

slide-25
SLIDE 25

1 ¡ 2 ¡ 3 ¡

Graph Modeling Principles

slide-26
SLIDE 26

Embrace the Paradigm 2 ¡ 3 ¡

Graph Modeling Principles

slide-27
SLIDE 27

Use the building blocks

  • Nodes
  • Relationships
  • Properties

name: ¡value ¡

RELATIONSHIP_NAME ¡

slide-28
SLIDE 28

Anti-pattern: rich properties

name: ¡“Canada” ¡ languages_spoken: ¡“[ ¡‘English’, ¡‘French’ ¡]” ¡ ¡ ¡

slide-29
SLIDE 29

Embrace the Paradigm

3 ¡

Graph Modeling Principles

Nodes for Identity

slide-30
SLIDE 30

Node represents multiple concepts

name ¡ flag_uri ¡ language_name ¡ number_of_words ¡ yes_in_langauge ¡ no_in_language ¡ currency_code ¡ currency_name ¡

Country ¡

slide-31
SLIDE 31

USES_CURRENCY ¡

Break out separate concepts

name ¡ flag_uri ¡ currency_code ¡ currency_name ¡

Country ¡

name ¡ number_of_words ¡ yes ¡ no ¡ ¡

Country ¡ SPEAKS ¡ Currency ¡

currency_code ¡ currency_name ¡

slide-32
SLIDE 32

Property represents entity

name: ¡“Alistair” ¡

Employee ¡

name: ¡“Neo ¡Tech” ¡ ¡

Workplace ¡ WORKS_AT ¡

role: ¡“soXware ¡engineer” ¡

slide-33
SLIDE 33

EMPLOYEE ¡ EMPLOYED_IN ¡

( Reify | Nodify ) connecting entities

name: ¡“Alistair” ¡

Employee ¡

name: ¡“Neo ¡Tech” ¡ ¡

Workplace ¡ Employment ¡

start_date: ¡Aug-­‑2011 ¡

Role ¡

name: ¡“soXware ¡ engineer” ¡

ROLE ¡

slide-34
SLIDE 34

Embrace the Paradigm

Graph Modeling Principles

Nodes for Identity Relation- ships for Access

slide-35
SLIDE 35 Argentina Argentina Argentina Spanish Spanish Spanish Australia Australia Australia English English English Austria Austria Austria German German German Belgium Belgium Belgium Dutch Dutch Dutch French French French Brazil Brazil Brazil Portuguese Portuguese Portuguese Canada Canada Canada China China China Simplified Chinese Simplified Chinese Simplified Chinese Mandarin Mandarin Mandarin Czech Czech Czech Czech Czech Czech Denmark Denmark Denmark Danish Danish Danish Egypt Egypt Egypt Arabic Arabic Arabic Finland Finland Finland Finnish Finnish Finnish France France France Germany Germany Germany Iceland Iceland Iceland Icelandic Icelandic Icelandic India India India Hindi Hindi Hindi Iraq Iraq Iraq Ireland Ireland Ireland Irish Irish Irish Israel Israel Israel Hebrew Hebrew Hebrew Italy Italy Italy Italian Italian Italian Japan Japan Japan Japanese Japanese Japanese Pakistan Pakistan Pakistan Urdu Urdu Urdu Romania Romania Romania Romanian Romanian Romanian South Africa South Africa South Africa Afrikaans Afrikaans Afrikaans Spain Spain Spain Catalan Catalan Catalan Sri Lanka Sri Lanka Sri Lanka Sinhala Sinhala Sinhala Tamil Tamil Tamil Switzerland Switzerland Switzerland Romansh Romansh Romansh Thailan Thailan Thailan Thai Thai Thai Turkey Turkey Turkey Turkish Turkish Turkish United Kingdom United Kingdom United Kingdom Welsh Welsh Welsh
slide-36
SLIDE 36

Relationships for querying

  • Relationships should the primary means

to access nodes in the database

  • Traversing relationships is cheap – that’s

the whole design goal of a graph database

  • Use indexes only to find starting nodes for

a query

slide-37
SLIDE 37

Anti-pattern: unconnected graph

name: ¡“Jones” ¡ name: ¡“Jones” ¡ name: ¡“Jones” ¡ name: ¡“Jones” ¡ name: ¡“Jones” ¡ name: ¡“Jones” ¡ name: ¡“Jones” ¡ name: ¡“Jones” ¡ name: ¡“Jones” ¡ name: ¡“Jones” ¡ name: ¡“Jones” ¡

slide-38
SLIDE 38

Challenge: Property or Relationship?

  • Can every property be replaced by a

relationship?

  • Should every entities with the same

property values be connected?

slide-39
SLIDE 39

Object Mapping

  • Similar to how you would map objects to

a relational database, using an ORM such as Hibernate

  • Generally simpler and easier to reason

about

  • Examples

– Java: Spring Data Graph – Ruby: Active Model

slide-40
SLIDE 40

Aside: why map?

  • Do you use mapping because you are

scared of SQL?

  • Following DDD, could you write your

repositories directly against the graph API?

slide-41
SLIDE 41

Embrace the Paradigm

Graph Modeling Principles

Nodes for Identity Relation- ships for Access

slide-42
SLIDE 42

Alistair ¡Jones ¡ alistair.jones@neotechnology.com ¡ @apcj ¡