Intro to Neo4j JavaLand Brühl March 2015
Michael Hunger [email protected] @mesirii @neo4j
(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
(c) Neo Technology, Inc 2014 Why Graphs?
(c) Neo Technology, Inc 2014 The World is a Graph
(c) Neo Technology, Inc 2014 Name ONE disconnected thing or piece of information!
(c) Neo Technology, Inc 2014 (c) Neo Technology, Inc 2014 Interaction Creation Influences Dependence Create Connections
(c) Neo Technology, Inc 2014 Some Use-Cases
(c) Neo Technology, Inc 2014 (c) Neo Technology, Inc 2014 What do you need? A whiteboard!
(c) Neo Technology, Inc 2014 Whiteboard Friendlyness Easy to design and model direct representation of the model
(c) Neo Technology, Inc 2014 (c) Neo Technology, Inc 2014 Tom Hanks Hugo Weaving
ACTED_IN ACTED_IN ACTED_IN
The Matrix Cloud Atlas DIRECTED
DIRECTED Lana Wachowski
(c) Neo Technology, Inc 2014 Person Actor Person Actor
name: Tom Hanks name: Hugo Weaving born: 1956 born: 1960
ACTED_IN ACTED_IN roles: Bill Smoke roles: Agent Smith ACTED_IN roles: Zachry
Movie
Movie title: The Matrix released: 1999 title: Cloud Atlas released: 2012
DIRECTED Person Director
DIRECTED name: Lana Wachowski born: 1965
(c) Neo Technology, Inc 2014 (c) Neo Technology, Inc 2014 What Is A Graph, Anyway?
(c) Neo Technology, Inc 2014 A Graph
Node
Relationship
(c) Neo Technology, Inc 2014 Four Graph Model Building Blocks
(c) Neo Technology, Inc 2014 Property Graph Data Model
(c) Neo Technology, Inc 2014 Nodes
(c) Neo Technology, Inc 2014 Rela onships
(c) Neo Technology, Inc 2014 Rela onships (con nued)
Nodes can be connected by more than one rela onship
Nodes can have more than one rela onship
Self rela onships are allowed
(c) Neo Technology, Inc 2014 Labels
(c) Neo Technology, Inc 2014 Four Building Blocks ๏ Nodes • En es ๏ Rela onships • Connect en es and structure domain ๏ Proper es • A ributes and metadata ๏ Labels • Group nodes by role
(c) Neo Technology, Inc 2014 The NoSQL Space
(c) Neo Technology, Inc 2014 Relational to Graph
(c) Neo Technology, Inc 2014 Relational to Graph Relational is simple until it gets complicated ...
table1 join-table table2
27
(c) Neo Technology, Inc 2014 Relational to Graph You know relational now consider relationships...
actors actor_movie movies
28
(c) Neo Technology, Inc 2014 Looks different. 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
# persons query time
Relational database 1.000 2000ms
Neo4j 1.000 2ms
Neo4j 1.000.000 2ms
29 (c) Neo Technology, Inc 2014 Neo4j is a Graph Database
(c) Neo Technology, Inc 2014 Neo4j is a Graph Database
(c) Neo Technology, Inc 2014 Graph Database: Why ?
•Powerful data model, as general as RDBMS •Whiteboard friendly, agile development •Fast, for connected data •Easy to query
(c) Neo Technology, Inc 2014 Graph Database: Why Not?
•Global Queries / Number Crunching •Binary Data / Blobs •Requires conceptual shift •graph-like thinking is addictive
(c) Neo Technology, Inc 2014 Graph Querying
(c) Neo Technology, Inc 2014 You know how to query a Relational Database?
(c) Neo Technology, Inc 2014 Just use SQL
select movie.title from actors join actor_movie on (...) join movies on (...) where actors.name = "Andreas"
actors actor_movie movies 36
(c) Neo Technology, Inc 2014 How to query a graph?
(c) Neo Technology, Inc 2014 You traverse the graph
// find starting points MATCH (:Person {name:'Andreas'}) // then traverse the relationships -[:ACTED_IN]->(movie:Movie) <-[:ACTED_IN]- (other:Person) // and return results RETURN other
Andreas
38
(c) Neo Technology, Inc 2014 Cypher a pattern-matching query language for graphs
(c) Neo Technology, Inc 2014 Cypher attributes
#1 Declarative You tell Cypher what you want, not how to get it
40
(c) Neo Technology, Inc 2014 Cypher attributes
#2 Expressive Optimize syntax for reading
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie) RETURN a.name, r.role, m.title
41
(c) Neo Technology, Inc 2014 Cypher attributes
#3 Pattern Matching Patterns are easy for your human brain
42
(c) Neo Technology, Inc 2014 Graph Query Examples
(c) Neo Technology, Inc 2014 Social Recommendation
(c) Neo Technology, Inc 2014 (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
http://maxdemarzi.com/?s=facebook (c) Neo Technology, Inc 2014 * Cypher query language example Query Structure
(c) Neo Technology, Inc 2014 Our Question!
Find all Actors Whose name starts with "T" Aggregate their activity and movie titles Who acted in more than 5 movies Return their name, age and movie-titles Ordered by number of movies Limited to top 10
(c) Neo Technology, Inc 2014 In SQL
SELECT a.name, a.age, group_concat(m.title) as movies, count(*) as cnt FROM actors as a JOIN actor_movie ON (a.id = actor_movie.actor_id) JOIN movies as m ON (actor_movie.movie_id = m.id) WHERE a.name LIKE "T%" GROUP BY a.name, a.age HAVING cnt > 5
(c) Neo Technology, Inc 2014 Cypher Query Structure
MATCH (a:Person)-[:ACTED_IN]->(m:Movie) WHERE a.name =~ "T.*" WITH a, count(m) AS cnt, collect(m.title) AS movies WHERE cnt > 5 RETURN a.name, a.age, movies ORDER BY length(movies) DESC LIMIT 10
(c) Neo Technology, Inc 2014 Breakdown
(c) Neo Technology, Inc 2014 MATCH describes the pattern
(c) Neo Technology, Inc 2014 MATCH - Pattern
MATCH (a:Person)-[:ACTED_IN]->(m:Movie) WHERE a.name =~ "T.*" WITH a, count(m) AS cnt, collect(m.title) AS movies WHERE cnt > 5 RETURN a.name, a.age, movies ORDER BY length(movies) DESC LIMIT 10
(c) Neo Technology, Inc 2014 WHERE filters the result set
(c) Neo Technology, Inc 2014 WHERE - filter
MATCH (a:Person)-[:ACTED_IN]->(m:Movie) WHERE a.name =~ "T.*" WITH a, count(m) AS cnt, collect(m.title) AS movies WHERE cnt > 5 RETURN a.name, a.age, movies ORDER BY length(movies) DESC LIMIT 10
(c) Neo Technology, Inc 2014 WITH computes intermediate results
(c) Neo Technology, Inc 2014 WITH - intermediate projection
MATCH (a:Person)-[:ACTED_IN]->(m:Movie) WHERE a.name =~ "T.*" WITH a, count(m) AS cnt, collect(m.title) AS movies WHERE cnt > 5 RETURN a.name, a.age, movies ORDER BY length(movies) DESC LIMIT 10
(c) Neo Technology, Inc 2014 RETURN returns the final results
(c) Neo Technology, Inc 2014 RETURN - results
MATCH (a:Person)-[:ACTED_IN]->(m:Movie) WHERE a.name =~ "T.*" WITH a, count(m) AS cnt, collect(m.title) AS movies WHERE cnt > 5 RETURN a.name, a.age, movies ORDER BY length(movies) DESC LIMIT 10
(c) Neo Technology, Inc 2014 ORDER BY LIMIT SKIP sort and paginate
(c) Neo Technology, Inc 2014 ORDER BY LIMIT - Paginate
MATCH (a:Person)-[:ACTED_IN]->(m:Movie) WHERE a.name =~ "T.*" WITH a, count(m) AS cnt, collect(m.title) AS movies WHERE cnt > 5 RETURN a.name, a.age, movies ORDER BY length(movies) DESC LIMIT 10
(c) Neo Technology, Inc 2014 Collections powerful datastructure handling
(c) Neo Technology, Inc 2014 Collections
MATCH (a:Person)-[:ACTED_IN]->(m:Movie) WHERE a.name =~ "T.*" WITH a, count(m) AS cnt, collect(m.title) AS movies WHERE cnt > 5 RETURN a.name, a.age, movies ORDER BY length(movies) DESC LIMIT 10
(c) Neo Technology, Inc 2014 More Cypher
(c) Neo Technology, Inc 2014 UPDATE OPERATIONS create & update nodes, relationships and patterns
(c) Neo Technology, Inc 2014 INDEX, CONSTRAINTS represent optional schema
(c) Neo Technology, Inc 2014 Demo Neo4j Browser
(c) Neo Technology, Inc 2014 Neo4j from Java
(c) Neo Technology, Inc 2014 Java API
(c) Neo Technology, Inc 2014 Neo4j Java API
GraphDatabaseService gdb = new GraphDatabaseFactory().newEmbeddedDatabase(PATH); try(Transaction tx = gdb.beginTx()) { Node actor = db.findNode("name","Tom Hanks") for (Relationship role : actor.getRelationships(ACTED_IN,OUTGOING)) { Node movie = role.getEndNode(); result.add(movie.getProperty("name")); } tx.success(); }
(c) Neo Technology, Inc 2014 JDBC
(c) Neo Technology, Inc 2014 JDBC Driver
// Make sure Neo4j Driver is registered Class.forName("org.neo4j.jdbc.Driver");
// Connect Connection con = DriverManager.getConnection( "jdbc:neo4j://localhost:7474/");
// Query try(Statement stmt = con.createStatement()) { ResultSet rs = stmt.executeQuery( "MATCH (n:Person)-[:ACTED_IN]->… RETURN m.title");
while(rs.next()) { System.out.println(rs.getString("m.title")); } }
(c) Neo Technology, Inc 2014 Spring Data Neo4j
(c) Neo Technology, Inc 2014 Spring Data Neo4j
@NodeEntity class Person { @Indexed String name; @RelatedTo(type="ACTED_IN") Collection
(c) Neo Technology, Inc 2014 Neo4j from Java - Demo
(c) Neo Technology, Inc 2014 Brown Bag Lunch
• you bring 10+ colleagues • you provide a room with a projector + screen • we bring a bag lunch • we introduce Neo4j to your team in 45 min + 15 min for Q&A http://neo4j.com/brownbag Schedule your Neo4j Intro now!
Neo Technology, Inc Confidential Neo4j 2.0 Graph datenbank für alle
• Kompaktes Handbuch • Übersicht über Neo4j, APIs, Cypher • Praktische Anwendungsfälle und Beispiele • Genügend Details um direkt starten zu können
http://bit.ly/das-buch Buch-PDF gratis verfügbar.
Neo Technology, Inc Confidential How to get started? ๏ Full day Neo4j Training & Online Training ๏ Free e-Books • Graph Databases, Neo4j 2.0 (DE) ๏ neo4j.com • neo4j.com/developer ๏ docs.neo4j.org • Data Modeling Examples ๏ http://console.neo4j.org ๏ http://graphgist.neo4j.com ๏ Get Neo4j • http://neo4j.com/download ๏ Participate • http://groups.google.com/group/neo4j http://neo4j.meetup.com • 78
(c) Neo Technology, Inc 2014 Thank You Time for Questions!
(c) Neo Technology, Inc 2014