Download - Use Neo4j In Your Next Java Project
![Page 1: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/1.jpg)
Use Neo4j In Your Next Java Project
Love relationships again
Tobias Coetzee
@tobiascode
![Page 2: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/2.jpg)
All Roads Lead To SQL
Do we even have
other choices?
Every project we work on will have
some form of database, 99% of
those in the enterprise will be a
relational database
![Page 3: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/3.jpg)
Impedance MismatchThe database model does not
match the domain model
Not ScalingMore data means less speed
Complex QueriesLots of joins and some self
joining nightmares as a bonus
Relationship Problems
![Page 4: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/4.jpg)
Facebook For Superheroes
MEMBER_OF
TOO
K_PA
RT_INW
AS_I
N
Characters
Events
Teams
Stories
CONSISTS_OF
FR
IEN
D_O
F
![Page 5: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/5.jpg)
Queries with lots of joins to get to the final answer
Similar Data Models
Configuration
Security
Navigation
![Page 6: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/6.jpg)
Graph Databases
Just MathematicsBased on graph theory, created
by Leonhard Euler
All About RelationshipsIn graph databases relationships
are first class citizens
Neo4jMost well known and widely
used graph database
![Page 7: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/7.jpg)
Graph Databases
NodesRepresent the entities or
records in the database
RelationshipsLink nodes together
PropertiesNodes and relationships can
also have properties
![Page 8: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/8.jpg)
MEM
BER
_OF
MEM
BER
_OF
FRIEND_OF
WA
S_I
N
TOOK_PART_IN • Eyes: Blue
• Hair: None
• Citizenship: Canada• Since: 14 Feb 2001
Graph Databases
![Page 9: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/9.jpg)
The database model does not
match the domain model
Impedance Mismatch
![Page 10: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/10.jpg)
Impedance MismatchCharacter_Team
Character_Event
Team_Event
Character_Story
Team_Story
Event_Story
FriendsMarvelCharacter
Team
Event
Story
![Page 11: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/11.jpg)
Traditional Solutions
ORM Frameworks, e.g.
Hibernate or Spring Data
Boilerplate code generation
tools
Impedance Mismatch
![Page 12: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/12.jpg)
Impedance Mismatch
MEMBER_OF
TOO
K_PA
RT_IN
WA
S_I
N
Characters
Events
Teams
Stories
CONSISTS_OF
FR
IEN
D_O
F
![Page 13: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/13.jpg)
MATCH
(c:Character)-[:MEMBER_OF]->(t:Team)
WHERE t.name = 'Avengers'
RETURN c.name
Cypher Intro
![Page 14: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/14.jpg)
Demo Time
![Page 15: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/15.jpg)
Lots of joins and some self
joining nightmares as a bonus
Complex Queries
![Page 16: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/16.jpg)
Denormalise the database
Hide complexity behind
views and stored procedures
Traditional Solutions
Complex Queries
![Page 17: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/17.jpg)
Friends of Friends
Name Number Friends
in Common
Vision 51
Wolfsbane 38
Punisher 33
Rage 38
Shard 25
Wind Dancer 25
Possible friend recommendations for Deadpool
![Page 18: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/18.jpg)
SELECT FriendOfFriend.Name, COUNT(*)
FROM MarvelCharacter deadpool
INNER JOIN Friends DeadpoolFriends
ON deadpool.Id = DeadpoolFriends.CharacterId1
INNER JOIN Friends FriendsFriends
ON DeadpoolFriends.CharacterId2 = FriendsFriends.CharacterId1
INNER JOIN MarvelCharacter FriendOfFriend
ON FriendsFriends.CharacterId2 = FriendOfFriend.Id
WHERE deadpool.Name = 'Deadpool'
AND FriendsFriends.CharacterId2 NOT IN( SELECT CharacterId2
FROM MarvelCharacter
INNER JOIN Friends
ON MarvelCharacter.Id = CharacterId1
WHERE Name = 'Deadpool')
GROUP BY FriendOfFriend.Name
ORDER BY COUNT(*) DESC
Sooo Many Joins
![Page 19: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/19.jpg)
MATCH (deadpool:Character)-[:FRIEND_OF*2]->(FriendOfFriend)
WHERE deadpool.name = 'Deadpool'
AND NOT (deadpool)-[:FRIEND_OF]->(FriendOfFriend)
AND NOT deadpool = FriendOfFriend
RETURN FriendOfFriend.name, COUNT(*)
ORDER BY COUNT(*) DESC, FriendOfFriend.name
Sooo Many Joins
![Page 20: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/20.jpg)
MATCH (deadpool:Character)-[:FRIEND_OF*2]->(FriendOfFriend)
Sooo Many Joins
FROM MarvelCharacter deadpool
INNER JOIN Friends DeadpoolFriends
ON deadpool.Id = DeadpoolFriends.CharacterId1
INNER JOIN Friends FriendsFriends
ON DeadpoolFriends.CharacterId2 = FriendsFriends.CharacterId1
INNER JOIN MarvelCharacter FriendOfFriend
ON FriendsFriends.CharacterId2 = FriendOfFriend.Id
![Page 21: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/21.jpg)
Quick Demo
![Page 22: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/22.jpg)
How Do You Know Him?
MEM
BER
_OF
MEM
BER
_OF
FRIEND_OF
How can Deadpool connect to Ironman?
![Page 23: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/23.jpg)
MATCH (deadpool:Character { name:"Deadpool" }),
(other:Character { name:"Ultron" }),
path = shortestPath((deadpool)-[*]-(other))
RETURN path
Shortest Path
![Page 24: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/24.jpg)
MATCH (deadpool:Character { name:"Deadpool" }),
(other:Character { name:"Ultron" }),
path = shortestPath((deadpool)-[*]-(other))
RETURN path
Shortest Path
![Page 25: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/25.jpg)
MATCH (deadpool:Character { name:"Deadpool" }),
(other:Character { name:"Ultron" }),
path = shortestPath((deadpool)-[*]-(other))
RETURN path
Shortest Path
![Page 26: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/26.jpg)
MATCH (deadpool:Character { name:"Deadpool" }),
(other:Character { name:"Ultron" }),
path = shortestPath((deadpool)-[*]-(other))
RETURN path
Shortest Path
![Page 27: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/27.jpg)
Quick Demo
![Page 28: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/28.jpg)
Not ScalingMore data means less speed
![Page 29: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/29.jpg)
Traditional Solutions
Offline databases
Batch runs to
process data
More indexes
Not Scaling
![Page 30: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/30.jpg)
Speed
Embedded ServerProcessing requires reading the
same data you wrote
Fixed Size RecordsAll records of the same type
have the same size on disk
Index-Free AdjacencyPointer to the next node
![Page 31: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/31.jpg)
Demo Time
![Page 32: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/32.jpg)
Wrong UsageThis is not a hammer
![Page 33: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/33.jpg)
Wrong Usage
Set OrientatedLists of things with few or no
joins
Global OperationsMade for local graph
operations
Aggregate QueriesProcessing requires reading the
same data you wrote
![Page 34: Use Neo4j In Your Next Java Project](https://reader031.vdocuments.site/reader031/viewer/2022030213/589d67fe1a28abd91d8b54e9/html5/thumbnails/34.jpg)
Thank YouFor Staying Till The End!
@tobiascode