2013-11-02 : DevFest Türkiye, Istanbul.
Slightly modified version of my previous Neo4J introduction talk about Neo4J in Soft-Shake Event, Geneva, Switzerland.
17. A glimpse at usecases: mantra
RELATIONSHIPS
ARE AS IMPORTANT AS
E N T I T I E S
18. A glimpse at usecases
Recommendations
People I may know
ex: people known by contacts I have
worked with in the past
19. A glimpse at usecases
Recommendations
People I may know
ex: people known by contacts I have
worked with in the past
Products I should buy
ex: people who bought “Twilight” and
“Justin Bieber biography” like you
also bought “The ultimate emo
guide”
20. A glimpse at usecases
Recommendations
People I may know
ex: people known by contacts I have
worked with in the past
Products I should buy
ex: people who bought “Twilight” and
“Justin Bieber biography” like you
also bought “The ultimate emo
guide”
Movies I should watch with whom and where...
21. A glimpse at usecases
Pattern detection
Fraud
ex: many IPs from Fraudistan have
made a purchase of game X
in the last hour
22. A glimpse at usecases
Pattern detection
Fraud
ex: many IPs from Fraudistan have
made a purchase of game X
in the last hour
Disease detection
ex: DNA sequencing
23. A glimpse at usecases
Pattern detection
Fraud
ex: many IPs from Fraudistan have
made a purchase of game X
in the last hour
Disease detection
ex: DNA sequencing
Trend detection
ex: the term Flu has been tweeted
789% times more in Guatemala area
in the last 24 hours
24. A glimpse at usecases
Path finding
Genealogy
ex: is François Mitterand related to
Elizabeth II ? (yes)
25. A glimpse at usecases
Path finding
Genealogy
ex: is François Mitterand related to
Elizabeth II ? (yes)
Navigation
ex: what is the cheapest way to go
to a sushi place < 15€ for me (Place
de Clichy) and my friend (Place d’
Italie)?
26. A glimpse at usecases
Path finding
Genealogy
ex: is François Mitterand related to
Elizabeth II ? (yes)
Navigation
ex: what is the cheapest way to go
to a sushi place < 15€ for me (Place
de Clichy) and my friend (Place d’
Italie)?
Impact analysis
ex: which customers are impacted if
network switch XYZ fails?
27. A glimpse at usecases
and more...
Topological ordering
ex: given a set of dependencies, in
which order should I include them?
Community detection
ex: tag clustering on annotated
resources to detect groups of interest
(targeted advertising)
and much more...
28. A glimpse at usecases
http://www.opentreeoflife.org/
http://bio4j.com/
http://www.reco4j.org/
http://structr.org/
https://github.com/neo4j-contrib/graphgist/wiki
31. Neo4J - the origins
Circa 2000, somewhere in Sweden
2 swedish guys hacking in a garage
32. Neo4J - the origins
Dialogue
- Man, I cannot stand Informix anymore
- Right, we’ve pushed it to the limit
- All these documents, these workflows…
- Right, it’s way too densely connected.
- Connected… connections? CONNECTIONS??
34. Flash-forward: Neo Technology!
1 company
Neo Technology
1 main product
Neo4J
~50 employees
All over the world
Sweden, US, Germany,
France, Malaysia, NZ...
35. Neo4J - moaar facts & figures
Versions
2.0.0.M06 / 1.9.4
Licenses
GPL, AGPL, OEM, Commercial
235 nodes
34_359_738_368
235 relationships
> 236 properties
at most 238
… capacity can be tailored on demand
51. Node CRUD (JVM)
try (Transaction transaction = graphDB.beginTx()) {
Node character = /*lookup*/ ;
character.delete();
transaction .success();
}
Gotchas
●
no relationships must be attached when transaction commits
●
all properties will be automatically removed
56. Relationship CRUD (JVM)
try (Transaction transaction = graphDB.beginTx()) {
Relationship relationship = /*lookup*/ ;
relationship .delete();
transaction .success();
}
Gotcha
●
a write lock is set on the relationship, as well as both start AND end
nodes of the relationship
57. Core API
Low Level
Transactions are ALWAYS required (v2.0)
Technical IDs are dangerous (findById)
have a look at github.com/sarmbruster/neo4j-uuid
SAME capabilities with REST API
83. Cypher reads
RULES OF THUMB
● MATCH for results
● use WHERE to filter (WHERE
)
a-[:F]->b or NOT(a-[:F]->b)
● favour parameters over literals (exec. plan reuse)
● javacodegeeks.com: “optimizing Neo4j Cypher Queries”
86. Cypher writes
START homer = node:characters("name:Hom*")
MATCH (d:JUNK:FOOD)
WHERE d.brand = "Fattylicious!"
CREATE (homer)-[luv:LOVES_EATING {quantity:∞}]->(d)
RETURN luv
87. Cypher writes
MERGE (keanu:ACTOR {name:'Keanu Reeves'})
ON CREATE keanu
SET keanu.created = timestamp()
ON MATCH keanu
SET keanu.lastSeen = timestamp()
RETURN keanu
88. Cypher - I want moaaar
Declarative power
Super nice syntax
Evolutionary design with MERGE!
http://console.neo4j.org to try it out!
Cypher will the #1 way to query data!
97. Moaaar Spring Data Neo4J repositories
public interface BranchRepository extends GraphRepository< Branch> {
Iterable<Branch> findByNameLike (String name);
@Query("MATCH (p:PROJECT)-[b:HAS_BRANCH]->(c:COMMIT) RETURN b" )
Cool things
Page<Branch> lookMaIveGotPages ();
● Branch findByNameAndCommitIdentifierLike (String
boilerplate methods already provided
}●
name, String commit);
you declare methods following a naming convention, Spring Data Neo4J
generates the right implementation for ya!
●
YOU EXPOSE YOUR DOMAIN, no Nodes, no Relationships!
98. Spring Data Neo4J node entities
@NodeEntity
public class Person {
@GraphId
private Long id;
@Indexed(indexName = "people", type=FULLTEXT)
private String name;
@RelatedTo (type="OWNS", enforceTargetType = true)
private Car car;
@RelatedToVia (type="FRIEND_OF" , direction = Direction.INCOMING)
private Iterable<Friendship> friendships;
@GraphTraversal (traversal = PeopleTraversalBuilder .class,
elementClass = Person.class, params = "persons")
private Iterable<Person> people;
}
99. Spring Data Neo4J relationship entities
@RelationshipEntity(type ="FRIEND_OF" )
public class Friendship {
@StartNode
private Person person;
@EndNode
private Dog humansBestFriend;
@GraphProperty /* optional here ;-) */
private Date since;
/**
* moaaaaar properties
*/
}