SlideShare ist ein Scribd-Unternehmen logo
1 von 42
Downloaden Sie, um offline zu lesen
Neo4j
Graph Database
for Ruby and Rails
Pablo Delgado
Madrid, Conferencia Rails 2010
Wednesday, November 10, 2010
Presentation Titel | Author | City, 11/05/2010 2
GRAPHS
Extracted from
“The Graph Traversal Programming Pattern”
Rodriguez, Marko A.
Graph Systems Architect at AT&T Interactive
Wednesday, November 10, 2010
Undirected Graph
• Vertices
All Vertices denote the
same type of node
• Edges
- All edges denote the
same type of relationship
- All edges denote
symmetric relationship
Wednesday, November 10, 2010
Directed Graph
• Vertices
All Vertices denote the
same type of node
• Edges
- All edges denote the
same type of relationship
- All edges denote
asymmetric relationship
Wednesday, November 10, 2010
Single-Relational Graph
Definition
• Without a way to differentiate types of edges, all
edges have the same meaning, hence single-
relational graph
Limitations
• Are only able to represent a single kind of vertex
(user, city, etc)
• Are only able to represent a single kind of edge
(follows, citation, friends)
Wednesday, November 10, 2010
Multi-Relational Graph
Definition
• With the labeling of the edges we obtain the
possibility to express different relationships (follows,
likes, include,etc)
Gain
• Edges can now have different meanings and vertex
can have different types.
• follows: user -> user
• likes: user -> webpage
• cites: article -> paper
likes PageUser
Wednesday, November 10, 2010
Increased expressivity
with multi-relational
graphs
Multi-Relational Graph
likes
likes likes
likes
likes
likes
follows
follows
follows
Wednesday, November 10, 2010
Property Graph
Definition
• A property graph extends a multi-relational graph
allowing both, vertices and edges to maintain a
key/value property map.
• Properties are useful to express non-relational data
• This allows adding more meaning to an edge
“John Smith liked http://twitter.com on 2010/10/01”
likes PageUser
name = John Smith url = http://twiiter.com
date = 2010/10/01
Wednesday, November 10, 2010
Increased meaning on
edges and nodes in a
property graph
John liked http://twittpic.com on 2010/06/06
Xurde liked http://yumit.com on 2010/03/03
Ivan liked http://uwi.sh on 2010/07/07
Property Graph
http://twittpic.com
John
Paul
2010/06/06
http://facebook.com
http://google.com
http://yumit.com
Xurde
http://uwi.sh/
Ivan
http://panoramio.com
2010/03/03
2007/03/01
2010/7/07
2005/04/05
2005/02/05
Wednesday, November 10, 2010
Presentation Titel | Author | City, 11/05/2010 10
GRAPH DATABASE
Wednesday, November 10, 2010
Graph with Relational Database
B
A
D
C
outV | inV
-----+-----
A | B
A | C
C | D
D | A
Wednesday, November 10, 2010
Graph with JSON Database
B
A
D
C
{
A: {
out : [B,C], in : [D]
},
B: {
in : [A]
},
C: {
out : [D], in : [A]
},
D: {
out : [A], in : [C]
}
}
Wednesday, November 10, 2010
Graph with XML Database
B
A
D
C
<graphml>
<graph>
<node id=A />
<node id=B />
<node id=C />
<node id=C />
<edge source=A target=B />
<edge source=A target=C />
<edge source=C target=D />
<edge source=D target=A />
</graph>
</graphml>
Wednesday, November 10, 2010
Graph Database
Definition
• Every element (i.e. vertex or edge) has a direct
pointer to its adjacent element.
• No O(log2(n)) index lookup required to determine
which vertex is adjacent to which other vertex.
• If the graph is connected, the graph as a whole is a
single atomic data structure.
Wednesday, November 10, 2010
Graph Databases and
Index-free Adjacency
B
A
D
C
• In a graph database, vertex A has direct references to its adjacent
vertices.
• Constant time cost to move from A to B and C . It is only dependent
upon the number of edges from vertex A (local lookup).
The Graph (explicit)
Wednesday, November 10, 2010
Non-Graph Databases and
Index-Based Adjacency
B
A
D
C
• In a non-graph database, an index lookup is needed to determine
what is adjacent to A.
• log2(n) time cost to move to B and C. It is dependent upon the total
number of vertices and edges in the database (global lookup).
A B C D
[B,C] [D] [A,B][ ]
The Graph (implicit)The Index (explicit)
Wednesday, November 10, 2010
Presentation Titel | Author | City, 11/05/2010 17
Neo4j
Wednesday, November 10, 2010
Neo4j is a Graph Database
• Non-relational, transactional (ACID), embedded
• Data is stored as a Graph / Network
‣Nodes and relationships with properties
‣“Property Graph” or “edge-labeled multi-digraph”
• Schema free, bottom-up data model design
Neo4j is Open Source / Free Software
• AGPLv3
• Commercial (“dual license”) license available
What is Neo4j?
Wednesday, November 10, 2010
• Traversal APIs
Neo4j core traversers
Blueprint Pipes (TinkerPop Productions)
• SPARQL - “SQL for linked data” - query by graph pattern
matching
SELECT ?person WHERE {
?person neo4j:KNOWS ?friend .
?friend neo4j:KNOWS ?foe .
?foe neo4j:name "Larry Ellison"
}
Find all persons that KNOWS a friend that KNOWS someone named “Larry Ellison”.
• Gremlin - “perl for graphs” - query by traversal
./outE[@label='KNOWS']/inV[@age > 30]/@name
Names of all People I know with age > 30.
Neo4j Query Languages
Wednesday, November 10, 2010
Data Manipulation API
GraphDatabaseService graphDb = getGraphDbInstanceSomehow();
// Create Extractor Cobb
Node cobb = graphDb.createNode();
cobb.setProperty( "name", "Cobb" );
cobb.setProperty( "role", "Extractor" );
// Create Ariadne Architect
Node ariadne = graphDb.createNode();
ariadne.setProperty( "name", "Ariadne" );
ariadne.setProperty( "role", "Architect" );
// Create relationship representing they know each other
cobb.createRelationshipTo( ariadne, RelTypes.KNOWS );
// ... similarly add more relations here
Wednesday, November 10, 2010
Data Manipulation API
GraphDatabaseService graphDb = getGraphDbInstanceSomehow();
Transaction tx = graphDb.beginTx();
try {
// Create Extractor Cobb
Node cobb = graphDb.createNode();
cobb.setProperty( "name", "Cobb" );
cobb.setProperty( "role", "Extractor" );
// Create Ariadne Architect
Node ariadne = graphDb.createNode();
ariadne.setProperty( "name", "Ariadne" );
ariadne.setProperty( "role", "Architect" );
// Create relationship representing they know each other
cobb.createRelationshipTo( ariadne, RelTypes.KNOWS );
// ... similarly add more relations here
tx.success();
} finally {
tx.finsh();
}
Wednesday, November 10, 2010
Presentation Titel | Author | City, 11/05/2010 22
Neo4j
with
Ruby and Rails
Wednesday, November 10, 2010
• neo4j.rb - by Andreas Ronge
https://github.com/andreasronge/neo4j
A Graph Database for JRuby. It uses the java lib neo4j as
storage and Lucene for quering/indexing
• neo4jr-simple by Matthew Deiters
http://github.com/mdeiters/neo4jr-simple
Its a JRuby wrapper for Neo4j
Neo4j for Ruby and Rails
Wednesday, November 10, 2010
Inception
since=
6years
activity = undisclosed
Knows relationship
Wednesday, November 10, 2010
Inception
Hire relationship
Wednesday, November 10, 2010
Inception
In Love relationship
Wednesday, November 10, 2010
Inception
since=
6years
activity = undisclosed
Knows relationship
Hire relationship
In Love relationship
Wednesday, November 10, 2010
Inception with Neo4j
Neo4j::Transaction.run do
cobb = Node.new :name => "Cobb", :role => "Extractor"
ariadne = Node.new :name => "Ariadne", :role => "Architect", location => "Paris"
saito = Node.new :name => "Saito", :role => "Employer", :rich => true
mal = Node.new :name => "Mal", :role => "Wife",
arthur = Node.new :name => "Arthur", :role => "Partner"
eames = Node.new :name => "Eames", :role => "Forger", :location => "Mombasa"
yusuf = Node.new :name => "Yusuf", :role => "Chemist", :phone => "+254 41 787878"
fischer = Node.new :name => "Fisher Jr.", :role => "Target", :age => 29
Relationship.new(:root, Neo4j.ref_node, cobb)
Relationship.new(:knows, cobb, arthur)[:since] => "6 years"
Relationship.new(:knows, cobb, eames)
Relationship.new(:knows, arthur, eames)
Relationship.new(:knows, cobb, mal)
Relationship.new(:in_love, cobb, mal)
Relationship.new(:knows, arthur, mal)
Relationship.new(:knows, eames, yusuf)[:activity] => "undisclosed"
Relationship.new(:knows, saito, fischer)
Relationship.new(:hire, cobb, ariadne)
Relationship.new(:hire, cobb, yusuf)
Relationship.new(:hire, cobb, eames)
Relationship.new(:hire, saito, cobb)
Relationship.new(:hire, saito, arthur)
end
cobb.outgoing(:knows).depth(:all).filter { self[:name] == "Yusuf" }.first
Wednesday, November 10, 2010
• ACID Atomicity, Consistency, Isolation, Durability
Only write locks, no read locks
Transactions
Neo4j::Transaction.run do
# DO STUFF
end
Wednesday, November 10, 2010
Object Oriented Mapping
class Person
include Neo4j::NodeMixin
property :name
end
Neo4j::Transaction.run do
person = Person.new
person.name = "pablo"
end
Wednesday, November 10, 2010
Lucene in Neo4j.rb
class Person
include Neo4j::NodeMixin
property :name
index :name
end
Neo4j::Transaction.run do
person = Person.new
person.name = "pablo"
end
Person.find("name: pablo")
Wednesday, November 10, 2010
Traversals in Neo4j.rb
cobb.outgoing(:knows).depth(:all).filter { self[:name] == "Yusuf" }
pablo.outgoing(:follows).depth(2).filter { self[:age] == 30 }
From all people who cobb knows, and they know is there anyone namedYusuf?
From all people who pablo follows, which ones are 30 years old?
Wednesday, November 10, 2010
Relations
Neo4j::Transaction.run do
pablete = Node.new(:login => "pablete")
mauro = Node.new(:login => "malditogeek")
tony = Node.new(:login => "antoniogarrote")
pablete.outgoing(:friends) << mauro << tony
pablete.outgoing(:friends).each {|friend| puts friend[:login]}
end
Wednesday, November 10, 2010
Relations with NodeMixin
class Person
include Neo4j::NodeMixin
property :name
has_n :friends
end
Neo4j::Transaction.run do
pablete = Person.new(:login => "pablete")
mauro = Person.new(:login => "malditogeek")
tony = Person.new(:login => "antoniogarrote")
pablete.friends << mauro << tony
pablete.friends.each {|friend| puts friend[:login]}
end
Wednesday, November 10, 2010
Rails Drop-in Replacement
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password,:password_confirmation
after_save :encrypt_password
validates :name, :presence => true,
:length => { :maximum => 50}
validates :password, :presence => true,
:confirmation => true,
:length => {:withing => 6..40}
has_one :profile
private
def encrypt_password
self.salt = make_salt if new_record?
self.encrypted_password = encrypt(password)
end
end
Wednesday, November 10, 2010
Rails Drop-in Replacement
class User < Neo4j::Model
attr_accessor :password
attr_accessible :name, :email, :password,:password_confirmation
after_save :encrypt_password
validates :name, :presence => true,
:length => { :maximum => 50}
validates :password, :presence => true,
:confirmation => true,
:length => {:withing => 6..40}
property :name, :email, :salt, :encrypted_password
index :email
has_one :profile
private
def encrypt_password
self.salt = make_salt if new_record?
self.encrypted_password = encrypt(password)
end
end
Wednesday, November 10, 2010
Presentation Titel | Author | City, 11/05/2010 37
DEMO TIME
Wednesday, November 10, 2010
Gephi - Makes Graph Handy
Github Example:
5 Users:
-pablete
-martincik
-ppeszko
-antoniogarrote
-malditogeek
Users in RED
Repositories in BLUE
http:://github.com/pablete/conferenciarails2010
Wednesday, November 10, 2010
Gephi - Makes Graph Handy
Github Example:
5 Users:
-pablete
-martincik
-ppeszko
-antoniogarrote
-malditogeek
Users in RED
Repositories in BLUE
http:://github.com/pablete/conferenciarails2010
Wednesday, November 10, 2010
Gephi - Makes Graph Handy
Github Example:
5 Users:
-pablete
-martincik
-ppeszko
-antoniogarrote
-malditogeek
Users in RED
Repositories in BLUE
http:://github.com/pablete/conferenciarails2010
Wednesday, November 10, 2010
Presentation Titel | Author | City, 11/05/2010 41
Special thanks to:
Andreas Ronge
http://twitter.com/ronge
Peter Neubauer
http://twitter.com/peterneubauer
Marko A. Rodriguez
http://twitter.com/twarko
Wednesday, November 10, 2010
Presentation Titel | Author | City, 11/05/2010 42
Thanks!
Pablo Delgado
pablete@gmail.com
@pablete
Wednesday, November 10, 2010

Weitere ähnliche Inhalte

Ähnlich wie Neo4j for Ruby and Rails

Ruby on-rails-workshop
Ruby on-rails-workshopRuby on-rails-workshop
Ruby on-rails-workshop
Ryan Abbott
 
Intro to Web Apps using HTML5 and Javascript
Intro to Web Apps using HTML5 and JavascriptIntro to Web Apps using HTML5 and Javascript
Intro to Web Apps using HTML5 and Javascript
Brian Hogg
 
Creating a Single View Part 2: Loading Disparate Source Data and Creating a S...
Creating a Single View Part 2: Loading Disparate Source Data and Creating a S...Creating a Single View Part 2: Loading Disparate Source Data and Creating a S...
Creating a Single View Part 2: Loading Disparate Source Data and Creating a S...
MongoDB
 
CouchDB at JAOO Århus 2009
CouchDB at JAOO Århus 2009CouchDB at JAOO Århus 2009
CouchDB at JAOO Århus 2009
Jason Davies
 
Geospatial Enhancements in MongoDB 2.4
Geospatial Enhancements in MongoDB 2.4Geospatial Enhancements in MongoDB 2.4
Geospatial Enhancements in MongoDB 2.4
MongoDB
 

Ähnlich wie Neo4j for Ruby and Rails (20)

Mapping Graph Queries to PostgreSQL
Mapping Graph Queries to PostgreSQLMapping Graph Queries to PostgreSQL
Mapping Graph Queries to PostgreSQL
 
Ruby on-rails-workshop
Ruby on-rails-workshopRuby on-rails-workshop
Ruby on-rails-workshop
 
Introduction to neo4j - a hands-on crash course
Introduction to neo4j - a hands-on crash courseIntroduction to neo4j - a hands-on crash course
Introduction to neo4j - a hands-on crash course
 
Lighthouse: Large-scale graph pattern matching on Giraph
Lighthouse: Large-scale graph pattern matching on GiraphLighthouse: Large-scale graph pattern matching on Giraph
Lighthouse: Large-scale graph pattern matching on Giraph
 
Lighthouse: Large-scale graph pattern matching on Giraph
Lighthouse: Large-scale graph pattern matching on GiraphLighthouse: Large-scale graph pattern matching on Giraph
Lighthouse: Large-scale graph pattern matching on Giraph
 
Multiple graphs in openCypher
Multiple graphs in openCypherMultiple graphs in openCypher
Multiple graphs in openCypher
 
Intro to Web Apps using HTML5 and Javascript
Intro to Web Apps using HTML5 and JavascriptIntro to Web Apps using HTML5 and Javascript
Intro to Web Apps using HTML5 and Javascript
 
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross LawleyOSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
 
Understanding Connected Data through Visualization
Understanding Connected Data through VisualizationUnderstanding Connected Data through Visualization
Understanding Connected Data through Visualization
 
MongoDB
MongoDBMongoDB
MongoDB
 
Training Week: Introduction to Neo4j
Training Week: Introduction to Neo4jTraining Week: Introduction to Neo4j
Training Week: Introduction to Neo4j
 
Creating a Single View: Data Design and Loading Strategies
Creating a Single View: Data Design and Loading StrategiesCreating a Single View: Data Design and Loading Strategies
Creating a Single View: Data Design and Loading Strategies
 
Morpheus - Cypher for Apache Spark
Morpheus - Cypher for Apache SparkMorpheus - Cypher for Apache Spark
Morpheus - Cypher for Apache Spark
 
Creating a Single View Part 2: Loading Disparate Source Data and Creating a S...
Creating a Single View Part 2: Loading Disparate Source Data and Creating a S...Creating a Single View Part 2: Loading Disparate Source Data and Creating a S...
Creating a Single View Part 2: Loading Disparate Source Data and Creating a S...
 
Go for Object Oriented Programmers or Object Oriented Programming without Obj...
Go for Object Oriented Programmers or Object Oriented Programming without Obj...Go for Object Oriented Programmers or Object Oriented Programming without Obj...
Go for Object Oriented Programmers or Object Oriented Programming without Obj...
 
CouchDB at JAOO Århus 2009
CouchDB at JAOO Århus 2009CouchDB at JAOO Århus 2009
CouchDB at JAOO Århus 2009
 
Graph Analysis over JSON, Larus
Graph Analysis over JSON, LarusGraph Analysis over JSON, Larus
Graph Analysis over JSON, Larus
 
Neo4j Introduction (for Techies)
Neo4j Introduction (for Techies)Neo4j Introduction (for Techies)
Neo4j Introduction (for Techies)
 
MongoDB Europe 2016 - Graph Operations with MongoDB
MongoDB Europe 2016 - Graph Operations with MongoDBMongoDB Europe 2016 - Graph Operations with MongoDB
MongoDB Europe 2016 - Graph Operations with MongoDB
 
Geospatial Enhancements in MongoDB 2.4
Geospatial Enhancements in MongoDB 2.4Geospatial Enhancements in MongoDB 2.4
Geospatial Enhancements in MongoDB 2.4
 

Kürzlich hochgeladen

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Kürzlich hochgeladen (20)

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 

Neo4j for Ruby and Rails

  • 1. Neo4j Graph Database for Ruby and Rails Pablo Delgado Madrid, Conferencia Rails 2010 Wednesday, November 10, 2010
  • 2. Presentation Titel | Author | City, 11/05/2010 2 GRAPHS Extracted from “The Graph Traversal Programming Pattern” Rodriguez, Marko A. Graph Systems Architect at AT&T Interactive Wednesday, November 10, 2010
  • 3. Undirected Graph • Vertices All Vertices denote the same type of node • Edges - All edges denote the same type of relationship - All edges denote symmetric relationship Wednesday, November 10, 2010
  • 4. Directed Graph • Vertices All Vertices denote the same type of node • Edges - All edges denote the same type of relationship - All edges denote asymmetric relationship Wednesday, November 10, 2010
  • 5. Single-Relational Graph Definition • Without a way to differentiate types of edges, all edges have the same meaning, hence single- relational graph Limitations • Are only able to represent a single kind of vertex (user, city, etc) • Are only able to represent a single kind of edge (follows, citation, friends) Wednesday, November 10, 2010
  • 6. Multi-Relational Graph Definition • With the labeling of the edges we obtain the possibility to express different relationships (follows, likes, include,etc) Gain • Edges can now have different meanings and vertex can have different types. • follows: user -> user • likes: user -> webpage • cites: article -> paper likes PageUser Wednesday, November 10, 2010
  • 7. Increased expressivity with multi-relational graphs Multi-Relational Graph likes likes likes likes likes likes follows follows follows Wednesday, November 10, 2010
  • 8. Property Graph Definition • A property graph extends a multi-relational graph allowing both, vertices and edges to maintain a key/value property map. • Properties are useful to express non-relational data • This allows adding more meaning to an edge “John Smith liked http://twitter.com on 2010/10/01” likes PageUser name = John Smith url = http://twiiter.com date = 2010/10/01 Wednesday, November 10, 2010
  • 9. Increased meaning on edges and nodes in a property graph John liked http://twittpic.com on 2010/06/06 Xurde liked http://yumit.com on 2010/03/03 Ivan liked http://uwi.sh on 2010/07/07 Property Graph http://twittpic.com John Paul 2010/06/06 http://facebook.com http://google.com http://yumit.com Xurde http://uwi.sh/ Ivan http://panoramio.com 2010/03/03 2007/03/01 2010/7/07 2005/04/05 2005/02/05 Wednesday, November 10, 2010
  • 10. Presentation Titel | Author | City, 11/05/2010 10 GRAPH DATABASE Wednesday, November 10, 2010
  • 11. Graph with Relational Database B A D C outV | inV -----+----- A | B A | C C | D D | A Wednesday, November 10, 2010
  • 12. Graph with JSON Database B A D C { A: { out : [B,C], in : [D] }, B: { in : [A] }, C: { out : [D], in : [A] }, D: { out : [A], in : [C] } } Wednesday, November 10, 2010
  • 13. Graph with XML Database B A D C <graphml> <graph> <node id=A /> <node id=B /> <node id=C /> <node id=C /> <edge source=A target=B /> <edge source=A target=C /> <edge source=C target=D /> <edge source=D target=A /> </graph> </graphml> Wednesday, November 10, 2010
  • 14. Graph Database Definition • Every element (i.e. vertex or edge) has a direct pointer to its adjacent element. • No O(log2(n)) index lookup required to determine which vertex is adjacent to which other vertex. • If the graph is connected, the graph as a whole is a single atomic data structure. Wednesday, November 10, 2010
  • 15. Graph Databases and Index-free Adjacency B A D C • In a graph database, vertex A has direct references to its adjacent vertices. • Constant time cost to move from A to B and C . It is only dependent upon the number of edges from vertex A (local lookup). The Graph (explicit) Wednesday, November 10, 2010
  • 16. Non-Graph Databases and Index-Based Adjacency B A D C • In a non-graph database, an index lookup is needed to determine what is adjacent to A. • log2(n) time cost to move to B and C. It is dependent upon the total number of vertices and edges in the database (global lookup). A B C D [B,C] [D] [A,B][ ] The Graph (implicit)The Index (explicit) Wednesday, November 10, 2010
  • 17. Presentation Titel | Author | City, 11/05/2010 17 Neo4j Wednesday, November 10, 2010
  • 18. Neo4j is a Graph Database • Non-relational, transactional (ACID), embedded • Data is stored as a Graph / Network ‣Nodes and relationships with properties ‣“Property Graph” or “edge-labeled multi-digraph” • Schema free, bottom-up data model design Neo4j is Open Source / Free Software • AGPLv3 • Commercial (“dual license”) license available What is Neo4j? Wednesday, November 10, 2010
  • 19. • Traversal APIs Neo4j core traversers Blueprint Pipes (TinkerPop Productions) • SPARQL - “SQL for linked data” - query by graph pattern matching SELECT ?person WHERE { ?person neo4j:KNOWS ?friend . ?friend neo4j:KNOWS ?foe . ?foe neo4j:name "Larry Ellison" } Find all persons that KNOWS a friend that KNOWS someone named “Larry Ellison”. • Gremlin - “perl for graphs” - query by traversal ./outE[@label='KNOWS']/inV[@age > 30]/@name Names of all People I know with age > 30. Neo4j Query Languages Wednesday, November 10, 2010
  • 20. Data Manipulation API GraphDatabaseService graphDb = getGraphDbInstanceSomehow(); // Create Extractor Cobb Node cobb = graphDb.createNode(); cobb.setProperty( "name", "Cobb" ); cobb.setProperty( "role", "Extractor" ); // Create Ariadne Architect Node ariadne = graphDb.createNode(); ariadne.setProperty( "name", "Ariadne" ); ariadne.setProperty( "role", "Architect" ); // Create relationship representing they know each other cobb.createRelationshipTo( ariadne, RelTypes.KNOWS ); // ... similarly add more relations here Wednesday, November 10, 2010
  • 21. Data Manipulation API GraphDatabaseService graphDb = getGraphDbInstanceSomehow(); Transaction tx = graphDb.beginTx(); try { // Create Extractor Cobb Node cobb = graphDb.createNode(); cobb.setProperty( "name", "Cobb" ); cobb.setProperty( "role", "Extractor" ); // Create Ariadne Architect Node ariadne = graphDb.createNode(); ariadne.setProperty( "name", "Ariadne" ); ariadne.setProperty( "role", "Architect" ); // Create relationship representing they know each other cobb.createRelationshipTo( ariadne, RelTypes.KNOWS ); // ... similarly add more relations here tx.success(); } finally { tx.finsh(); } Wednesday, November 10, 2010
  • 22. Presentation Titel | Author | City, 11/05/2010 22 Neo4j with Ruby and Rails Wednesday, November 10, 2010
  • 23. • neo4j.rb - by Andreas Ronge https://github.com/andreasronge/neo4j A Graph Database for JRuby. It uses the java lib neo4j as storage and Lucene for quering/indexing • neo4jr-simple by Matthew Deiters http://github.com/mdeiters/neo4jr-simple Its a JRuby wrapper for Neo4j Neo4j for Ruby and Rails Wednesday, November 10, 2010
  • 24. Inception since= 6years activity = undisclosed Knows relationship Wednesday, November 10, 2010
  • 27. Inception since= 6years activity = undisclosed Knows relationship Hire relationship In Love relationship Wednesday, November 10, 2010
  • 28. Inception with Neo4j Neo4j::Transaction.run do cobb = Node.new :name => "Cobb", :role => "Extractor" ariadne = Node.new :name => "Ariadne", :role => "Architect", location => "Paris" saito = Node.new :name => "Saito", :role => "Employer", :rich => true mal = Node.new :name => "Mal", :role => "Wife", arthur = Node.new :name => "Arthur", :role => "Partner" eames = Node.new :name => "Eames", :role => "Forger", :location => "Mombasa" yusuf = Node.new :name => "Yusuf", :role => "Chemist", :phone => "+254 41 787878" fischer = Node.new :name => "Fisher Jr.", :role => "Target", :age => 29 Relationship.new(:root, Neo4j.ref_node, cobb) Relationship.new(:knows, cobb, arthur)[:since] => "6 years" Relationship.new(:knows, cobb, eames) Relationship.new(:knows, arthur, eames) Relationship.new(:knows, cobb, mal) Relationship.new(:in_love, cobb, mal) Relationship.new(:knows, arthur, mal) Relationship.new(:knows, eames, yusuf)[:activity] => "undisclosed" Relationship.new(:knows, saito, fischer) Relationship.new(:hire, cobb, ariadne) Relationship.new(:hire, cobb, yusuf) Relationship.new(:hire, cobb, eames) Relationship.new(:hire, saito, cobb) Relationship.new(:hire, saito, arthur) end cobb.outgoing(:knows).depth(:all).filter { self[:name] == "Yusuf" }.first Wednesday, November 10, 2010
  • 29. • ACID Atomicity, Consistency, Isolation, Durability Only write locks, no read locks Transactions Neo4j::Transaction.run do # DO STUFF end Wednesday, November 10, 2010
  • 30. Object Oriented Mapping class Person include Neo4j::NodeMixin property :name end Neo4j::Transaction.run do person = Person.new person.name = "pablo" end Wednesday, November 10, 2010
  • 31. Lucene in Neo4j.rb class Person include Neo4j::NodeMixin property :name index :name end Neo4j::Transaction.run do person = Person.new person.name = "pablo" end Person.find("name: pablo") Wednesday, November 10, 2010
  • 32. Traversals in Neo4j.rb cobb.outgoing(:knows).depth(:all).filter { self[:name] == "Yusuf" } pablo.outgoing(:follows).depth(2).filter { self[:age] == 30 } From all people who cobb knows, and they know is there anyone namedYusuf? From all people who pablo follows, which ones are 30 years old? Wednesday, November 10, 2010
  • 33. Relations Neo4j::Transaction.run do pablete = Node.new(:login => "pablete") mauro = Node.new(:login => "malditogeek") tony = Node.new(:login => "antoniogarrote") pablete.outgoing(:friends) << mauro << tony pablete.outgoing(:friends).each {|friend| puts friend[:login]} end Wednesday, November 10, 2010
  • 34. Relations with NodeMixin class Person include Neo4j::NodeMixin property :name has_n :friends end Neo4j::Transaction.run do pablete = Person.new(:login => "pablete") mauro = Person.new(:login => "malditogeek") tony = Person.new(:login => "antoniogarrote") pablete.friends << mauro << tony pablete.friends.each {|friend| puts friend[:login]} end Wednesday, November 10, 2010
  • 35. Rails Drop-in Replacement class User < ActiveRecord::Base attr_accessor :password attr_accessible :name, :email, :password,:password_confirmation after_save :encrypt_password validates :name, :presence => true, :length => { :maximum => 50} validates :password, :presence => true, :confirmation => true, :length => {:withing => 6..40} has_one :profile private def encrypt_password self.salt = make_salt if new_record? self.encrypted_password = encrypt(password) end end Wednesday, November 10, 2010
  • 36. Rails Drop-in Replacement class User < Neo4j::Model attr_accessor :password attr_accessible :name, :email, :password,:password_confirmation after_save :encrypt_password validates :name, :presence => true, :length => { :maximum => 50} validates :password, :presence => true, :confirmation => true, :length => {:withing => 6..40} property :name, :email, :salt, :encrypted_password index :email has_one :profile private def encrypt_password self.salt = make_salt if new_record? self.encrypted_password = encrypt(password) end end Wednesday, November 10, 2010
  • 37. Presentation Titel | Author | City, 11/05/2010 37 DEMO TIME Wednesday, November 10, 2010
  • 38. Gephi - Makes Graph Handy Github Example: 5 Users: -pablete -martincik -ppeszko -antoniogarrote -malditogeek Users in RED Repositories in BLUE http:://github.com/pablete/conferenciarails2010 Wednesday, November 10, 2010
  • 39. Gephi - Makes Graph Handy Github Example: 5 Users: -pablete -martincik -ppeszko -antoniogarrote -malditogeek Users in RED Repositories in BLUE http:://github.com/pablete/conferenciarails2010 Wednesday, November 10, 2010
  • 40. Gephi - Makes Graph Handy Github Example: 5 Users: -pablete -martincik -ppeszko -antoniogarrote -malditogeek Users in RED Repositories in BLUE http:://github.com/pablete/conferenciarails2010 Wednesday, November 10, 2010
  • 41. Presentation Titel | Author | City, 11/05/2010 41 Special thanks to: Andreas Ronge http://twitter.com/ronge Peter Neubauer http://twitter.com/peterneubauer Marko A. Rodriguez http://twitter.com/twarko Wednesday, November 10, 2010
  • 42. Presentation Titel | Author | City, 11/05/2010 42 Thanks! Pablo Delgado pablete@gmail.com @pablete Wednesday, November 10, 2010