Upload
neo4j-the-fastest-and-most-scalable-native-graph-database
View
628
Download
1
Embed Size (px)
Citation preview
Intro to Neo4j
@praveenasekhar / @markneedham
<3
Tweet us your pictures
@neo4j @WomenWhoCode
Welcome!
Logistics
‣ Wi-fi• network: LNVisitor• user/password: LNGuest / LNGuest
‣ Grab a USB key from one of the tables or download from neo4j.com/download
‣ Install Neo4j 3.0.3
They GoT the title wrong ...
There maybe spoilers.. [Maybe]
Example
Game of Thrones Wiki
By the end you will help us work out
‣ Who is the most prominent character in a season?
‣ Which house features the most characters? ‣ How are Starks and Targaryens related?
A quick show of hands...
How many people have used databases before?
A quick show of hands...
How many people have used Neo4j before?
A quick show of hands...
How many people have watched Game of Thrones?
Whiteboard / Post-It Exercise
Everyone!
List down different types of entities and relationships that tie them in the Game of Thrones universe!
http://gameofthrones.wikia.com/wiki/Game_of_Thrones_Wiki
‣ Actors‣ Characters‣ Allegiances‣ Houses‣ Episodes‣ Locations
What data do we have?
‣ An introduction to Neo4j and the Cypher query language
‣ GoT characters and episodes‣ GoT houses ‣ GoT family relationships
Agenda
Example
A brief into to the graph data model
GoT graph model
Nodes
Labels
Properties
Relationships
Introducing Cypher
What is Cypher?
‣ The graph query language
‣ Declarative and based on finding patterns
‣ ASCII Art + Query Language = Cypher
Nodes
() or (n)
● Surrounded with parentheses
● Use an alias to refer to our node later
(n:Label)● Specify a Label - grouping nodes by roles or types
● Think of Labels like "Tables"++
(n:Label {prop: 'value'})● Nodes can have properties
Relationships
--> or -[:TYPE]->
● Hyphenated & encased in square brackets● Like labels, a relationship type starts with a colon :● < > Specify the direction of the relationship
-[:TYPE {propertyName: "value"}]->
● Relationships can have properties too!
Our first graph
‣ Navigate to http://localhost:7474 ‣ Type the following into the pane at the top::play http://guides.neo4j.com/got
Follow AlongRun the first guide with us
When there are slides to see
ContinueContinue with the Guide
Take NoteConstraints
Unique constraints
We create unique constraints to:
‣ ensure uniqueness ‣ allow fast lookup of nodes which match
these (label,property) pairs.
Unique constraints
We create unique constraints to:
‣ ensure uniqueness ‣ allow fast lookup of nodes which match
these (label,property) pairs.CREATE CONSTRAINT ON (c:Character)
ASSERT c.name IS UNIQUE
ContinueContinue with the Guide
End of Section 1Continue with the Guide
Follow AlongRun the next guide with us
Take NoteDeleting
Deleting data
The DELETE command allows us to delete nodes and relationships...but we can’t delete nodes if they still have relationships
Deleting data
The DELETE command allows us to delete nodes and relationships...but we can’t delete nodes if they still have relationshipsMATCH (n)
DELETE n
org.neo4j.kernel.api.exceptions.ConstraintViolationTransactionFailureException:
Cannot delete node<0>, because it still has relationships. To delete this node, you
must first delete its relationships.
Deleting data
The DETACH DELETE command is our friend when we want to delete a node and all relationships attached to it.MATCH (n)
DETACH DELETE n
Deleting schema
We can delete constraints as well by changing ‘CREATE’ to ‘DROP’ in the commandCREATE CONSTRAINT ON (c:Character)
ASSERT c.name IS UNIQUE;
DROP CONSTRAINT ON (c:Character)
ASSERT c.name IS UNIQUE;
Follow AlongContinue with the guide
Take NoteLOAD CSV
LOAD CSV
‣ Import data from a HTTP or file URI• LOAD CSV WITH HEADERS FROM "file://" AS row
LOAD CSV WITH HEADERS FROM "http://" AS row
‣ Transform and convert CSV values• MATCH (node1:Label {property: row.propertyValue})
CREATE (node2:Label {property: row.propertyValue})
MERGE (node3:Label {property: row.propertyValue})
• CREATE (node1)-[:REL_TYPE]->(node2)
MERGE (node1)-[:REL_TYPE]->(node2)
ContinueContinue with the Guide
Take NoteAggregation queries
Aggregation queries
We don’t specify a GROUP BY key when using aggregation functions. The key is all non aggregation fields in the RETURN statement.
Aggregation queries
We don’t specify a GROUP BY key when using aggregation functions. The key is all non aggregation fields in the RETURN statement.
MATCH (character:Character)-[:APPEARED_IN]->()
RETURN character.name, COUNT(*) AS appearances
ORDER BY appearances DESC
Follow AlongContinue with the guide
End of Section 2Let’s take a 5 minute break
Follow AlongOnto the next guide
Take NoteAdvanced aggregation queries
Let’s talk about joins
Houses Episodes
Characters
Houses Episodes
Characters
Let’s talk about joins
We can use the WITH clause to work around this problem
WITH
The WITH clause allows query parts to be chained together, piping the results from one to be used as starting points or criteria in the next.
WITH
It’s used to:
‣ limit the number of entries that are then passed on to other MATCH clauses.
‣ filter on aggregated values‣ separate reading from updating of the graph
WITH
MATCH (h:House)<-[:HAS_ALLEGIANCE_TO]-(c:Character)-[:APPEARED_IN]->()
RETURN c.id, c.name, COLLECT(h.name) AS houses, COUNT(*) AS appearances
ORDER BY appearances DESC
MATCH (h:House)<-[:HAS_ALLEGIANCE_TO]-(c:Character)
WITH c, COLLECT(h.name) as houses
MATCH (c)-[:APPEARED_IN]->()
RETURN c.id, c.name, houses, COUNT(*) AS appearances
ORDER BY appearances DESC
Follow AlongContinue with the guide
End of Section 3One more to go
Tweet us your pictures
@neo4j @WomenWhoCode
Thanks for coming!
That’s all for today!
Thanks for coming!
<3
ResourcesHBO Game of Thrones
Wikipedia
Family Tree House Stark
Game of Thones Wikia
Cool Interactive Infographic
http://www.fastcodesign.com/1671439/infographic-every-murder-in-game-of-thrones-in-just-90-seconds
https://www.washingtonpost.com/graphics/entertainment/game-of-thrones/
http://winteriscoming.net/2016/04/18/infographic-shows-that-death-has-been-good-for-game-of-thrones-ratings/
15 GoT Infographics
http://hauteslides.com/2011/05/game-of-thrones-infographic-illustrated-guide-to-houses-and-character-relationships/
Social Media Infographics
Betrayal Graph
Impressive GoT Railroad Map
Character Relationship Graph from Book 3 NLP Analytics