SlideShare una empresa de Scribd logo
1 de 42
Descargar para leer sin conexión
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

Más contenido relacionado

Similar a 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
 

Similar a 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
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
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...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 

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