SlideShare a Scribd company logo
NEO4J OPEN SOURCE GRAPH
DATABASE
Presented by Mark Maslyn – mmaslyn@msn.com to
Denver Open Source User Group (4/7/15) and
Graph Nerds of Boulder (5/14/15)
THREE PARTS TO THIS NEO4J PRESENTATION
• What Are Graph Databases and Why Are They
Useful
• Neo4J Application Modes and Java API Syntax
• Demo Animal Guessing Game using Neo4J
Decision Tree
WHAT ARE GRAPH DATABASES ?
• Graph Databases are a type of NoSQL ( non-
relational) database
• In a Graph Database entities (nouns) are
represented as nodes
• Relationships are represented by edges
connecting nodes
• Both nodes and relationships can have properties
FRIEND-OF EXAMPLE
FRIEND-OFFRIEND-OF
Properties:
Name: Mark M
Language: Java
Properties:
Name: Tom M
Language: Java
Properties:
Name: Tom F
Language: Scala
SOME COMPANIES AND INDUSTRIES WITH GRAPH
DATABASE APPLICATIONS
• Social Media (Facebook, LinkedIn, Twitter)
• Configuration Management (Assimilation
Systems)
• Retail Recommendation (Walmart)
• Resource Authorization (Telenor)
• Fraud Detection ( Banks and Credit Card
Companies)
• Online Education (Pearson)
• Bioinformatics (Bio4j)
WHERE GRAPH DATABASES ARE
ADVANTAGEOUS
• Can provide a more natural representation
for the data
• Can give a faster query response
• Recommendation engines
EXAMPLES WHERE A GRAPH REPRESENTATION
IS A NATURAL MODEL
FLIGHT ROUTE MAP – SF TO NY
3
4
1
2
1.5
2
3
1
1
3
1.5
FLYS-TO
FLYS-TO FLYS-TO
FLYS-TO
FLYS-TO
FLYS-TOFLYS-TO
FLYS-TO
FLYS-TO
FLYS-TO
TREE OF LIFE
http://oceanexplorer.noaa.gov/okeanos after Woese (1990)
TWO LEVEL PROTEIN NETWORK DIAGRAM
From CYTOSCAPE.ORG
Two Level Tree
p53
EXAMPLE OF QUERY SPEED-UP
FRIEND-OF-A-FRIEND-OF-A-FRIEND …
From Vukotic, et al (2014)
EXECUTION TIME RDBMS VS GRAPH DATABASE FOR
DIFFERENT DEPTHS OF FRIEND SEARCH (1000 FRIENDS)
Depth Execution Time (sec) Count Result
2 0.028 ~900
3 0.213 ~999
4 10.273 ~999
5 92.613 ~999
Depth Execution Time (sec) Count Result
2 0.04 ~900
3 0.06 ~999
4 0.07 ~999
5 0.07 ~999
MYSQL RDMS
NEO4J
From Vukotic, et al (2014)
EXAMPLES OF RECOMMENDATIONS BASED
ON GRAPH CONNECTIONS AND CONTENT
TYPICAL APPROACHES TO GENERATING
RECOMMENDATIONS (AND PREDICTIONS)
• COLLABORATIVE FILTERING – Recommendations based on
common attributes between you and / or your connections –
Online dating model
• CONTENT BASED FILTERING - Deriving a second level of
information from the data and use that to derive
recommendations – Content classification model
• GRAPH TOPOLOGY – Infer links based on graph topology.
COLLABORATIVE FILTERING RECOMMENDATION BASED ON
FRIENDS CHOICES – I WILL LIKE WHAT MY FRIENDS LIKE
FRIEND-OFFRIEND-OF
Properties:
Name: Mark M
Language: Java
Properties:
Name: Tom M
Language: Java, Scala
Movie: Empire Strikes Back
Properties:
Name: Tom F
Language: Scala, Java
Movie: Raiders of the Lost Ark
CONTENT BASED RECOMMENDATIONS BASED ON
CLASSIFICATION OF FRIENDS MOVIE SELECTION
FRIEND-OFFRIEND-OF
Properties:
Name: Mark M
Language: Java
Properties:
Name: Tom M
Language: Java, Scala
Movie: Empire Strikes Back
Properties:
Name: Tom F
Language: Scala, Java
Movie: Raiders of the Lost Ark
CONTENT CLASSIFICATION BASED ON TERMS
SPORTS CATEGORY
FOOTBALL BASEBALL
Broncos
Seahawks
Ronnie HillmanPeyton Manning
Rockies
1 .. N SPORTS
Russell Wilson
CATEGORY
SPORT
TEAM
PLAYER
1 .. N TEAMS
1 .. N PLAYERS
FINDING RELATIONSHIPS BASED ON GRAPH TOPOLOGY
FRIEND-OFFRIEND-OF
Transitive Relationship
A FRIEND OF B, B FRIEND OF C, Therefore High
Probability A FRIEND OF C
CONCLUSION: A TRIADIC CLOSURE OR MY FRIENDS ARE
LIKELY TO BE FRIENDS WITH EACH OTHER
FRIEND-OFFRIEND-OF
Properties:
Name: Mark M
Language: Java
Properties:
Name: Tom M
Language: Java
Properties:
Name: Tom F
Language: Scala
TRIADIC CLOSURE
FRIEND RELATIONSHIPS BETWEEN PARTNERS IN A
KARATE CLUB – BEFORE SPLIT INTO TWO CLUBS
From Zachary (1977)
KARATE CLUBS MEMBERSHIP AFTER SPLIT
From Zachary (1977)
PART II – NEO4J GRAPH DATABASE MODES
AND JAVA API SYNTAX
NEO4J GRAPH DATABASE MODES
• Embedded Mode Uses Local NEO4J Jar Files
• Server Mode Uses RESTFul API’s
• Browser Client Mode Using CYPHER Query
Language
EMBEDDED MODE WITH JAVA API – OPENING
A GRAPH DATABASE
private static GraphDatabaseService graphDb;
private static String ANIMAL_DATABASE_LOC =
"/home/ubuntu/animal_game/animal.db";
public AnimalGame() {
// open the graph database
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(
ANIMAL_DATABASE_LOC);
// enable clean db shutdown on Ctrl-C
registerShutdownHook( graphDb );
}
JAVA API – CREATING AN INDEX
// create index using a combination of the node label and
the name property
try (Transaction tx = graphDb.beginTx()) {
graphDb.schema().indexFor(NodeLabels.PERSON).on(
"name").create();
tx.success();
}
JAVA API – CREATING A DATABASE NODE AND
SETTING PROPERTY VALUES
// create node
Node node = graphDb.createNode();
// add label to identify what group the node belongs to
node.addLabel(nodeLabel);
// set property values
node.setProperty(AnimalConstants.NAME, name);
node.setProperty(AnimalConstants.TEXT, text);
node.setProperty(AnimalConstants.TYPE, nodeType);
// make a list of possible answers (answerList)
// these match with relationships
node.setProperty(AnimalConstants.ANSWERS, answerList);
JAVA API – CREATING RELATIONSHIPS
BETWEEN NODES
public enum RelTypes implements RelationshipType {
YES,
NO
}
public void createRelationship(Node node1,
Node node2,
RelTypes rt) {
// relationship between node1 and node2
// direction is from node1 to node2
node1.createRelationshipTo((Node) node2, rt);
}
JAVA API - QUERYING THE DATABASE FOR A
NODE BY LABEL AND NAME
public Node getNodeByLabelAndName(String nodeName) {
Node node = null;
ResourceIterable<Node>nodeList =
graphDb.findNodesByLabelAndProperty(
NodeLabels.PERSON, "name", nodeName);
If (nodeList != null) {
try (Transaction tx = graphDb.beginTx()) {
for ( Node nodeL : nodeList )
node = nodeL;
tx.success();
}
}
return node;
}
NEO4J SERVER RESTFUL API
• Neo4J Server Provides Fine-Grained REST Calls
• Returns JSON Content Type
• Higher Level Libraries Available for Java, .NET, Python,
etc. to Wrap the Lower Level Calls
NEO4J SERVER RESTFUL API REQUEST /
RESPONSE WITH DETAILED AVAILABLE URL’S
RESPONSE
{
"extensions": {},
"node": "http://localhost:7474/db/data/node",
"node_index": "http://localhost:7474/db/data/index/node",
"relationship_index": "http://localhost:7474/db/data/index/relationship",
"extensions_info": "http://localhost:7474/db/data/ext",
"relationship_types": "http://localhost:7474/db/data/relationship/types",
"batch": "http://localhost:7474/db/data/batch",
"cypher": "http://localhost:7474/db/data/cypher",
"indexes": "http://localhost:7474/db/data/schema/index",
"constraints": "http://localhost:7474/db/data/schema/constraint",
"transaction": "http://localhost:7474/db/data/transaction",
"node_labels": "http://localhost:7474/db/data/labels",
"neo4j_version": "2.1.7"
}
REQUEST (GET): http://localhost:7474/db/data
NEO4J SERVER RESTFUL API – DRILLING DOWN FOR
META-DATA FROM ANIMAL DATABASE
REQUEST: http://localhost:7474/db/data/relationship/types
JSON RESPONSE:
[
"YES",
"NO"
]
RETRIEVING A SINGLE NODE AND ITS PROPERTIES
REQUEST: http://localhost:7474/db/data/node/0
JSON RESPONSE:
{
…..
"metadata": {
"id": 0,
"labels": [
“ANIMALS"
]
},
"data": {
"text": "Does the animal live on land ?",
"name": "start",
"answers": [
"Y/YES",
"N/NO",
"Q/QUIT"
],
"type": "question"
}
}
RETRIEVING A NODE USING THE RESTFUL LIBRARY
FROM JAVA CODE
private static GraphDatabaseService graphDb;
private static String ANIMAL_DATABASE_URL =
“http://localhost:7474/db/data”;
// public constructor
public AnimalGame() {
// connect to the graph database server
graphDb = new RestGraphDatabase(
ANIMAL_DATABASE_URL);
// wrap the request and retrieve the node
Node startNode =
graphDb.findNodesByLabelAndProperty(
NODE_LABEL,
“name”,
“start”);
NEO4J BROWSER BASED WEB ADMIN CLIENT
• Browser Connects to Neo4J Server.
• Graphically Displays Database Nodes and Relationships in a
Dashboard. Can Drill Down on Each Node.
• Allows User to Execute Cypher Queries on Database From
the Browser.
NEO4J BROWSER WEB ADMIN DASHBOARD
CLICK ON A NODE FOR MORE INFO
CYPHER QUERY LANGUAGE
• Cypher is Neo4J’s version of SQL
• Queries can be entered programmatically in Java
or through the browser interface
• Queries can retrieve nodes, relationships,
property values or call functions such as return
“shortest path traversal”
• Declarative syntax using “Ascii Art”
NODES - CYPHER SYNTAX
Cypher uses “Ascii Art” syntax that reflects graph elements. Nodes
are represented by parentheses “( )”, relationships by arrows “-->”.
The arrow head indicates the direction of the relationship
Adding constraints “find two Person group nodes a and b that are
connected by a relationship”.
Match (a) –-> (b)
Return a.name, b.name
Match (a:Person) --> (b:Person)
Return a.name, b.name
RELATIONSHIPS - CYPHER SYNTAX
Anonymous relationships match all relationship and are indicated
by the arrow alone
This query syntax is used to find two nodes a and b that are
connected by the specific “ACTED_IN” relationship to match
actors with movies
Match (a) --> (b)
Match (a) –[:ACTED_IN]-> (b)
PROPERTY GRAPH EXAMPLE FOR A MOVIE
DATABASE
SIX DEGREES OF KEVIN BACON GAME – HOW
MANY LINKS ARE REQUIRED TO CONNECT AN
ACTOR TO KEVIN BACON ?
Kevin Bacon (left) and Tom Hanks in Apollo 13 (1995)
CYPHER QUERY FOR SIX DEGREES OF KEVIN BACON
FOR MEG RYAN USING NEO4J SHORTEST PATH
FUNCTION
MATCH p=shortestPath(
(b:Person {name:"Kevin Bacon"})-[*]-(m:Person {name:"Meg
Ryan"})
)
RETURN p
AND THE ANSWER IS …
Meg Ryan’s “Bacon” Number is 2
NEO4J DECISION TREE – ANIMAL GUESSING GAME
Mammal ?
Has Stripes ? Slithers ?
Does it Growl ?
Has Trunk ?
Zebra ?Tiger ?
Elephant ?
Snake?
YES
YES
YES
YES
YES
NO
NO
NO
PART III
DEMO ANIMAL GUESSING GAME – AUDIENCE
VOLUNTEER REQUESTED
A Final Comment on Connected Graphs
(Paraphrasing) Imagination is the relationship
connecting two nodes…
TWO BOOKS I RECOMMEND FOR MORE
INFORMATION ON GRAPH THEORY AND NEO4J
Graph Theory Neo4J
By Jennifer Golbeck, 2013 By Aleksa Vukotic, et al 2014

More Related Content

PDF
RDF Tutorial - SPARQL 20091031
PDF
Evaluating Your Learning to Rank Model: Dos and Don’ts in Offline/Online Eval...
PDF
Search Quality Evaluation: a Developer Perspective
PDF
How to Build your Training Set for a Learning To Rank Project - Haystack
PDF
Rated Ranking Evaluator: An Open Source Approach for Search Quality Evaluation
PDF
Interactive Questions and Answers - London Information Retrieval Meetup
PDF
Rated Ranking Evaluator (RRE) Hands-on Relevance Testing @Chorus
PDF
Search Quality Evaluation to Help Reproducibility : an Open Source Approach
RDF Tutorial - SPARQL 20091031
Evaluating Your Learning to Rank Model: Dos and Don’ts in Offline/Online Eval...
Search Quality Evaluation: a Developer Perspective
How to Build your Training Set for a Learning To Rank Project - Haystack
Rated Ranking Evaluator: An Open Source Approach for Search Quality Evaluation
Interactive Questions and Answers - London Information Retrieval Meetup
Rated Ranking Evaluator (RRE) Hands-on Relevance Testing @Chorus
Search Quality Evaluation to Help Reproducibility : an Open Source Approach

What's hot (13)

PDF
How to Build your Training Set for a Learning To Rank Project
PPTX
4 sw architectures and sparql
PPT
Ontologies in RDF-S/OWL
PDF
Neo4j Introduction (Basics, Cypher, RDBMS to GRAPH)
PDF
Rated Ranking Evaluator Enterprise: the next generation of free Search Qualit...
PDF
Semantic Web(Web 3.0) SPARQL
PDF
Lucene And Solr Document Classification
PPT
Querying the Semantic Web with SPARQL
PPTX
Sparql
PDF
Enhance discovery Solr and Mahout
PDF
Two graph data models : RDF and Property Graphs
PPT
PPTX
Globe seminar
How to Build your Training Set for a Learning To Rank Project
4 sw architectures and sparql
Ontologies in RDF-S/OWL
Neo4j Introduction (Basics, Cypher, RDBMS to GRAPH)
Rated Ranking Evaluator Enterprise: the next generation of free Search Qualit...
Semantic Web(Web 3.0) SPARQL
Lucene And Solr Document Classification
Querying the Semantic Web with SPARQL
Sparql
Enhance discovery Solr and Mahout
Two graph data models : RDF and Property Graphs
Globe seminar
Ad

Viewers also liked (8)

PDF
COSCUP 2016 Workshop : 快快樂樂學Neo4j
PPT
MyMobileWeb Certification Part II
PPT
Integrating an App with Amazon Web Services SimpleDB - A Matter of Choices
PDF
2016 COSCUP ONOS
PPTX
Document Classification with Neo4j
PDF
Lecture 4 Decision Trees (2): Entropy, Information Gain, Gain Ratio
PDF
Neo4j - 5 cool graph examples
PDF
Data Modeling with Neo4j
COSCUP 2016 Workshop : 快快樂樂學Neo4j
MyMobileWeb Certification Part II
Integrating an App with Amazon Web Services SimpleDB - A Matter of Choices
2016 COSCUP ONOS
Document Classification with Neo4j
Lecture 4 Decision Trees (2): Entropy, Information Gain, Gain Ratio
Neo4j - 5 cool graph examples
Data Modeling with Neo4j
Ad

Similar to Neo4J Open Source Graph Database (20)

PPTX
Graph Databases
PDF
Neo4j a bit of math and magic
PDF
Graph Databases 101
PDF
Getting started with Graph Databases & Neo4j
PDF
Introduction to Graph databases and Neo4j (by Stefan Armbruster)
PDF
Neo4j (Part 1)
PDF
Introduction to Graphs with Neo4j
PPTX
Neo4j 20 minutes introduction
PPTX
Introduction to graph databases in term of neo4j
PDF
Lab3-DB_Neo4j
PDF
Graph Databases and Graph Data Science in Neo4j
PPT
Hands on Training – Graph Database with Neo4j
PPTX
Neo4j Training Introduction
PPTX
Neo4j graphdatabaseforrecommendations-130531021030-phpapp02-converted
PPTX
Neo4j tms
PDF
Betting the Company on a Graph Database - Aseem Kishore @ GraphConnect Boston...
PPT
Graph Database and Neo4j
PDF
DevFest Istanbul - a free guided tour of Neo4J
PDF
Intro to Neo4j 2.0
PDF
Soft Shake Event / A soft introduction to Neo4J
Graph Databases
Neo4j a bit of math and magic
Graph Databases 101
Getting started with Graph Databases & Neo4j
Introduction to Graph databases and Neo4j (by Stefan Armbruster)
Neo4j (Part 1)
Introduction to Graphs with Neo4j
Neo4j 20 minutes introduction
Introduction to graph databases in term of neo4j
Lab3-DB_Neo4j
Graph Databases and Graph Data Science in Neo4j
Hands on Training – Graph Database with Neo4j
Neo4j Training Introduction
Neo4j graphdatabaseforrecommendations-130531021030-phpapp02-converted
Neo4j tms
Betting the Company on a Graph Database - Aseem Kishore @ GraphConnect Boston...
Graph Database and Neo4j
DevFest Istanbul - a free guided tour of Neo4J
Intro to Neo4j 2.0
Soft Shake Event / A soft introduction to Neo4J

Recently uploaded (20)

PDF
Business Analytics and business intelligence.pdf
PPTX
STERILIZATION AND DISINFECTION-1.ppthhhbx
PDF
annual-report-2024-2025 original latest.
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPT
Reliability_Chapter_ presentation 1221.5784
PDF
Mega Projects Data Mega Projects Data
PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
PPTX
Introduction to machine learning and Linear Models
PDF
Introduction to Data Science and Data Analysis
PPT
ISS -ESG Data flows What is ESG and HowHow
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPTX
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PDF
Introduction to the R Programming Language
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPT
Miokarditis (Inflamasi pada Otot Jantung)
Business Analytics and business intelligence.pdf
STERILIZATION AND DISINFECTION-1.ppthhhbx
annual-report-2024-2025 original latest.
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
IBA_Chapter_11_Slides_Final_Accessible.pptx
Business Ppt On Nestle.pptx huunnnhhgfvu
Reliability_Chapter_ presentation 1221.5784
Mega Projects Data Mega Projects Data
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
Introduction to machine learning and Linear Models
Introduction to Data Science and Data Analysis
ISS -ESG Data flows What is ESG and HowHow
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
STUDY DESIGN details- Lt Col Maksud (21).pptx
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
Introduction to the R Programming Language
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Miokarditis (Inflamasi pada Otot Jantung)

Neo4J Open Source Graph Database

  • 1. NEO4J OPEN SOURCE GRAPH DATABASE Presented by Mark Maslyn – mmaslyn@msn.com to Denver Open Source User Group (4/7/15) and Graph Nerds of Boulder (5/14/15)
  • 2. THREE PARTS TO THIS NEO4J PRESENTATION • What Are Graph Databases and Why Are They Useful • Neo4J Application Modes and Java API Syntax • Demo Animal Guessing Game using Neo4J Decision Tree
  • 3. WHAT ARE GRAPH DATABASES ? • Graph Databases are a type of NoSQL ( non- relational) database • In a Graph Database entities (nouns) are represented as nodes • Relationships are represented by edges connecting nodes • Both nodes and relationships can have properties
  • 4. FRIEND-OF EXAMPLE FRIEND-OFFRIEND-OF Properties: Name: Mark M Language: Java Properties: Name: Tom M Language: Java Properties: Name: Tom F Language: Scala
  • 5. SOME COMPANIES AND INDUSTRIES WITH GRAPH DATABASE APPLICATIONS • Social Media (Facebook, LinkedIn, Twitter) • Configuration Management (Assimilation Systems) • Retail Recommendation (Walmart) • Resource Authorization (Telenor) • Fraud Detection ( Banks and Credit Card Companies) • Online Education (Pearson) • Bioinformatics (Bio4j)
  • 6. WHERE GRAPH DATABASES ARE ADVANTAGEOUS • Can provide a more natural representation for the data • Can give a faster query response • Recommendation engines
  • 7. EXAMPLES WHERE A GRAPH REPRESENTATION IS A NATURAL MODEL
  • 8. FLIGHT ROUTE MAP – SF TO NY 3 4 1 2 1.5 2 3 1 1 3 1.5 FLYS-TO FLYS-TO FLYS-TO FLYS-TO FLYS-TO FLYS-TOFLYS-TO FLYS-TO FLYS-TO FLYS-TO
  • 10. TWO LEVEL PROTEIN NETWORK DIAGRAM From CYTOSCAPE.ORG Two Level Tree p53
  • 11. EXAMPLE OF QUERY SPEED-UP
  • 13. EXECUTION TIME RDBMS VS GRAPH DATABASE FOR DIFFERENT DEPTHS OF FRIEND SEARCH (1000 FRIENDS) Depth Execution Time (sec) Count Result 2 0.028 ~900 3 0.213 ~999 4 10.273 ~999 5 92.613 ~999 Depth Execution Time (sec) Count Result 2 0.04 ~900 3 0.06 ~999 4 0.07 ~999 5 0.07 ~999 MYSQL RDMS NEO4J From Vukotic, et al (2014)
  • 14. EXAMPLES OF RECOMMENDATIONS BASED ON GRAPH CONNECTIONS AND CONTENT
  • 15. TYPICAL APPROACHES TO GENERATING RECOMMENDATIONS (AND PREDICTIONS) • COLLABORATIVE FILTERING – Recommendations based on common attributes between you and / or your connections – Online dating model • CONTENT BASED FILTERING - Deriving a second level of information from the data and use that to derive recommendations – Content classification model • GRAPH TOPOLOGY – Infer links based on graph topology.
  • 16. COLLABORATIVE FILTERING RECOMMENDATION BASED ON FRIENDS CHOICES – I WILL LIKE WHAT MY FRIENDS LIKE FRIEND-OFFRIEND-OF Properties: Name: Mark M Language: Java Properties: Name: Tom M Language: Java, Scala Movie: Empire Strikes Back Properties: Name: Tom F Language: Scala, Java Movie: Raiders of the Lost Ark
  • 17. CONTENT BASED RECOMMENDATIONS BASED ON CLASSIFICATION OF FRIENDS MOVIE SELECTION FRIEND-OFFRIEND-OF Properties: Name: Mark M Language: Java Properties: Name: Tom M Language: Java, Scala Movie: Empire Strikes Back Properties: Name: Tom F Language: Scala, Java Movie: Raiders of the Lost Ark
  • 18. CONTENT CLASSIFICATION BASED ON TERMS SPORTS CATEGORY FOOTBALL BASEBALL Broncos Seahawks Ronnie HillmanPeyton Manning Rockies 1 .. N SPORTS Russell Wilson CATEGORY SPORT TEAM PLAYER 1 .. N TEAMS 1 .. N PLAYERS
  • 19. FINDING RELATIONSHIPS BASED ON GRAPH TOPOLOGY FRIEND-OFFRIEND-OF Transitive Relationship A FRIEND OF B, B FRIEND OF C, Therefore High Probability A FRIEND OF C
  • 20. CONCLUSION: A TRIADIC CLOSURE OR MY FRIENDS ARE LIKELY TO BE FRIENDS WITH EACH OTHER FRIEND-OFFRIEND-OF Properties: Name: Mark M Language: Java Properties: Name: Tom M Language: Java Properties: Name: Tom F Language: Scala TRIADIC CLOSURE
  • 21. FRIEND RELATIONSHIPS BETWEEN PARTNERS IN A KARATE CLUB – BEFORE SPLIT INTO TWO CLUBS From Zachary (1977)
  • 22. KARATE CLUBS MEMBERSHIP AFTER SPLIT From Zachary (1977)
  • 23. PART II – NEO4J GRAPH DATABASE MODES AND JAVA API SYNTAX
  • 24. NEO4J GRAPH DATABASE MODES • Embedded Mode Uses Local NEO4J Jar Files • Server Mode Uses RESTFul API’s • Browser Client Mode Using CYPHER Query Language
  • 25. EMBEDDED MODE WITH JAVA API – OPENING A GRAPH DATABASE private static GraphDatabaseService graphDb; private static String ANIMAL_DATABASE_LOC = "/home/ubuntu/animal_game/animal.db"; public AnimalGame() { // open the graph database graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( ANIMAL_DATABASE_LOC); // enable clean db shutdown on Ctrl-C registerShutdownHook( graphDb ); }
  • 26. JAVA API – CREATING AN INDEX // create index using a combination of the node label and the name property try (Transaction tx = graphDb.beginTx()) { graphDb.schema().indexFor(NodeLabels.PERSON).on( "name").create(); tx.success(); }
  • 27. JAVA API – CREATING A DATABASE NODE AND SETTING PROPERTY VALUES // create node Node node = graphDb.createNode(); // add label to identify what group the node belongs to node.addLabel(nodeLabel); // set property values node.setProperty(AnimalConstants.NAME, name); node.setProperty(AnimalConstants.TEXT, text); node.setProperty(AnimalConstants.TYPE, nodeType); // make a list of possible answers (answerList) // these match with relationships node.setProperty(AnimalConstants.ANSWERS, answerList);
  • 28. JAVA API – CREATING RELATIONSHIPS BETWEEN NODES public enum RelTypes implements RelationshipType { YES, NO } public void createRelationship(Node node1, Node node2, RelTypes rt) { // relationship between node1 and node2 // direction is from node1 to node2 node1.createRelationshipTo((Node) node2, rt); }
  • 29. JAVA API - QUERYING THE DATABASE FOR A NODE BY LABEL AND NAME public Node getNodeByLabelAndName(String nodeName) { Node node = null; ResourceIterable<Node>nodeList = graphDb.findNodesByLabelAndProperty( NodeLabels.PERSON, "name", nodeName); If (nodeList != null) { try (Transaction tx = graphDb.beginTx()) { for ( Node nodeL : nodeList ) node = nodeL; tx.success(); } } return node; }
  • 30. NEO4J SERVER RESTFUL API • Neo4J Server Provides Fine-Grained REST Calls • Returns JSON Content Type • Higher Level Libraries Available for Java, .NET, Python, etc. to Wrap the Lower Level Calls
  • 31. NEO4J SERVER RESTFUL API REQUEST / RESPONSE WITH DETAILED AVAILABLE URL’S RESPONSE { "extensions": {}, "node": "http://localhost:7474/db/data/node", "node_index": "http://localhost:7474/db/data/index/node", "relationship_index": "http://localhost:7474/db/data/index/relationship", "extensions_info": "http://localhost:7474/db/data/ext", "relationship_types": "http://localhost:7474/db/data/relationship/types", "batch": "http://localhost:7474/db/data/batch", "cypher": "http://localhost:7474/db/data/cypher", "indexes": "http://localhost:7474/db/data/schema/index", "constraints": "http://localhost:7474/db/data/schema/constraint", "transaction": "http://localhost:7474/db/data/transaction", "node_labels": "http://localhost:7474/db/data/labels", "neo4j_version": "2.1.7" } REQUEST (GET): http://localhost:7474/db/data
  • 32. NEO4J SERVER RESTFUL API – DRILLING DOWN FOR META-DATA FROM ANIMAL DATABASE REQUEST: http://localhost:7474/db/data/relationship/types JSON RESPONSE: [ "YES", "NO" ]
  • 33. RETRIEVING A SINGLE NODE AND ITS PROPERTIES REQUEST: http://localhost:7474/db/data/node/0 JSON RESPONSE: { ….. "metadata": { "id": 0, "labels": [ “ANIMALS" ] }, "data": { "text": "Does the animal live on land ?", "name": "start", "answers": [ "Y/YES", "N/NO", "Q/QUIT" ], "type": "question" } }
  • 34. RETRIEVING A NODE USING THE RESTFUL LIBRARY FROM JAVA CODE private static GraphDatabaseService graphDb; private static String ANIMAL_DATABASE_URL = “http://localhost:7474/db/data”; // public constructor public AnimalGame() { // connect to the graph database server graphDb = new RestGraphDatabase( ANIMAL_DATABASE_URL); // wrap the request and retrieve the node Node startNode = graphDb.findNodesByLabelAndProperty( NODE_LABEL, “name”, “start”);
  • 35. NEO4J BROWSER BASED WEB ADMIN CLIENT • Browser Connects to Neo4J Server. • Graphically Displays Database Nodes and Relationships in a Dashboard. Can Drill Down on Each Node. • Allows User to Execute Cypher Queries on Database From the Browser.
  • 36. NEO4J BROWSER WEB ADMIN DASHBOARD
  • 37. CLICK ON A NODE FOR MORE INFO
  • 38. CYPHER QUERY LANGUAGE • Cypher is Neo4J’s version of SQL • Queries can be entered programmatically in Java or through the browser interface • Queries can retrieve nodes, relationships, property values or call functions such as return “shortest path traversal” • Declarative syntax using “Ascii Art”
  • 39. NODES - CYPHER SYNTAX Cypher uses “Ascii Art” syntax that reflects graph elements. Nodes are represented by parentheses “( )”, relationships by arrows “-->”. The arrow head indicates the direction of the relationship Adding constraints “find two Person group nodes a and b that are connected by a relationship”. Match (a) –-> (b) Return a.name, b.name Match (a:Person) --> (b:Person) Return a.name, b.name
  • 40. RELATIONSHIPS - CYPHER SYNTAX Anonymous relationships match all relationship and are indicated by the arrow alone This query syntax is used to find two nodes a and b that are connected by the specific “ACTED_IN” relationship to match actors with movies Match (a) --> (b) Match (a) –[:ACTED_IN]-> (b)
  • 41. PROPERTY GRAPH EXAMPLE FOR A MOVIE DATABASE
  • 42. SIX DEGREES OF KEVIN BACON GAME – HOW MANY LINKS ARE REQUIRED TO CONNECT AN ACTOR TO KEVIN BACON ? Kevin Bacon (left) and Tom Hanks in Apollo 13 (1995)
  • 43. CYPHER QUERY FOR SIX DEGREES OF KEVIN BACON FOR MEG RYAN USING NEO4J SHORTEST PATH FUNCTION MATCH p=shortestPath( (b:Person {name:"Kevin Bacon"})-[*]-(m:Person {name:"Meg Ryan"}) ) RETURN p
  • 44. AND THE ANSWER IS … Meg Ryan’s “Bacon” Number is 2
  • 45. NEO4J DECISION TREE – ANIMAL GUESSING GAME Mammal ? Has Stripes ? Slithers ? Does it Growl ? Has Trunk ? Zebra ?Tiger ? Elephant ? Snake? YES YES YES YES YES NO NO NO
  • 46. PART III DEMO ANIMAL GUESSING GAME – AUDIENCE VOLUNTEER REQUESTED
  • 47. A Final Comment on Connected Graphs (Paraphrasing) Imagination is the relationship connecting two nodes…
  • 48. TWO BOOKS I RECOMMEND FOR MORE INFORMATION ON GRAPH THEORY AND NEO4J Graph Theory Neo4J By Jennifer Golbeck, 2013 By Aleksa Vukotic, et al 2014