SlideShare a Scribd company logo
Introduksjon 
,l 
grafdatabaser, 
Neo4j 
og 
Spring 
Data 
Aleksander 
M. 
Stensby 
Monokkel 
A/S
• Daglig 
leder 
i 
Monokkel 
AS 
• Tidligere 
COO 
i 
Integrasco 
AS 
• Persistering, 
Prosessering 
og 
Presentasjon 
av 
data 
Persistering 
– 
Prosessering 
– 
Presentasjon
• Aleksander 
M. 
Stensby 
• Daglig 
leder 
i 
Monokkel 
AS 
• Tidligere 
COO 
i 
Integrasco 
AS 
www.monokkel.io
Agenda 
• Intro 
@l 
grafdatabaser 
og 
modellering 
• Neo4j 
og 
Cypher 
• SpringData 
Neo4j
Relasjondatabaser...
...på 
relasjoner
Relasjondatabaser 
er 
fantas@sk 
på 
aggregering 
av 
data, 
mapping 
av 
skjemaer 
og 
tabell 
data!
MEN
vi 
har 
en 
tendens 
@l 
å 
tvinge 
_alle_ 
problems@llinger 
inn 
i 
relasjondatabasene 
våre!
Join-­‐hell.. 
Rekursjon... 
Null-­‐sjekking...
Polyglot 
persistering
NoSQL
Not 
only 
SQL
NoSQL 
• Key-­‐value 
stores 
– Amazon 
Dynamo 
– Eks: 
Voldemort,... 
• BigTable 
kloner 
– Google’s 
BigTable 
– Eks: 
Hbase, 
... 
• Dokumentdatbaser 
– Lotus 
Notes 
– Eks: 
MongoDB, 
CouchDB
NoSQL 
• Grafdatabaser 
– Euler 
og 
graXeori 
– Eks: 
Neo4j, 
VertexDB, 
AllegroGraph, 
Giraphe, 
OrientDB, 
etc...
NoSQL 
Størrelse 
Grafer 
Kompleksitet 
Dokument 
BigTable 
KV
NoSQL 
Størrelse 
Grafer 
Kompleksitet 
Dokument 
BigTable 
KV 
90% 
Milliarder 
av 
noder 
og 
relasjoner
dagens 
dose 
buzzwords
Venner 
av 
venner...
Person 
ID 
Navn 
Venner 
PersonID 
VennID
«Aleks» 
sine 
venner 
Person 
ID 
Navn 
Venner 
PersonID 
VennID 
SELECT 
p1.Person 
FROM 
Person 
p1 
JOIN 
Venner 
ON 
Venner.VennID 
= 
p1.ID 
JOIN 
Person 
p2 
ON 
Venner.PersonID 
= 
p2.ID 
WHERE 
p2.Navn 
= 
'Aleks'
Venner 
med 
«Aleks» 
Person 
ID 
Navn 
Venner 
PersonID 
VennID 
SELECT 
p1.Person 
FROM 
Person 
p1 
JOIN 
Venner 
ON 
Venner.PersonID 
= 
p1.ID 
JOIN 
Person 
p2 
ON 
Venner.VennID 
= 
p2.ID 
WHERE 
p2.Navn 
= 
'Aleks'
«Aleks» 
sine 
venners 
venner? 
Person 
ID 
Navn 
Venner 
PersonID 
VennID 
SELECT 
p1.Person 
AS 
PERSON, 
p2.Person 
AS 
VENN_AV_VENN 
FROM 
Venner 
v1 
JOIN 
Person 
p1 
ON 
v1.PersonID 
= 
p1.ID 
JOIN 
Venner 
v2 
ON 
v2.VennID 
= 
v1.VennID 
JOIN 
Person 
p2 
ON 
v2.VennID 
= 
p2. 
ID 
WHERE 
p1.Navn 
= 
'Aleks‘ 
AND 
v2.VennID 
<> 
p1.ID
Kilde: 
Neo4j 
in 
Ac@on 
1,000,000 
personer 
Dybde 
RDBMS 
Neo4j 
Antall 
rader 
2 
0.016 
0.01 
~ 
2500 
3 
30.267 
0.168 
~ 
110,000 
4 
1543.505 
1.359 
~ 
600,000 
5 
DNF 
2.132 
~ 
800,000
En 
grafdatabase... 
• benyper 
grafstrukturer 
med 
– Noder 
– Relasjoner 
– Apribuper 
• @l 
å 
lagre 
informasjon! 
• Ypperlig 
@l 
relasjoner 
– 
men 
ikke 
best 
på 
aggregering 
av 
data
Grafdatabaser 
og 
neo4j 
• Visuelt 
– 
Skjemaløst! 
• Dobbelt-­‐lenkede-­‐lister 
– hver 
node 
har 
en 
liste 
med 
innkommende 
og 
utgående 
relasjoner 
• Direkte 
lookup 
= 
O(1)
En 
graf 
lagrer 
data 
i 
noder 
ALEKS 
TARJEI
Relasjoner 
knyper 
noder 
sammen 
FRIENDS_WITH 
ALEKS 
TARJEI
Noder 
har 
aribuer 
ALEKS 
TARJEI 
Age: 
29 
First 
Name: 
Aleksander 
Last 
Name: 
Stensby 
Age: 
30 
First 
Name: 
Tarjei 
Last 
Name: 
Romtveit 
FRIENDS_WITH
Relasjoner 
kan 
også 
ha 
aribuer 
ALEKS 
TARJEI 
Age: 
29 
First 
Name: 
Aleksander 
Last 
Name: 
Stensby 
Age: 
30 
First 
Name: 
Tarjei 
Last 
Name: 
Romtveit 
FRIENDS_WITH 
Since: 
01.01.2004
Relasjoner 
kan 
gå 
flere 
veier 
ALEKS 
TARJEI 
FRIENDS_WITH 
Since: 
01.01.2004 
Age: 
29 
First 
Name: 
Aleksander 
Last 
Name: 
Stensby 
Age: 
30 
First 
Name: 
Tarjei 
Last 
Name: 
Romtveit 
FRIENDS_WITH 
Since: 
01.01.2004
ALEKS 
TARJEI 
Age: 
29 
First 
Name: 
Aleksander 
Last 
Name: 
Stensby 
Age: 
30 
First 
Name: 
Tarjei 
Last 
Name: 
Romtveit 
FRIENDS_WITH 
Since: 
01.01.2004
forskjellige 
ALEKS 
TARJEI 
Age: 
29 
First 
Name: 
Aleksander 
Last 
Name: 
Stensby 
Type: 
Person 
Age: 
30 
First 
Name: 
Tarjei 
Last 
Name: 
Romtveit 
Type: 
Person 
typer 
FRIENDS_WITH 
Since: 
01.01.2004 
av 
noder
Person 
Person 
Sport 
Programmeringspråk 
Drikke 
Person
Noen 
bruksområder 
• Sosiale 
Medier 
• Recommenda@ons 
• Geo 
rou@ng 
og 
logis@kk 
• Brukerkontroll 
/ 
@lgangshåndtering 
• Network 
management 
• Finans 
/ 
Svindel
Neo4j 
– 
[ER_EN] 
-­‐> 
Property 
Graph
(node) 
– 
[relasjon] 
-­‐> 
(node)
(Aleks) 
– 
[FRIENDS_WITH] 
-­‐> 
(Tarjei)
Cypher 
"Make 
the 
simple 
things 
simple, 
and 
the 
complex 
things 
possible"
Intro 
@l 
Cypher 
• START 
• MATCH 
• WHERE 
• RETURN 
• CREATE 
• DELETE 
• SET 
• FOREACH 
• WITH
MATCH 
<papern> 
WHERE 
RETURN
Beskriv 
hva 
du 
ønsker 
å 
hente 
ut 
med 
PATTERNS 
(a)-­‐[r]-­‐>(b)
(Aleks) 
– 
[FRIENDS_WITH] 
-­‐> 
(Tarjei)
Path 
depth 
(a)-­‐[*]-­‐>(b)
START 
START 
n=node(1) 
RETURN 
n
MATCH 
MATCH 
(movie:Movie) 
RETURN 
movie
WHERE 
MATCH 
movie 
WHERE 
move.@tle 
= 
‘Blade 
Runner' 
RETURN 
movie
START 
a=node(*) 
MATCH 
(a:Person) 
WHERE 
a.name='Danny 
DeVito' 
RETURN 
a
START 
a=node(*) 
MATCH 
(a:Person) 
WHERE 
a.name='Danny 
DeVito' 
RETURN 
a
START 
a=node(*) 
MATCH 
(a:Person 
{name: 
'Danny 
DeVito'} 
) 
WHERE 
a.name='Danny 
DeVito' 
RETURN 
a 
Neo4j 
2.0.1
Venners 
venner... 
MATCH 
(aleks)-­‐[:KNOWS*2..2]-­‐>(friend_of_friend) 
WHERE 
aleks.firstName= 
'Aleks' 
RETURN 
friend_of_friend.firstName
Venners 
venner... 
MATCH 
(aleks)-­‐[:KNOWS*2..2]-­‐>(friend_of_friend) 
WHERE 
aleks.firstName= 
'Aleks' 
AND 
NOT 
(aleks)-­‐[:KNOWS]-­‐>(friend_of_friend) 
RETURN 
friend_of_friend.firstName
hpp://docs.neo4j.org/refcard/2.1.5/
på 
1-­‐2-­‐3 
DEMO
Og 
mye 
mye 
mer... 
• Traversals 
navigerer 
grafen 
• Traversals 
iden@fiserer 
paths 
• Paths 
ordner 
noder 
• Cypher 
parametere 
• Indexes 
• Neo4j 
embedded, 
Neo4j 
REST 
• Neo4j 
2.x 
– 
Labels 
og 
automaaske 
indekser 
• Gremlin 
(procedural) 
– 
Cypher 
(declara@ve) 
• Enterprise 
(mul@-­‐instance 
cluster, 
online 
backup) 
• Neo4j 
Spaaal
Språkdrivere
Contrib
Intro 
@l 
SpringData 
Neo4j 
• SpringData 
• Repositories 
• Kjapt 
eksempel 
på 
oppsep 
<dependencies> 
<dependency> 
<groupId>org.springframework.data</groupId> 
<ar@factId>spring-­‐data-­‐neo4j</ar@factId> 
<version>3.2.0.RELEASE</version> 
</dependency> 
</dependencies> 
neo4j 
2.1.4 
spring-­‐data-­‐neo4j 
3.2.0 
spring 
4.1.0
SpringData 
Neo4j 
Config 
@Configuration 
@EnableNeo4jRepositories("no.stensby.javabin.neo.repositories") 
public 
class 
Config 
extends 
Neo4jConfiguration 
{ 
} 
@Autowired 
protected 
Neo4jTemplate 
template;
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;
SpringData 
Neo4j 
Config 
Embedded: 
@Bean 
GraphDatabaseService 
graphDatabaseService() 
{ 
return 
new 
GraphDatabaseFactory().newEmbeddedDatabase("tmp/neo4j"); 
} 
REST: 
@Bean 
GraphDatabaseService 
graphDatabaseService() 
{ 
return 
new 
SpringRestGraphDatabase("http://localhost:7474/db/data"); 
} 
TEST: 
@Bean(destroyMethod 
= 
"shutdown") 
@Scope(SCOPE_PROTOTYPE) 
public 
GraphDatabaseService 
graphDatabaseService() 
{ 
return 
new 
TestGraphDatabaseFactory().newImpermanentDatabase(); 
}
SpringData 
Neo4j 
-­‐ 
Domene 
@NodeEntity 
public 
class 
Person 
{ 
@GraphId 
public 
Long 
nodeId; 
@Indexed(unique 
= 
true) 
public 
int 
id; 
public 
String 
firstName; 
public 
String 
lastName; 
}
1-­‐1 
relasjoner 
@NodeEntity 
public 
class 
Address 
{ 
private 
Country 
country; 
... 
}
1-­‐1 
relasjoner 
@NodeEntity 
public 
class 
Address 
{ 
@Fetch 
private 
Country 
country; 
... 
}
1-­‐m 
relasjoner 
-­‐ 
@RelatedTo 
@RelatedTo 
private 
Set<Person> 
venner 
= 
new 
HashSet<Person> 
(); 
@RelatedTo(type 
= 
"ADDRESS") 
private 
Set<Address> 
addresses 
= 
new 
HashSet<Address>();
m-­‐m 
relasjoner 
-­‐ 
@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>(); 
Movie 
Actor 
@RelationshipEntity(type 
= 
"ACTS_IN") 
public 
class 
Role 
{ 
@GraphId 
Long 
nodeId; 
@StartNode 
Actor 
actor; 
@EndNode 
Movie 
movie; 
Role
SpringData 
Neo4j 
-­‐ 
Repositories 
public 
interface 
PersonRepository 
extends 
GraphRepository<Person>{} 
@Autowired 
PersonRepository 
repository;
SpringData 
Neo4j 
-­‐ 
Repositories 
public 
interface 
PersonRepository 
extends 
GraphRepository<Person>{ 
findByFirstName(String 
firstName); 
}
SpringData 
Neo4j 
-­‐ 
Repositories 
public 
interface 
PersonRepository 
extends 
GraphRepository<Person>{ 
Person 
findByFirstName(String 
firstName); 
@Query("MATCH 
(p:Person{firstName:{0}}) 
RETURN 
p") 
Person 
getPersonWithFirstName 
(String 
firstName); 
}
SpringData 
Neo4j 
-­‐ 
Repositories 
public 
interface 
PersonRepository 
extends 
GraphRepository<Person>{ 
Person 
findByFirstName(String 
firstName); 
@Query("MATCH 
(p:Person{firstName:{0}}) 
RETURN 
p") 
Person 
getPersonWithFirstName 
(String 
firstName); 
@Query( 
"MATCH 
(p:Person{firstName:{0}})-­‐[:knows]-­‐>friends 
" 
+ 
" 
RETURN 
friends") 
Iterable<Person> 
findFriendsOfPerson(String 
firstName); 
}
Lessons 
Learned 
• Vær 
forsik@g 
med 
versjoner 
og 
oppgraderinger… 
– Neo 
1.9.x 
-­‐> 
Neo 
2.x 
kan 
by 
på 
mange 
problemer 
– Mange 
breaking 
changes 
underveis… 
– Spring 
Data 
Neo4j 
!= 
Neo4j 
• Noder 
er 
“first-­‐class” 
ci@zens 
I 
grafen 
– Hyperedges 
støpes 
ikke 
-­‐> 
kan 
bruke 
“event 
nodes” 
• Dyrt 
å 
aggregere 
-­‐ 
caching 
av 
stats 
på 
noder! 
• Unike 
og 
ekspressive 
relasjonstyper
Pi…alls 
• Ikke 
bruk 
graphid 
som 
ID 
• Ini@aliser 
alle 
Set 
• Bruk 
@Fetch, 
men 
vær 
forsik@g 
• Bruk 
standard 
gepers/sepers 
• Vær 
varsom 
med 
String 
escaping
Inspirasjon 
• Single 
Malt 
Scotch 
Whisky 
hpp://gist.neo4j.org/?8139605 
• Chess 
Games 
and 
Posiaons 
hpp://gist.neo4j.org/?6506717 
• Movie 
Recommendaaons 
hpp://gist.neo4j.org/?8173017 
• Food 
Recipes 
Recommendaaon 
hpp://gist.neo4j.org/?8731452

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Introduction to Neo4j and Spring Data

  • 1. Introduksjon ,l grafdatabaser, Neo4j og Spring Data Aleksander M. Stensby Monokkel A/S
  • 2. • Daglig leder i Monokkel AS • Tidligere COO i Integrasco AS • Persistering, Prosessering og Presentasjon av data Persistering – Prosessering – Presentasjon
  • 3. • Aleksander M. Stensby • Daglig leder i Monokkel AS • Tidligere COO i Integrasco AS www.monokkel.io
  • 4. Agenda • Intro @l grafdatabaser og modellering • Neo4j og Cypher • SpringData Neo4j
  • 7. Relasjondatabaser er fantas@sk på aggregering av data, mapping av skjemaer og tabell data!
  • 8. MEN
  • 9. vi har en tendens @l å tvinge _alle_ problems@llinger inn i relasjondatabasene våre!
  • 12. NoSQL
  • 14. NoSQL • Key-­‐value stores – Amazon Dynamo – Eks: Voldemort,... • BigTable kloner – Google’s BigTable – Eks: Hbase, ... • Dokumentdatbaser – Lotus Notes – Eks: MongoDB, CouchDB
  • 15. NoSQL • Grafdatabaser – Euler og graXeori – Eks: Neo4j, VertexDB, AllegroGraph, Giraphe, OrientDB, etc...
  • 16. NoSQL Størrelse Grafer Kompleksitet Dokument BigTable KV
  • 17. NoSQL Størrelse Grafer Kompleksitet Dokument BigTable KV 90% Milliarder av noder og relasjoner
  • 20. Person ID Navn Venner PersonID VennID
  • 21. «Aleks» sine venner Person ID Navn Venner PersonID VennID SELECT p1.Person FROM Person p1 JOIN Venner ON Venner.VennID = p1.ID JOIN Person p2 ON Venner.PersonID = p2.ID WHERE p2.Navn = 'Aleks'
  • 22. Venner med «Aleks» Person ID Navn Venner PersonID VennID SELECT p1.Person FROM Person p1 JOIN Venner ON Venner.PersonID = p1.ID JOIN Person p2 ON Venner.VennID = p2.ID WHERE p2.Navn = 'Aleks'
  • 23. «Aleks» sine venners venner? Person ID Navn Venner PersonID VennID SELECT p1.Person AS PERSON, p2.Person AS VENN_AV_VENN FROM Venner v1 JOIN Person p1 ON v1.PersonID = p1.ID JOIN Venner v2 ON v2.VennID = v1.VennID JOIN Person p2 ON v2.VennID = p2. ID WHERE p1.Navn = 'Aleks‘ AND v2.VennID <> p1.ID
  • 24. Kilde: Neo4j in Ac@on 1,000,000 personer Dybde RDBMS Neo4j Antall rader 2 0.016 0.01 ~ 2500 3 30.267 0.168 ~ 110,000 4 1543.505 1.359 ~ 600,000 5 DNF 2.132 ~ 800,000
  • 25.
  • 26. En grafdatabase... • benyper grafstrukturer med – Noder – Relasjoner – Apribuper • @l å lagre informasjon! • Ypperlig @l relasjoner – men ikke best på aggregering av data
  • 27. Grafdatabaser og neo4j • Visuelt – Skjemaløst! • Dobbelt-­‐lenkede-­‐lister – hver node har en liste med innkommende og utgående relasjoner • Direkte lookup = O(1)
  • 28. En graf lagrer data i noder ALEKS TARJEI
  • 29. Relasjoner knyper noder sammen FRIENDS_WITH ALEKS TARJEI
  • 30. Noder har aribuer ALEKS TARJEI Age: 29 First Name: Aleksander Last Name: Stensby Age: 30 First Name: Tarjei Last Name: Romtveit FRIENDS_WITH
  • 31. Relasjoner kan også ha aribuer 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. Relasjoner kan gå flere veier ALEKS TARJEI FRIENDS_WITH Since: 01.01.2004 Age: 29 First Name: Aleksander Last Name: Stensby Age: 30 First Name: Tarjei Last Name: Romtveit 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. forskjellige ALEKS TARJEI Age: 29 First Name: Aleksander Last Name: Stensby Type: Person Age: 30 First Name: Tarjei Last Name: Romtveit Type: Person typer FRIENDS_WITH Since: 01.01.2004 av noder
  • 35. Person Person Sport Programmeringspråk Drikke Person
  • 36. Noen bruksområder • Sosiale Medier • Recommenda@ons • Geo rou@ng og logis@kk • Brukerkontroll / @lgangshåndtering • Network management • Finans / Svindel
  • 37. Neo4j – [ER_EN] -­‐> Property Graph
  • 38. (node) – [relasjon] -­‐> (node)
  • 39. (Aleks) – [FRIENDS_WITH] -­‐> (Tarjei)
  • 40. Cypher "Make the simple things simple, and the complex things possible"
  • 41. Intro @l Cypher • START • MATCH • WHERE • RETURN • CREATE • DELETE • SET • FOREACH • WITH
  • 43. Beskriv hva du ønsker å hente ut med PATTERNS (a)-­‐[r]-­‐>(b)
  • 44. (Aleks) – [FRIENDS_WITH] -­‐> (Tarjei)
  • 48. WHERE MATCH movie WHERE move.@tle = ‘Blade Runner' RETURN movie
  • 49. START a=node(*) MATCH (a:Person) WHERE a.name='Danny DeVito' RETURN a
  • 50. START a=node(*) MATCH (a:Person) WHERE a.name='Danny DeVito' RETURN a
  • 51. START a=node(*) MATCH (a:Person {name: 'Danny DeVito'} ) WHERE a.name='Danny DeVito' RETURN a Neo4j 2.0.1
  • 52. Venners venner... MATCH (aleks)-­‐[:KNOWS*2..2]-­‐>(friend_of_friend) WHERE aleks.firstName= 'Aleks' RETURN friend_of_friend.firstName
  • 53. Venners venner... MATCH (aleks)-­‐[:KNOWS*2..2]-­‐>(friend_of_friend) WHERE aleks.firstName= 'Aleks' AND NOT (aleks)-­‐[:KNOWS]-­‐>(friend_of_friend) RETURN friend_of_friend.firstName
  • 56. Og mye mye mer... • Traversals navigerer grafen • Traversals iden@fiserer paths • Paths ordner noder • Cypher parametere • Indexes • Neo4j embedded, Neo4j REST • Neo4j 2.x – Labels og automaaske indekser • Gremlin (procedural) – Cypher (declara@ve) • Enterprise (mul@-­‐instance cluster, online backup) • Neo4j Spaaal
  • 59.
  • 60. Intro @l SpringData Neo4j • SpringData • Repositories • Kjapt eksempel på oppsep <dependencies> <dependency> <groupId>org.springframework.data</groupId> <ar@factId>spring-­‐data-­‐neo4j</ar@factId> <version>3.2.0.RELEASE</version> </dependency> </dependencies> neo4j 2.1.4 spring-­‐data-­‐neo4j 3.2.0 spring 4.1.0
  • 61. SpringData Neo4j Config @Configuration @EnableNeo4jRepositories("no.stensby.javabin.neo.repositories") public class Config extends Neo4jConfiguration { } @Autowired protected Neo4jTemplate template;
  • 62. 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;
  • 63. SpringData Neo4j Config Embedded: @Bean GraphDatabaseService graphDatabaseService() { return new GraphDatabaseFactory().newEmbeddedDatabase("tmp/neo4j"); } REST: @Bean GraphDatabaseService graphDatabaseService() { return new SpringRestGraphDatabase("http://localhost:7474/db/data"); } TEST: @Bean(destroyMethod = "shutdown") @Scope(SCOPE_PROTOTYPE) public GraphDatabaseService graphDatabaseService() { return new TestGraphDatabaseFactory().newImpermanentDatabase(); }
  • 64. SpringData Neo4j -­‐ Domene @NodeEntity public class Person { @GraphId public Long nodeId; @Indexed(unique = true) public int id; public String firstName; public String lastName; }
  • 65. 1-­‐1 relasjoner @NodeEntity public class Address { private Country country; ... }
  • 66. 1-­‐1 relasjoner @NodeEntity public class Address { @Fetch private Country country; ... }
  • 67. 1-­‐m relasjoner -­‐ @RelatedTo @RelatedTo private Set<Person> venner = new HashSet<Person> (); @RelatedTo(type = "ADDRESS") private Set<Address> addresses = new HashSet<Address>();
  • 68. m-­‐m relasjoner -­‐ @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>(); Movie Actor @RelationshipEntity(type = "ACTS_IN") public class Role { @GraphId Long nodeId; @StartNode Actor actor; @EndNode Movie movie; Role
  • 69. SpringData Neo4j -­‐ Repositories public interface PersonRepository extends GraphRepository<Person>{} @Autowired PersonRepository repository;
  • 70. SpringData Neo4j -­‐ Repositories public interface PersonRepository extends GraphRepository<Person>{ findByFirstName(String firstName); }
  • 71. SpringData Neo4j -­‐ Repositories public interface PersonRepository extends GraphRepository<Person>{ Person findByFirstName(String firstName); @Query("MATCH (p:Person{firstName:{0}}) RETURN p") Person getPersonWithFirstName (String firstName); }
  • 72. SpringData Neo4j -­‐ Repositories public interface PersonRepository extends GraphRepository<Person>{ Person findByFirstName(String firstName); @Query("MATCH (p:Person{firstName:{0}}) RETURN p") Person getPersonWithFirstName (String firstName); @Query( "MATCH (p:Person{firstName:{0}})-­‐[:knows]-­‐>friends " + " RETURN friends") Iterable<Person> findFriendsOfPerson(String firstName); }
  • 73. Lessons Learned • Vær forsik@g med versjoner og oppgraderinger… – Neo 1.9.x -­‐> Neo 2.x kan by på mange problemer – Mange breaking changes underveis… – Spring Data Neo4j != Neo4j • Noder er “first-­‐class” ci@zens I grafen – Hyperedges støpes ikke -­‐> kan bruke “event nodes” • Dyrt å aggregere -­‐ caching av stats på noder! • Unike og ekspressive relasjonstyper
  • 74. Pi…alls • Ikke bruk graphid som ID • Ini@aliser alle Set • Bruk @Fetch, men vær forsik@g • Bruk standard gepers/sepers • Vær varsom med String escaping
  • 75. Inspirasjon • Single Malt Scotch Whisky hpp://gist.neo4j.org/?8139605 • Chess Games and Posiaons hpp://gist.neo4j.org/?6506717 • Movie Recommendaaons hpp://gist.neo4j.org/?8173017 • Food Recipes Recommendaaon hpp://gist.neo4j.org/?8731452