The document provides an introduction and agenda for a Neo4j workshop about modeling Game of Thrones data. It discusses loading Game of Thrones character, episode and house data into Neo4j and demonstrates different Cypher queries to analyze relationships between these entities such as determining the most prominent characters or how houses are related. The workshop also covers more advanced topics like aggregation queries using the WITH clause and deleting or constraining data.
3. 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
4.
5.
6.
7. They GoT the title wrong ...
There maybe spoilers.. [Maybe]
10. 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?
11. A quick show of hands...
How many people have used
databases before?
12. A quick show of hands...
How many people have used
Neo4j before?
13. A quick show of hands...
How many people have
watched Game of Thrones?
14. 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
25. What is Cypher?
‣ The graph query language
‣ Declarative and based on finding patterns
‣ ASCII Art + Query Language = Cypher
26. 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
27. 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!
28. Our first graph
‣ Navigate to http://localhost:7474
‣ Type the following into the pane at the top:
:play http://guides.neo4j.com/got
33. Unique constraints
We create unique constraints to:
‣ ensure uniqueness
‣ allow fast lookup of nodes which match
these (label,property) pairs.
34. 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
39. Deleting data
The DELETE command allows us to delete nodes
and relationships...but we can’t delete nodes if
they still have relationships
40. Deleting data
The DELETE command allows us to delete nodes
and relationships...but we can’t delete nodes if
they still have relationships
MATCH (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.
41. 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
42. Deleting schema
We can delete constraints as well by changing
‘CREATE’ to ‘DROP’ in the command
CREATE CONSTRAINT ON (c:Character)
ASSERT c.name IS UNIQUE;
DROP CONSTRAINT ON (c:Character)
ASSERT c.name IS UNIQUE;
45. 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)
48. 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.
49. 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
55. Let’s talk about joins
We can use the WITH clause to work around this
problem
56. 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.
57. 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
58. 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
63. Resources
HBO 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