English edition of my introduction to Neo4j and Spring Data talk - updated in February 2015 for the latest changes in Neo4j and Spring Data. The talk gives an introduction to graph databases, Neo4j and its query language Cypher. In addition, the talk covers Spring Data Neo4j.
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Introduction to graph databases, Neo4j and Spring Data - English 2015 Edition
1. Introduc)on
to
graph
databases,
Neo4j
and
Spring
Data
Aleksander
M.
Stensby
Monokkel
A/S
2. • Aleksander
M.
Stensby
• CEO
in
Monokkel
AS
• Previously
COO
in
Integrasco
AS
• Working
with
search
and
data
analysis
since
2004
www.monokkel.io
3. • Daglig
leder
i
Monokkel
AS
• Tidligere
COO
i
Integrasco
AS
• Persistering,
Prosessering
og
Presentasjon
av
data
Persistence
–
Processing
–
PresentaHon
4. Agenda
• Intro
to
graph
databases
and
modelling
• Neo4j
and
Cypher
• SpringData
Neo4j
21. Person
ID
Name
Friends
PersonID
FriendID
«Aleks»’s
friends
SELECT
p1.Person
FROM
Person
p1
JOIN
Friends
ON
Friends.FriendID
=
p1.ID
JOIN
Person
p2
ON
Friends.PersonID
=
p2.ID
WHERE
p2.Name=
'Aleks'
22. Person
ID
Name
Friends
PersonID
FriendID
Friends
with
«Aleks»
SELECT
p1.Person
FROM
Person
p1
JOIN
Friends
ON
Friends.PersonID
=
p1.ID
JOIN
Person
p2
ON
Friends.FriendID
=
p2.ID
WHERE
p2.Name
=
'Aleks'
23. Person
ID
Name
Friends
PersonID
FriendID
«Aleks»’s
friends’
friends?
SELECT
p1.Person
AS
PERSON,
p2.Person
AS
FRIEND_OF_FRIEND
FROM
Friends
v1
JOIN
Person
p1
ON
v1.PersonID
=
p1.ID
JOIN
Friends
v2
ON
v2.FriendID
=
v1.FriendID
JOIN
Person
p2
ON
v2.FriendID
=
p2.
ID
WHERE
p1.Name
=
'Aleks‘
AND
v2.FriendID
<>
p1.ID
26. A
graph
database...
• uses
graph
structures
with
– Nodes
– RelaHonships
– Apributes
• to
store
informaHon!
• Exellent
for
relaHonships
–
but
not
necessarily
best
at
aggregaHng
data
27. Graph
databases
and
neo4j
• Visual
–
Schema
less!
• Doubly-‐linked-‐lists
– each
node
has
a
list
of
incoming
and
outgoing
relaWonships
• Direct
lookup
=
O(1)
30. Nodes
have
aributes
ALEKS
TARJEI
Age:
29
First
Name:
Aleksander
Last
Name:
Stensby
Age:
30
First
Name:
Tarjei
Last
Name:
Romtveit
FRIENDS_WITH
31. RelaWonships
can
also
have
aributes
ALEKS
TARJEI
Age:
29
First
Name:
Aleksander
Last
Name:
Stensby
Age:
30
First
Name:
Tarjei
Last
Name:
Romtveit
FRIENDS_WITH
Since:
01.01.2004
32. RelaWonships
can
be
bi-‐direcHonal
ALEKS
TARJEI
Age:
29
First
Name:
Aleksander
Last
Name:
Stensby
Age:
30
First
Name:
Tarjei
Last
Name:
Romtveit
FRIENDS_WITH
Since:
01.01.2004
FRIENDS_WITH
Since:
01.01.2004
33. ALEKS
TARJEI
Age:
29
First
Name:
Aleksander
Last
Name:
Stensby
Age:
30
First
Name:
Tarjei
Last
Name:
Romtveit
FRIENDS_WITH
Since:
01.01.2004
34. ALEKS
TARJEI
Age:
29
First
Name:
Aleksander
Last
Name:
Stensby
Type:
Person
Age:
30
First
Name:
Tarjei
Last
Name:
Romtveit
Type:
Person
FRIENDS_WITH
Since:
01.01.2004
different
types
of
nodes
35. Person
Person
Sport
Programming
language
Drink
Person
36. Some
use
cases
• Social
Media
• RecommendaHons
• Geo
rouHng
and
logisHcs
• Access
management
• Network
management
• Finance
/
Fraud
38. Neo4j
2.2.x
• Currently
early
access
milestone
4
• New
page
cache
-‐>
high
concurrency
• TransacHonal
and
batch
write
performance
• Cost-‐Based
OpHmizer
for
Cypher
53. START
a=node(*)
MATCH
(a:Person
{name:
'Danny
DeVito'}
)
WHERE
a.name='Danny
DeVito'
RETURN
a
Neo4j
2.0.1
54. Friend
of
friend...
MATCH
(aleks)-‐[r:KNOWS]-‐()-‐[r2:KNOWS]
-‐>(friend_of_friend)
WHERE
aleks.firstName=
'Aleks'
RETURN
friend_of_friend.firstName
55. Friend
of
friend...
MATCH
(aleks)-‐[:KNOWS*2..2]-‐>(friend_of_friend)
WHERE
aleks.firstName=
'Aleks'
AND
NOT
(aleks)-‐[:KNOWS]-‐>(friend_of_friend)
RETURN
friend_of_friend.firstName
62. Intro
to
SpringData
Neo4j
• SpringData
• Repositories
• Quick
example
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<arHfactId>spring-‐data-‐neo4j</arHfactId>
<version>3.2.2.RELEASE</version>
</dependency>
</dependencies>
neo4j
2.1.7
spring-‐data-‐neo4j
3.2.2
spring
4.1.4
63. SpringData
Neo4j
Config
@Configuration
@EnableNeo4jRepositories("no.stensby.javabin.neo.repositories")
public
class
Config
extends
Neo4jConfiguration
{
}
@Autowired
protected
Neo4jTemplate
template;
64. SpringData
Neo4j
Config
@Configuration
@EnableNeo4jRepositories("no.stensby.javabin.neo.repositories")
public
class
Config
extends
Neo4jConfiguration
{
public
Config
(){
setBasePackage("no.stensby.javabin.neo.domain");
}
}
@Autowired
protected
Neo4jTemplate
template;
65. SpringData
Neo4j
Config
@Bean
GraphDatabaseService
graphDatabaseService()
{
return
new
GraphDatabaseFactory().newEmbeddedDatabase("tmp/neo4j");
}
Embedded:
REST:
@Bean
GraphDatabaseService
graphDatabaseService()
{
return
new
SpringRestGraphDatabase("http://localhost:7474/db/data");
}
@Bean(destroyMethod
=
"shutdown")
@Scope(SCOPE_PROTOTYPE)
public
GraphDatabaseService
graphDatabaseService()
{
return
new
TestGraphDatabaseFactory().newImpermanentDatabase();
}
TEST:
66. SpringData
Neo4j
-‐
Domain
@NodeEntity
public
class
Person
{
@GraphId
public
Long
nodeId;
@Indexed(unique
=
true)
public
int
id;
public
String
firstName;
public
String
lastName;
}
70. m-‐m
relaHonships
-‐
@RelatedToVia
@Fetch
@RelatedToVia
(type
=
"ACTS_IN")
Set<Role>
roles
=
new
HashSet<Role>();
@Fetch
@RelatedToVia(type
=
"ACTS_IN",
direction
=
Direction.INCOMING)
Set<Role>
cast
=
new
HashSet<Role>();
Actor
Movie
@RelationshipEntity(type
=
"ACTS_IN")
public
class
Role
{
@GraphId
Long
nodeId;
@StartNode
Actor
actor;
@EndNode
Movie
movie;
Role
76. Lessons
Learned
• Be
careful
with
versions
and
upgrading
…
– Neo
1.9.x
-‐>
Neo
2.x
=
lots
of
problems…
– Lots
of
breaking
changes…
– Spring
Data
Neo4j
!=
Neo4j
• Nodes
are
“first-‐class”
ciHzens
in
the
graph
– Hyperedges
are
not
supported
-‐>
use
“event
nodes”
• AggregaHons
are
expensive
–
caching
stats
on
nodes!
• Unique
and
expressive
relaHonship
types
77. Pi•alls
• Don’t
use
the
graphid
as
your
ID
• IniHalize
Set
• Use
@Fetch
with
care
• Use
standard
gepers/sepers
• Be
careful
with
String
escaping
78. InspiraHon
• Single
Malt
Scotch
Whisky
hpp://gist.neo4j.org/?8139605
• Chess
Games
and
PosiWons
hpp://gist.neo4j.org/?6506717
• Movie
RecommendaWons
hpp://gist.neo4j.org/?8173017
• Food
Recipes
RecommendaWon
hpp://gist.neo4j.org/?8731452