Ian closely looks at design and implementation strategies you can employ when building a Neo4j-based graph database solution, including architectural choices, data modelling, and testing.g
Ähnlich wie Designing and Building a Graph Database Application – Architectural Choices, Data Modeling, and Testing - Ian Robinson @ GraphConnect NY 2013
Efficient Rails Test Driven Development (class 3) by Wolfram ArnoldMarakana Inc.
Ähnlich wie Designing and Building a Graph Database Application – Architectural Choices, Data Modeling, and Testing - Ian Robinson @ GraphConnect NY 2013 (20)
5. Models
Purposeful
abstrac5on
of
a
domain
designed
to
sa5sfy
par5cular
applica5on/end-‐user
goals
Images:
en.wikipedia.org
6. Example
Applica5on
• Knowledge
management
– People,
companies,
skills
– Cross
organiza5onal
• Find
my
professional
social
network
– Exchange
knowledge
– Interest
groups
– Help
– Staff
projects
7. Applica5on/End-‐User
Goals
As
an
emp
loyee
I
want
to
k
now
who
i
n
the
comp
has
similar
any
skills
to
m
e
So
that
we
can
excha
nge
knowl
edge
8. Ques5ons
To
Ask
of
the
Domain
As
an
emp
loyee
I
want
to
k
now
who
i
n
the
co
has
similar
skills
to
me mpany
So
that
we
can
excha
nge
knowle
dge
Which
people,
who
work
for
the
same
company
as
me,
have
similar
skills
to
me?
9. Iden5fy
En55es
Which
people,
who
work
for
the
same
company
as
me,
have
similar
skills
to
me?
Person
Company
Skill
10. Iden5fy
Rela5onships
Between
En55es
Which
people,
who
work
for
the
same
company
as
me,
have
similar
skills
to
me?
Person
WORKS_FOR
Company
Person
HAS_SKILL
Skill
11. Convert
to
Cypher
Paths
Rela5onship
Person
WORKS_FOR
Company
Person
HAS_SKILL
Skill
Label
(:Person)-[:WORKS_FOR]->(:Company),
(:Person)-[:HAS_SKILL]->(:Skill)
14. Express
Ques5on
as
Graph
Pa9ern
Which
people,
who
work
for
the
same
company
as
me,
have
similar
skills
to
me?
15. Cypher
Query
Which
people,
who
work
for
the
same
company
as
me,
have
similar
skills
to
me?
MATCH (company)<-[:WORKS_FOR]-(me:Person)-[:HAS_SKILL]->(skill),
(company)<-[:WORKS_FOR]-(colleague)-[:HAS_SKILL]->(skill)
WHERE me.name = {name}
RETURN colleague.name AS name,
count(skill) AS score,
collect(skill.name) AS skills
ORDER BY score DESC
16. Graph
Pa9ern
Which
people,
who
work
for
the
same
company
as
me,
have
similar
skills
to
me?
MATCH (company)<-[:WORKS_FOR]-(me:Person)-[:HAS_SKILL]->(skill),
(company)<-[:WORKS_FOR]-(colleague)-[:HAS_SKILL]->(skill)
WHERE me.name = {name}
RETURN colleague.name AS name,
count(skill) AS score,
collect(skill.name) AS skills
ORDER BY score DESC
17. Anchor
Pa9ern
in
Graph
Which
people,
who
work
for
the
same
company
as
me,
have
similar
skills
to
me?
MATCH (company)<-[:WORKS_FOR]-(me:Person)-[:HAS_SKILL]->(skill),
(company)<-[:WORKS_FOR]-(colleague)-[:HAS_SKILL]->(skill)
WHERE me.name = {name}
RETURN colleague.name AS name,
count(skill) AS score,
collect(skill.name) AS skills
ORDER BY score DESC
Search
nodes
labeled
‘Person’,
matching
on
‘name’
property
18. Create
Projec5on
of
Results
Which
people,
who
work
for
the
same
company
as
me,
have
similar
skills
to
me?
MATCH (company)<-[:WORKS_FOR]-(me:Person)-[:HAS_SKILL]->(skill),
(company)<-[:WORKS_FOR]-(colleague)-[:HAS_SKILL]->(skill)
WHERE me.name = {name}
RETURN colleague.name AS name,
count(skill) AS score,
collect(skill.name) AS skills
ORDER BY score DESC
23. From
User
Story
to
Model
and
Query
As
an
emp
loyee
I
want
to
k
now
who
i
n
the
co
has
similar
skills
to
me mpany
So
that
we
can
excha
nge
knowle
dge
MATCH (company)<-[:WORKS_FOR]-(me:Person)-[:HAS_SKILL]->(skill),
(company)<-[:WORKS_FOR]-(colleague)-[:HAS_SKILL]->(skill)
WHERE me.name = {name}
RETURN colleague.name AS name,
count(skill) AS score,
collect(skill.name) AS skills
ORDER BY score DESC
?
Which
people,
who
work
for
the
same
company
as
me,
have
similar
skills
to
me?
Person
WORKS_FOR
Company
Person
HAS_SKILL
Skill
(:Company)<-[:WORKS_FOR]-(:Person)-[:HAS_SKILL]->(:Skill)
25. Why
Test?
• Ensure
model
is
fit
for
queries
– Rapid
feedback
• Ensure
correctness
of
queries
• Document
your
understanding
of
your
domain
– Including
corner
cases
and
excep5ons
• Provide
a
regression
test
suite
– Allows
you
to
change
and
evolve
model
and
queries
26. Method
• Develop
queries,
or
classes
that
encapsulate
queries,
using
unit
tests
• Use
small,
well-‐understood
datasets
in
each
test
– Create
data
in
test
setup
– Test
dataset
expresses
your
understanding
of
(part
of)
the
domain
• Inject
in-‐memory
graph
database
(or
Cypher
engine)
into
object
under
test
• The
exact
strategy
you
use
depends
on
your
applica5on
architecture…
28. Applica5on
Architectures
• Embedded
– Host
in
Java
process
– Access
to
Java
APIs
• Server
• Server
with
Extensions
Applica5on
Java
APIs
29. Applica5on
Architectures
• Embedded
• Server
– HTTP/JSON
interface
– Server
wraps
embedded
instance
• Server
with
Extensions
Applica5on
REST
Client
Write
LB
REST
API
REST
API
Read
LB
REST
API
30. Applica5on
Architectures
• Embedded
• Server
• Server
with
Extensions
REST
API
– Execute
complex
logic
on
server
– Control
HTTP
request/response
format
Extensions
31. Embedded
Example
• Company
social
network
• Find
colleagues
with
similar
skills
• Encapsulate
query
in
a
ColleagueFinder
32. Unit
Test
Fixture
public class ColleagueFinderTest {
private GraphDatabaseService db;
private ColleagueFinder finder;
@Before
public void init() {
db = new TestGraphDatabaseFactory().newImpermanentDatabase();
ExampleGraph.populate( db );
finder = new ColleagueFinder( new ExecutionEngine( db ) );
}
@After
public void shutdown() {
db.shutdown();
}
}
33. Create
Database
public class ColleagueFinderTest {
private GraphDatabaseService db;
private ColleagueFinder finder;
@Before
public void init() {
db = new TestGraphDatabaseFactory().newImpermanentDatabase();
ExampleGraph.populate( db );
finder = new ColleagueFinder( new ExecutionEngine( db ) );
}
@After
public void shutdown() {
db.shutdown();
}
}
34. Populate
Graph
public class ColleagueFinderTest {
private GraphDatabaseService db;
private ColleagueFinder finder;
@Before
public void init() {
db = new TestGraphDatabaseFactory().newImpermanentDatabase();
ExampleGraph.populate( db );
finder = new ColleagueFinder( new ExecutionEngine( db ) );
}
@After
public void shutdown() {
db.shutdown();
}
}
35. Create
Object
Under
Test
public class ColleagueFinderTest {
private GraphDatabaseService db;
private ColleagueFinder finder;
@Before
public void init() {
db = new TestGraphDatabaseFactory().newImpermanentDatabase();
ExampleGraph.populate( db );
finder = new ColleagueFinder( new ExecutionEngine( db ) );
}
@After
public void shutdown() {
db.shutdown();
}
}
Inject
Execu5onEngine
36. ImpermanentGraphDatabase
• In-‐memory
• For
tes5ng
only,
not
produc5on!
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-kernel</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
64. ts gy
en lo
i m no
pl h
m ec
Co eo T
N
of
Graph
h
Databases
Thank
you
Twi9er:
@ianSrobinson
#neo4j
Ian Robinson,
Jim Webber & Emil Eifrem
github.com/iansrobinson/neo4j-‐good-‐prac5ces