neo4j introduction - game of thrones

63
Intro to Neo4j @praveenasekhar / @markneedham <3

Upload: neo4j-the-fastest-and-most-scalable-native-graph-database

Post on 21-Apr-2017

628 views

Category:

Data & Analytics


1 download

TRANSCRIPT

Page 1: Neo4j Introduction  - Game of Thrones

Intro to Neo4j

@praveenasekhar / @markneedham

<3

Page 2: Neo4j Introduction  - Game of Thrones

Tweet us your pictures

@neo4j @WomenWhoCode

Welcome!

Page 3: Neo4j Introduction  - Game of Thrones

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

Page 4: Neo4j Introduction  - Game of Thrones
Page 5: Neo4j Introduction  - Game of Thrones
Page 6: Neo4j Introduction  - Game of Thrones
Page 7: Neo4j Introduction  - Game of Thrones

They GoT the title wrong ...

There maybe spoilers.. [Maybe]

Page 8: Neo4j Introduction  - Game of Thrones

Example

Page 9: Neo4j Introduction  - Game of Thrones

Game of Thrones Wiki

Page 10: Neo4j Introduction  - Game of Thrones

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?

Page 11: Neo4j Introduction  - Game of Thrones

A quick show of hands...

How many people have used databases before?

Page 12: Neo4j Introduction  - Game of Thrones

A quick show of hands...

How many people have used Neo4j before?

Page 13: Neo4j Introduction  - Game of Thrones

A quick show of hands...

How many people have watched Game of Thrones?

Page 14: Neo4j Introduction  - 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

Page 15: Neo4j Introduction  - Game of Thrones

‣ Actors‣ Characters‣ Allegiances‣ Houses‣ Episodes‣ Locations

What data do we have?

Page 16: Neo4j Introduction  - Game of Thrones

‣ An introduction to Neo4j and the Cypher query language

‣ GoT characters and episodes‣ GoT houses ‣ GoT family relationships

Agenda

Page 17: Neo4j Introduction  - Game of Thrones

Example

Page 18: Neo4j Introduction  - Game of Thrones

A brief into to the graph data model

Page 19: Neo4j Introduction  - Game of Thrones

GoT graph model

Page 20: Neo4j Introduction  - Game of Thrones

Nodes

Page 21: Neo4j Introduction  - Game of Thrones

Labels

Page 22: Neo4j Introduction  - Game of Thrones

Properties

Page 23: Neo4j Introduction  - Game of Thrones

Relationships

Page 24: Neo4j Introduction  - Game of Thrones

Introducing Cypher

Page 25: Neo4j Introduction  - Game of Thrones

What is Cypher?

‣ The graph query language

‣ Declarative and based on finding patterns

‣ ASCII Art + Query Language = Cypher

Page 26: Neo4j Introduction  - Game of Thrones

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

Page 27: Neo4j Introduction  - Game of Thrones

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!

Page 28: Neo4j Introduction  - Game of Thrones

Our first graph

‣ Navigate to http://localhost:7474 ‣ Type the following into the pane at the top::play http://guides.neo4j.com/got

Page 29: Neo4j Introduction  - Game of Thrones

Follow AlongRun the first guide with us

Page 30: Neo4j Introduction  - Game of Thrones

When there are slides to see

Page 31: Neo4j Introduction  - Game of Thrones

ContinueContinue with the Guide

Page 32: Neo4j Introduction  - Game of Thrones

Take NoteConstraints

Page 33: Neo4j Introduction  - Game of Thrones

Unique constraints

We create unique constraints to:

‣ ensure uniqueness ‣ allow fast lookup of nodes which match

these (label,property) pairs.

Page 34: Neo4j Introduction  - Game of Thrones

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

Page 35: Neo4j Introduction  - Game of Thrones

ContinueContinue with the Guide

Page 36: Neo4j Introduction  - Game of Thrones

End of Section 1Continue with the Guide

Page 37: Neo4j Introduction  - Game of Thrones

Follow AlongRun the next guide with us

Page 38: Neo4j Introduction  - Game of Thrones

Take NoteDeleting

Page 39: Neo4j Introduction  - Game of Thrones

Deleting data

The DELETE command allows us to delete nodes and relationships...but we can’t delete nodes if they still have relationships

Page 40: Neo4j Introduction  - Game of Thrones

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.

Page 41: Neo4j Introduction  - Game of Thrones

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

Page 42: Neo4j Introduction  - Game of Thrones

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;

Page 43: Neo4j Introduction  - Game of Thrones

Follow AlongContinue with the guide

Page 44: Neo4j Introduction  - Game of Thrones

Take NoteLOAD CSV

Page 45: Neo4j Introduction  - Game of Thrones

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)

Page 46: Neo4j Introduction  - Game of Thrones

ContinueContinue with the Guide

Page 47: Neo4j Introduction  - Game of Thrones

Take NoteAggregation queries

Page 48: Neo4j Introduction  - Game of Thrones

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.

Page 49: Neo4j Introduction  - Game of Thrones

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

Page 50: Neo4j Introduction  - Game of Thrones

Follow AlongContinue with the guide

Page 51: Neo4j Introduction  - Game of Thrones

End of Section 2Let’s take a 5 minute break

Page 52: Neo4j Introduction  - Game of Thrones

Follow AlongOnto the next guide

Page 53: Neo4j Introduction  - Game of Thrones

Take NoteAdvanced aggregation queries

Page 54: Neo4j Introduction  - Game of Thrones

Let’s talk about joins

Houses Episodes

Characters

Houses Episodes

Characters

Page 55: Neo4j Introduction  - Game of Thrones

Let’s talk about joins

We can use the WITH clause to work around this problem

Page 56: Neo4j Introduction  - Game of Thrones

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.

Page 57: Neo4j Introduction  - Game of Thrones

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

Page 58: Neo4j Introduction  - Game of Thrones

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

Page 59: Neo4j Introduction  - Game of Thrones

Follow AlongContinue with the guide

Page 60: Neo4j Introduction  - Game of Thrones

End of Section 3One more to go

Page 61: Neo4j Introduction  - Game of Thrones

Tweet us your pictures

@neo4j @WomenWhoCode

Thanks for coming!

Page 62: Neo4j Introduction  - Game of Thrones

That’s all for today!

Thanks for coming!

<3

Page 63: Neo4j Introduction  - Game of Thrones

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