Soft Shake Event / A soft introduction to Neo4J
-
Upload
florent-biville -
Category
Technology
-
view
595 -
download
3
description
Transcript of Soft Shake Event / A soft introduction to Neo4J
![Page 1: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/1.jpg)
A soft introduction to
(Reality)-[:IS_A]->(Graph)
@fbiville / @LateraIThoughts / florent.biville.net / www.lateral-thoughts.com
![Page 2: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/2.jpg)
What we have always known
selection sort (O(n2))
![Page 3: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/3.jpg)
What we have always known
selection sort (O(n2))
|
heap sort (O(n*log(n))
![Page 4: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/4.jpg)
What we have always known
selection sort (O(n2))
|
heap sort (O(n*log(n))
same algorithm, different data structure, better execution time !
![Page 5: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/5.jpg)
What we have always known
1 data structure
1 usage
![Page 6: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/6.jpg)
One NOSQL lesson?
1 data STORE
1 usage
![Page 7: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/7.jpg)
One NOSQL lesson?
polyglot persistence, anyone ?
![Page 8: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/8.jpg)
![Page 9: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/9.jpg)
ZOOM on Graph Databases
graph=
nodes/vertices+
edges/relationships/arcs
![Page 10: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/10.jpg)
Graph DB : a common model
property graph=
nodes+
labeled relationships+
K/V pairs
![Page 11: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/11.jpg)
Graph DB : a common model
property graph=
labeledneov2 nodes+
labeled relationships+
K/V pairs
![Page 12: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/12.jpg)
Property Graph DBs
Flock DB
![Page 13: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/13.jpg)
Property Graph DBs
WHY DO THEY
KICK ASS?
![Page 14: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/14.jpg)
BECAUSE
![Page 15: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/15.jpg)
Graph-based computing
Intuitive model
Expressive querying
Powerful analyses
![Page 16: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/16.jpg)
Graph-based computing
Intuitive model
Expressive querying
Powerful analyses
Whiteboard-friendliness
Pregel (GOOG), TAO (FB)
Pattern matching, path
finding...
![Page 17: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/17.jpg)
A glimpse at usecases: mantra
RELATIONSHIPS
ARE AS IMPORTANT AS E N T I T I E S
![Page 18: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/18.jpg)
A glimpse at usecases
Recommendations
People I may know ex: people known by contacts I have worked with in the past
![Page 19: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/19.jpg)
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”
![Page 20: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/20.jpg)
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...
![Page 21: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/21.jpg)
A glimpse at usecases
Pattern detection
Fraud ex: many IPs from Fraudistan havemade a purchase of game X in the last hour
![Page 22: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/22.jpg)
A glimpse at usecases
Pattern detection
Fraud ex: many IPs from Fraudistan havemade a purchase of game X in the last hour
Disease detection ex: DNA sequencing
![Page 23: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/23.jpg)
A glimpse at usecases
Pattern detection
Fraud ex: many IPs from Fraudistan havemade 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 areain the last 24 hours
![Page 24: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/24.jpg)
A glimpse at usecases
Path finding
Genealogy ex: is François Mitterand related toElizabeth II ? (yes)
![Page 25: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/25.jpg)
A glimpse at usecases
Path finding
Genealogy ex: is François Mitterand related toElizabeth II ? (yes)
Navigation ex: what is the cheapest way to go to a sushi < 15€ for me (Place de Clichy) and my friend (Place d’Italie)?
![Page 26: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/26.jpg)
A glimpse at usecases
Path finding
Genealogy ex: is François Mitterand related toElizabeth II ? (yes)
Navigation ex: what is the cheapest way to go to a sushi < 15€ for me (Place de Clichy) and my friend (Place d’Italie)?
Impact analysis ex: which customers are impacted ifnetwork switch XYZ fails?
![Page 27: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/27.jpg)
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...
![Page 28: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/28.jpg)
A glimpse at usecases
http://www.opentreeoflife.org/
http://bio4j.com/
http://www.reco4j.org/
https://github.com/neo4j-contrib/graphgist/wiki
![Page 29: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/29.jpg)
In short
GRAPHS ARE
EVERYWHERE!
![Page 30: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/30.jpg)
THAT’S WHYYOU
SHOULD TRY
![Page 31: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/31.jpg)
Neo4J - the origins
Circa 2000, somewhere in Sweden
2 swedish guys hacking in a garage
![Page 32: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/32.jpg)
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??
![Page 33: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/33.jpg)
![Page 34: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/34.jpg)
Flash-forward: Neo Technology!
1 company Neo Technology
1 main product Neo4J
~50 employees
All over the world Sweden, US, Germany,
France, Malaysia, NZ...
![Page 35: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/35.jpg)
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
![Page 36: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/36.jpg)
Neo4J anatomy
![Page 37: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/37.jpg)
GRAPH ON DISK(roughly)
original presentation: http://www.ustream.tv/channel/neo4j
![Page 38: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/38.jpg)
![Page 39: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/39.jpg)
![Page 40: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/40.jpg)
![Page 41: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/41.jpg)
![Page 42: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/42.jpg)
COREAPI
![Page 43: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/43.jpg)
Neo4J anatomy
![Page 44: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/44.jpg)
Node CRUD (JVM)
GraphDatabaseService graphDB = new TestGraphDatabaseFactory ()
.newImpermanentDatabase();
try (Transaction transaction = graphDB.beginTx()) {
Node character=graphDB.createNode(DynamicLabel.label("CHARACTER"));
character.setProperty("name", "Homer Simpson");
transaction.success();
}
![Page 45: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/45.jpg)
Node CRUD (JVM)
GraphDatabaseService graphDB = new TestGraphDatabaseFactory()
.newImpermanentDatabase();
try (Transaction transaction = graphDB.beginTx()) {
Node character=graphDB.createNode(DynamicLabel.label("CHARACTER"));
character.setProperty("name", "Homer Simpson");
transaction.success();
}
officially distributed
test version!
![Page 46: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/46.jpg)
Node CRUD (JVM)
GraphDatabaseService graphDB = new TestGraphDatabaseFactory ()
.newImpermanentDatabase();
try (Transaction transaction = graphDB.beginTx()) {
Node character=graphDB.createNode(DynamicLabel.label("CHARACTER"));
character.setProperty("name", "Homer Simpson");
transaction.success();
}
transaction is MANDATORY
Java 7 required since 2.0
![Page 47: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/47.jpg)
Node CRUD (JVM)
GraphDatabaseService graphDB = new TestGraphDatabaseFactory ()
.newImpermanentDatabase();
try (Transaction transaction = graphDB.beginTx()) {
Node character=graphDB.createNode(DynamicLabel.label("CHARACTER"));
character.setProperty("name", "Homer Simpson");
transaction.success();
}
labels are a way
to semi-structure your nodes
(since 2.0)
![Page 48: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/48.jpg)
Node CRUD (JVM)
try (Transaction transaction = graphDB.beginTx()) {
for (Node node: graphDB.findNodesByLabelAndProperty(
DynamicLabel.label("CHARACTER"),
"name",
"Homer Simpson")) {
/* do something very useful */
}
transaction.success();
}
![Page 49: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/49.jpg)
Node CRUD (JVM)
try (Transaction transaction = graphDB.beginTx()) {
for (Node node: graphDB.findNodesByLabelAndProperty(
DynamicLabel.label("CHARACTER"),
"name",
"Homer Simpson")) {
/* do something very useful */
}
transaction.success();
}
Gotchas
● avoid graphDB.findById !!!
● transaction is MANDATORY for reads as well (new in 2.0)
![Page 50: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/50.jpg)
Node CRUD (JVM)
try (Transaction transaction = graphDB.beginTx()) {
Node character = /*lookup*/;
character.delete();
transaction.success();
}
![Page 51: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/51.jpg)
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
![Page 52: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/52.jpg)
Relationship CRUD (JVM)
try (Transaction transaction = graphDB.beginTx()) {
Node homer = /*lookup*/;
Node doughnut = /*lookup*/;
Relationship eating = homer.createRelationshipTo(
doughnut,
DynamicRelationshipType .withName("LOVES_EATING")
);
eating.setProperty("quantity", Long.MAX_VALUE);
transaction.success();
}
![Page 53: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/53.jpg)
Relationship CRUD (JVM)
try (Transaction transaction = graphDB.beginTx()) {
Node homer = /*lookup*/;
Node doughnut = /*lookup*/;
Relationship eating = homer.createRelationshipTo(
doughnut,
DynamicRelationshipType.withName("LOVES_EATING")
);
eating.setProperty("quantity", Long.MAX_VALUE);
transaction.success();
}
unrelated to
Node labels
![Page 54: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/54.jpg)
Relationship CRUD (JVM)
try (Transaction transaction = graphDB.beginTx()) {
Node homer = /*lookup*/;
Node doughnut = /*lookup*/;
Relationship eating = homer.createRelationshipTo(
doughnut,
DynamicRelationshipType .withName("LOVES_EATING")
);
eating.setProperty("quantity", Long.MAX_VALUE);
transaction.success();
}Gotchas
● relationship direction matters at query time
● avoid human-eating doughnuts ;-)
![Page 55: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/55.jpg)
Relationship CRUD (JVM)
try (Transaction transaction = graphDB.beginTx()) {
Relationship relationship = /*lookup*/;
relationship.delete();
transaction.success();
}
![Page 56: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/56.jpg)
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
![Page 57: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/57.jpg)
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
![Page 58: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/58.jpg)
QUERYING
DATA
![Page 59: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/59.jpg)
Neo4J anatomy
![Page 60: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/60.jpg)
Two strategies
IMPERATIVE
VERY extensive
Totally customizable
100% under your
responsability
DECLARATIVE
VERY intuitive
90% of your needs
No free lunch (yet)!
Might not be as performant
![Page 61: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/61.jpg)
Two strategies
TRAVERSALS CYPHER QL
(/GREMLIN)
![Page 62: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/62.jpg)
Traversals
DEPTH FIRST BREADTH FIRST
![Page 63: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/63.jpg)
Traversals
![Page 64: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/64.jpg)
Traversal - basic git log
try (Transaction transaction = graphDB.beginTx()) {
for (Path position : Traversal.description()
.depthFirst()
.evaluator(toDepth(LOG_DEFAULT_SIZE))
.relationships(
DynRelType.withName("PARENT_COMMIT"),
INCOMING
).traverse(headCommit)) {
Node currentNode = position.endNode;
logs.add(currentNode);
}
transaction.success();
}
![Page 65: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/65.jpg)
Traversal - basic git log
try (Transaction transaction = graphDB.beginTx()) {
for (Path position : Traversal.description()
.depthFirst()
.evaluator(toDepth(LOG_DEFAULT_SIZE))
.relationships(
DynRelType.withName("PARENT_COMMIT"),
Direction.INCOMING
).traverse(headCommit)) {
Node currentNode = position.endNode;
logs.add(currentNode);
}
transaction.success();
}
lazy traversal
definition
![Page 66: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/66.jpg)
Traversal - basic git log
try (Transaction transaction = graphDB.beginTx()) {
for (Path position : Traversal.description()
.depthFirst()
.evaluator(toDepth(LOG_DEFAULT_SIZE))
.relationships(
DynRelType.withName("PARENT_COMMIT"),
INCOMING
).traverse(headCommit)) {
Node currentNode = position.endNode;
logs.add(currentNode);
}
transaction.success();
}
start traversal
with node
![Page 67: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/67.jpg)
Traversal - basic git log
try (Transaction transaction = graphDB.beginTx()) {
for (Path position : Traversal.description()
.depthFirst()
.evaluator(toDepth(LOG_DEFAULT_SIZE))
.relationships(
DynRelType.withName("PARENT_COMMIT"),
INCOMING
).traverse(headCommit)) {
Node currentNode = position.endNode;
logs.add(currentNode);
}
transaction.success();
}
keeps track of
current position
& visited nodes/rels
![Page 68: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/68.jpg)
Traversals
Extensive
but verbose
and error-prone
WE <3 ASCII ART!
![Page 69: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/69.jpg)
Pattern matching and ASCII art
WE <3
CYPHER
![Page 70: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/70.jpg)
Pattern matching and ASCII art
![Page 71: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/71.jpg)
Pattern matching and ASCII art
![Page 72: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/72.jpg)
Pattern matching and ASCII art
![Page 73: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/73.jpg)
Pattern matching and ASCII art
![Page 74: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/74.jpg)
Pattern matching and ASCII art
![Page 75: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/75.jpg)
Pattern matching and ASCII art
![Page 76: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/76.jpg)
Cypher syntax with <3
Cypher
()-->()
![Page 77: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/77.jpg)
Cypher
(A)-->(B)
Cypher syntax with <3
![Page 78: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/78.jpg)
Cypher
(A)-[:LOVES]->(B)LOVES
Cypher syntax with <3
![Page 79: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/79.jpg)
Cypher
(C)<--(A)-->(B)-->(C)
A-->B-->C,A-->C
Cypher syntax with <3
![Page 80: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/80.jpg)
Cypher reads
START <lookup> (optional)
MATCH <pattern>
WHERE <filtering>
RETURN <expression>
![Page 81: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/81.jpg)
Cypher reads
MATCH (homer:HUMAN)-[:LOVES_EATING]->(doughnut:FOOD)
WHERE homer.name = "Homer Simpson"
AND doughnut.brand = "Fattylicious!"
RETURN homer
![Page 82: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/82.jpg)
Cypher reads
MATCH (sugg:CONTACT)-[:IN_CONTACT*2..10]-(me:CONTACT)
WHERE me.name = "Florent Biville"
AND me <> sugg
RETURN me, sugg
![Page 83: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/83.jpg)
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”
![Page 84: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/84.jpg)
Cypher writes
CREATE (UNIQUE) <expression>
MERGE <expression>
![Page 85: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/85.jpg)
Cypher writes
CREATE (homer:HUMAN:DAD {name: "Homer Simpson"})
RETURN homer
![Page 86: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/86.jpg)
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
![Page 87: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/87.jpg)
Cypher writes
MERGE (keanu:ACTOR {name:'Keanu Reeves'})
ON CREATE keanu
SET keanu.created = timestamp()
ON MATCH keanu
SET keanu.lastSeen = timestamp()
RETURN keanu
![Page 88: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/88.jpg)
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!
![Page 89: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/89.jpg)
OBJECT-GRAPH
MAPPING
![Page 90: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/90.jpg)
With...
![Page 91: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/91.jpg)
Spring Data
History ~2010
Rod Johnson, Scala last poet
Emil Eifrem, Neo Tech. founder & CEO
![Page 92: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/92.jpg)
Spring Data
Familiar model for Spring apps
THIN common layer
Embraces diversity MongoDB
Redis
Neo4J
ElasticSearch…
Current version 2.3.1.RELEASE
![Page 93: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/93.jpg)
Vanilla Neo4J repositories with Spring
@Repository
public class BranchRepository {
public Relationship createBranch(Node p, Node c, Map<String,?> props) {
try (Transaction transaction = graphDB.beginTx()) {
Relationship relationship = p.createRelationshipTo(
c,
DynamicRelationshipType .name("HAS_BRANCH")
);
for (Entry<String,?> entry:props.entrySet()) {
relationship.setProperty(entry .getKey(), entry.getValue());
}
transaction.success();
return relationship;
}}}
![Page 94: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/94.jpg)
Vanilla Neo4J repositories with Spring
@Repository
public class BranchRepository {
public Relationship createBranch(Node p, Node commit, Map<String,?> props)
{
try (Transaction transaction = graphDB.beginTx()) {
Relationship relationship = p.createRelationshipTo(
commit,
DynamicRelationshipType .name("HAS_BRANCH")
);
for (Entry<String,?> entry:props.entrySet()) {
relationship.setProperty(entry .getKey(), entry.getValue());
}
transaction.success();
return relationship;
}}}
![Page 95: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/95.jpg)
Spring Data Neo4J repositories
public interface BranchRepository extends GraphRepository< Branch> {
// look ma! no code!
}
![Page 96: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/96.jpg)
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" )
Page<Branch> lookMaIveGotPages ();
Branch findByNameAndCommitIdentifierLike (String name, String commit);
}
![Page 97: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/97.jpg)
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" )
Page<Branch> lookMaIveGotPages ();
Branch findByNameAndCommitIdentifierLike (String name, String commit);
}
Cool things
● boilerplate methods already provided
● you declare methods following a naming convention, Spring Data Neo4J
generates the right implementation for ya!
● YOU EXPOSE YOUR DOMAIN, no Nodes, no Relationships!
![Page 98: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/98.jpg)
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;
}
![Page 99: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/99.jpg)
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
*/
}
![Page 100: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/100.jpg)
Neo4jTemplate
Geospatial queries
Cross-store support
Dynamic relationships
“Advanced” mapping
And much more
![Page 101: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/101.jpg)
Conclusion
![Page 102: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/102.jpg)
So much to talk about, so little time
● moaaar Cypher
● REST
○ standard API
○ unmanaged extensions
○ streaming
● Tinkerpop abstractions, http://www.tinkerpop.com/
● dataviz
○ auto : http://linkurio.us/, Neoclipse, Gephi
○ custom : d3.js, sigma.js…● NeoAAS : http://www.graphenedb.com/, Heroku
● misc. : backup, batch-import, JDBC drivers
![Page 103: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/103.jpg)
And one more thing
AssertJ-Neo4J 1.0 is coming soon!
Fluent test assertions for Neo4J
https://github.com/joel-costigliola/assertj-neo4j
![Page 104: Soft Shake Event / A soft introduction to Neo4J](https://reader033.fdocuments.net/reader033/viewer/2022042813/547c6ad4b479598e508b4670/html5/thumbnails/104.jpg)