Weitere ähnliche Inhalte Ähnlich wie Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 2013 (20) Kürzlich hochgeladen (20) Kaiso: Modeling Complex Class Hierarchies with Neo4j - David Szotten @ GraphConnect London 20131. Kaiso
-‐
an
object
persistence
framework
for
Python
and
Neo4j
David
Szo>en
onefinestay
!
!
!
@szo;en
github.com/davidszo;en
1
©
Lifealike
Limited,
2013.
4. onefinestay
-‐
the
unhotel
• Guests
live
like
a
local
– but
with
services
and
ameniNes
of
a
hotel
!
• Members
with
disNncNve
homes
earn
hassle-‐free
income
!
• From
just
a
handful
of
homes
in
2010
to
over
1000
homes
in
4
ciNes
in
2013
4
©
Lifealike
Limited,
2013.
5. How
do
we
do
it?
• Provision:
Home
Unhotel
– Clean
– Pack
away
certain
items
• Make
wardrobe
space
• Deprovision:
Unhotel
Home
– Clean
– Put
everything
back
5
©
Lifealike
Limited,
2013.
6. We
care
about
our
homes.
A
lot.
• Every
home
is
different
• We
require
inNmate
knowledge
!
• For
guests:
“I
can’t
walk
up
stairs”,
“Where
is
the
iron?”
• For
members:
“Please
put
my
fragile
vase
away
before
the
guests
arrive”
• For
us:
“How
much
bed
linen
do
I
need
to
take
to
this
home
for
this
booking?”
6
©
Lifealike
Limited,
2013.
7. Previous
system
• Custom
storage
soluNon
• Dates
back
to
when
we
were
much
smaller
• Problems:
– Siloed
– Hard
to
query
denormalised
copies
7
©
Lifealike
Limited,
2013.
8. New
system
• Want
to
model
our
homes
– both
layout
and
contents
!
• Need
flexibility
– Our
needs
are
likely
to
grow
and
change
!
• Track
not
only
objects,
but
their
classes
– Ideally
treat
the
taxonomy
itself
as
data
8
©
Lifealike
Limited,
2013.
9. Enter
Neo4j
• Handle
complex
data
structures
!
• Ensure
extensibility
for
future
needs
!
• Easy
to
create
powerful
queries
!
• Makes
for
a
nice
conceptual
model
of
our
data
9
©
Lifealike
Limited,
2013.
10. Flexibility
requirements
• We
want
to
associate
informaNon
with
classes
as
well
as
objects
– This
dishwasher
is
of
make
Electropool
– (All)
appliances
need
PAT
tesNng
!
• Unlike
convenNonal
object
mappers,
the
taxonomy/class
hierarchy
needs
to
be
editable
by
users,
and
so
should
be
part
of
the
data
10
©
Lifealike
Limited,
2013.
11. Kaiso
-‐
an
object
persistence
framework
# (import some stuff)!
!
class Animal(Entity):!
id = Uuid(unique=True)!
name = String()!
friend_of = Outgoing(FriendOf)!
!
class Carnivore(Animal): pass!
class Herbivore(Animal): pass!
!
class Penguin(Herbivore):!
favourite_ice_cream = String()!
!
class Lion(Carnivore):!
n_siblings = Integer()!
11
©
Lifealike
Limited,
2013.
12. Kaiso:
Create
some
instances
# import types, connect to db!
!
manager = Manager("http://localhost:7474/db/data/")!
!
manager.save(Lion)!
manager.save(Penguin)!
!
# create some instances!
fred = Penguin(name="Fred")!
tom = Lion(name="Tom")!
!
relation = FriendOf(fred, tom)!
!
manager.save(fred)!
manager.save(tom)!
manager.save(relation)
12
©
Lifealike
Limited,
2013.
13. Instances
are
saved
in
the
graph...
FRIENDOF
[6]
Penguin:
Fred
13
[7]
Lion:
Tom
©
Lifealike
Limited,
2013.
14. ...
and
so
is
the
class
hierarchy
[1]
Type:
Animal
ISA
ISA
[4]
Type:
Herbivore
[2]
Type:
Carnivore
ISA
ISA
[3]
Type:
Lion
[5]
Type:
Penguin
INSTANCEOF
INSTANCEOF
FRIENDOF
[6]
Penguin:
Fred
14
[7]
Lion:
Tom
©
Lifealike
Limited,
2013.
15. Kaiso:
Queries
based
on
the
type
hierarchy
START!
Herbivore=node:persistabletype(id="Herbivore"),!
Carnivore=node:persistabletype(id="Carnivore")!
!
MATCH!
Carnivore <-[:ISA*]-()<-[:INSTANCEOF]-(carnivore),!
Herbivore <-[:ISA*]-()<-[:INSTANCEOF]-(herbivore),!
!
!
(herbivore)-[:FRIENDOF]->(carnivore)!
RETURN!
"The herbivore",!
herbivore.name,!
“is a friend of the carnivore",!
carnivore.name;!
!
=> +--------------------------------------------------------------------+!
=> | "The herbivore" | "Fred" | "is a friend of the carnivore" | “Tom" |
=> +--------------------------------------------------------------------+
15
©
Lifealike
Limited,
2013.