intro to neo4j - jug da · 2020-06-08 · intro to neo4j jug darmstadt november 2015 dirk mahler...

52
(c) Neo Technology, Inc 2014 Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler [email protected] @dirkmahler

Upload: others

Post on 16-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Intro to Neo4jJUG Darmstadt

November 2015

Dirk Mahler

[email protected]

@dirkmahler

Page 2: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Agenda

1. Why Graphs, Why Now?

2. What Is A Graph, Anyway?

3. Neo4j as a Graph Database

4. Graph Querying

1. Cypher

2. Examples

Page 3: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Why Graphs?

Page 4: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

The World is a Graph

Page 5: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Some Use-Cases

Page 6: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Social Network

Page 7: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

(Network) Impact Analysis

Page 8: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Logistics & Routing

Page 9: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Recommendations

Page 10: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Access Control

Page 11: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Fraud Analysis

Page 12: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

What Is A Graph,

Anyway?

Page 13: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

A Graph

Relationship

Node

Page 14: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Four Graph Model

Building Blocks

Page 15: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Property Graph Data Model

Page 16: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Nodes

Page 17: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Relationships

Page 18: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Relationships (continued)

Nodes can have more

than one relationship

Self relationships are allowed

Nodes can be connected by more

than one relationship

Page 19: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Labels

Page 20: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Four Building Blocks

๏ Nodes

• Entities

๏ Relationships

• Connect entities and structure domain

๏ Properties

• Attributes and metadata

๏ Labels

• Group nodes by role

Page 21: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Whiteboard

FriendlynessEasy to design and model

direct representation of the model

Page 22: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Page 23: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Tom HanksHugo Weaving

Cloud Atlas

The Matrix

LanaWachowski

ACTED_IN

ACTED_IN

ACTED_IN

DIRECTED

DIRECTED

Page 24: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

name: Tom Hanksborn: 1956

title: Cloud Atlasreleased: 2012

title: The Matrixreleased: 1999

name: Lana Wachowskiborn: 1965

ACTED_IN

roles: Zachry

ACTED_IN

roles: Bill Smoke

DIRECTED

DIRECTED

ACTED_IN

roles: Agent Smith

name: Hugo Weavingborn: 1960

Person

Movie

Movie

Person Director

ActorPerson Actor

Page 25: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Page 26: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Relational vs. GraphYou know relational

37

now consider relationships...

users skillsuser_skill

Page 27: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Looks different, fine. Who cares?

๏a sample social graph

•with ~1,000 persons

๏average 50 friends per person

๏pathExists(a,b) limited to depth 4

๏caches warmed up to eliminate disk I/O

38

# persons query time

Relational database 1.000 2000ms

Neo4j 1.000 2ms

Neo4j 1.000.000 2ms

Page 28: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Neo4j is a

Graph Database

Page 29: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Neo4j is a Graph Database

• A Graph Database:

• a schema-free labeled Property Graph

• perfect for complex, highly connected data

• A Graph Database:

• reliable with real ACID Transactions

• scalable: Billions of Nodes and Relationships, Scale

out with highly available Neo4j-Cluster

• fast with more than 2-4M traversals / second

• Server with HTTP API, or Embeddable on the JVM

• Declarative Query Language

Page 30: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Graph Database: Pros & Cons

• Strengths

• Powerful data model, as general as RDBMS

• Whiteboard friendly, agile development

• Fast, for connected data

• Easy to query

• Weaknesses:

• Sharding

• Global Queries / Number Crunching

• Binary Data / Blobs

• Requires conceptual shift

• graph-like thinking becomes addictive

Page 31: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Graph Querying

Page 32: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

You know how to query a

relational database!

Page 33: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Just use SQL

54users skillsuser_skills

select skills.name

from users join user_skills on (...) join skills on (...)

where users.name = “Michael“

Page 34: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

How to query a

graph?

Page 35: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

// find starting nodes

MATCH (me:Person {name:'Andreas'})

// then traverse the relationships

MATCH (me:Person {name:'Andreas'})-[:FRIEND]->(friend)

-[:FRIEND]->(friend2)

RETURN friend2

You traverse the graph

56

Page 36: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Cyphera pattern-matching

query language for graphs

Page 37: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Cypher attributes

#1 Declarative

You tell Cypher what you

want, not how to get it

58

Page 38: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Cypher attributes

#2 Expressive

Optimize syntax for reading

59

MATCH (a:Actor)-[r:ACTS_IN]->(m:Movie)

RETURN a.name, r.role, m.title

Page 39: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Cypher attributes

#3 Pattern Matching

Patterns are easy for your

human brain

60

Page 40: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Cypher attributes

#4 Idempotent

State change should be

expressed idempotently

61

Page 41: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Graph Query

Examples

Page 42: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Social

Recommendation

Page 43: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Page 44: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

MATCH (person:Person)-[:IS_FRIEND_OF]->(friend),

(friend)-[:LIKES]->(restaurant),

(restaurant)-[:LOCATED_IN]->(loc:Location),

(restaurant)-[:SERVES]->(type:Cuisine)

WHERE person.name = 'Philip' AND loc.location='New York' AND

type.cuisine='Sushi'

RETURN restaurant.name

* Cypher query language examplehttp://maxdemarzi.com/?s=facebook

Page 45: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Page 46: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Network

Management

Example

Page 47: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Network Management - Create

CREATE

(crm {name:"CRM"}),

(dbvm {name:"Database VM"}),

(www {name:"Public Website"}),

(wwwvm {name:"Webserver VM"}),

(srv1 {name:"Server 1"}),

(san {name:"SAN"}),

(srv2 {name:"Server 2"}),

(crm)-[:DEPENDS_ON]->(dbvm),

(dbvm)-[:DEPENDS_ON]->(srv2),

(srv2)-[:DEPENDS_ON]->(san),

(www)-[:DEPENDS_ON]->(dbvm),

(www)-[:DEPENDS_ON]->(wwwvm),

(wwwvm)-[:DEPENDS_ON]->(srv1),

(srv1)-[:DEPENDS_ON]->(san)

Practical Cypher

Page 48: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Network Management - Impact Analysis

// Server 1 Outage

MATCH (n)<-[:DEPENDS_ON*]-(upstream)

WHERE n.name = "Server 1"

RETURN upstream

Practical Cypher

upstream

{name:"Webserver VM"}

{name:"Public Website"}

Page 49: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Network Management - Dependency Analysis

// Public website dependencies

MATCH (n)-[:DEPENDS_ON*]->(downstream)

WHERE n.name = "Public Website"

RETURN downstream

Practical Cypher

downstream

{name:"Database VM"}

{name:"Server 2"}

{name:"SAN"}

{name:"Webserver VM"}

{name:"Server 1"}

Page 50: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Network Management - Statistics

// Most depended on component

MATCH (n)<-[:DEPENDS_ON*]-(dependent)

RETURN n,

count(DISTINCT dependent)

AS dependents

ORDER BY dependents DESC

LIMIT 1

Practical Cypher

n dependents

{name:"SAN"} 6

Page 51: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

๏ Full day Neo4j Training & Online Training

๏ Free e-Books

• Graph Databases, Learning Neo4j

๏ http://neo4j.com

๏ http://neo4j.com/developer

๏ http://neo4j.com/docs

๏ http://gist.neo4j.org

๏ Get Neo4j

• http://neo4j.com/download

๏ Participate

• http://groups.google.com/group/neo4j

• http://neo4j.meetup.com

• a session like this one ;)

How to get started?

114

Page 52: Intro to Neo4j - JUG DA · 2020-06-08 · Intro to Neo4j JUG Darmstadt November 2015 Dirk Mahler ... Agenda 1. Why Graphs, Why Now? 2. What Is A Graph, Anyway? 3. Neo4j as a Graph

(c) Neo Technology, Inc 2014

Thank YouTime for Questions!