Modelling Data in Neo4j, bidirectional relationships, qualifying relationships with properties vs. relationship types (performance comparison), Neo4j hardware sizing, Cypher vs. Java API
25. Property Graph
name: "Triller"
type: "genre"
name: "Drama"
type: "genre"
IS
_
OF
_G
EN
RE
ED
CT
ipsum
IS_A
IS_
A
_
IS
name: "Pulp Fiction"
year: 1994
type: "movie"
name: "Director"
type: "occupation"
A
name: "Actor"
type: "occupation"
IS_OF_GENRE
RE
DI
A
ED
CT
le:
ro
name: "Samuel L. Jackson"
type: "person"
name: "Quentin Tarantino"
type: "person"
_IN
imm
"J
Dim
ie
ck"
mi
N
D_I
ld"
TE
AC
nfie
Win
les
"J u
:
ole
r
GraphAware
TM
26. Traversal
name: "Triller"
type: "genre"
name: "Drama"
type: "genre"
IS
_
OF
_G
EN
RE
IS_
IS_A
ED
CT
A
_
IS
name: "Pulp Fiction"
year: 1994
type: "movie"
name: "Director"
type: "occupation"
A
name: "Actor"
type: "occupation"
IS_OF_GENRE
E
IR
D
A
ED
CT
le:
ro
name: "Samuel L. Jackson"
type: "person"
_IN
imm
"J
name: "Quentin Tarantino"
type: "person"
Dim
ie
"
ick
m
_IN
D
A
:
ole
r
E
CT
les
"J u
"
eld
fi
inn
W
GraphAware
TM
27. Modeling Data as Graphs
There is no single correct way.
GraphAware
TM
28. One Way
name: "Triller"
type: "genre"
name: "Drama"
type: "genre"
IS
_
OF
_G
EN
RE
ED
CT
ipsum
IS_A
IS_
A
_
IS
name: "Pulp Fiction"
year: 1994
type: "movie"
name: "Director"
type: "occupation"
A
name: "Actor"
type: "occupation"
IS_OF_GENRE
RE
DI
A
ED
CT
le:
ro
name: "Samuel L. Jackson"
type: "person"
name: "Quentin Tarantino"
type: "person"
_IN
imm
"J
Dim
ie
ck"
mi
N
D_I
ld"
TE
AC
nfie
Win
les
"J u
:
ole
r
GraphAware
TM
29. name: "Pulp Fiction"
year: 1994
type: "movie"
genres: "Drama", "Thriller"
TE
R
AC
AR
CH
CT
ED
DI
RE
name: "Quentin Tarantino"
type: "person"
occupation: "Actor", "Director"
_IN
TER
ACTED_AS
RAC
name: "Jimmie Dimmick"
type: "role"
CHA
_IN
Another Way
name: "Jules Winnfield"
type: "role"
ACTED_AS
name: "Samuel L. Jackson"
type: "person"
occupation: "Actor"
GraphAware
TM
37. Node Record in the Node Store (9 bytes), first bit = inUse flag
next
relationship
(35 bits)
next
property
(36 bits)
Relationship Record in the Relationship Store (33 bytes), first bit = inUse flag, second bit unused
first node's
first node's
second
second
type
previous
next
node's first
node's next next property
first node
second node
(16
relationship relationship relationship relationship
(36 bits)
(35 bits)
(35 bits)
bits)
(35 bits)
(35 bits)
(35 bits)
(35 bits)
Neo4j Data Layout
GraphAware
TM
49. Who liked Pulp Fiction? (Cypher)
START
pulpFiction=node({id})
MATCH
(pulpFiction)<-‐[r:RATED]-‐(fan)
WHERE
r.rating
>
3
RETURN
fan
GraphAware
TM
50. Who liked Pulp Fiction? (Java)
for
(Relationship
r
:
pulpFiction.getRelationships(INCOMING,
RATED))
{
if
((int)
r.getProperty("rating")
>
3)
{
Node
fan
=
r.getStartNode();
//do
something
with
it
}
}
GraphAware
TM
52. Who liked Pulp Fiction? (Cypher)
START
pulpFiction=node({id})
MATCH
(pulpFiction)<-‐[r:LIKED|LOVED]-‐(fan)
RETURN
fan
GraphAware
TM
53. Who liked Pulp Fiction? (Java)
for
(Relationship
r
:
pF.getRelationships(INCOMING,
LIKED,
LOVED))
{
Node
fan
=
r.getStartNode();
//do
something
with
it
}
GraphAware
TM
71. START
from=node:node_auto_index(user_id="{FROM}"),
to=node:node_auto_index(user_id="{TO}")
MATCH
p
=
from-‐[r*1..5]-‐>to
RETURN
extract(n
in
nodes(p)
:
n.user_id),
extract(rel
in
relationships(p)
:
rel.weight),
extract(rel
in
relationships(p)
:
type(rel))
ORDER
BY
length(p),
reduce(totalWeight
=
0,
rel
in
relationships(p)
:
totalWeight
+
rel.weight)
LIMIT
3
GraphAware
TM
72. START
from=node:node_auto_index(user_id="{FROM}"),
to=node:node_auto_index(user_id="{TO}")
MATCH
p
=
from-‐[r*1..5]-‐>to
RETURN
extract(n
in
nodes(p)
:
n.user_id),
extract(rel
in
relationships(p)
:
rel.weight),
extract(rel
in
relationships(p)
:
type(rel))
ORDER
BY
length(p),
reduce(totalWeight
=
0,
rel
in
relationships(p)
:
totalWeight
+
rel.weight)
LIMIT
3
> 1 second
GraphAware
TM