SlideShare a Scribd company logo
1 of 42
Download to read offline
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

More Related Content

Similar to Neo4j for Ruby and Rails

Mapping Graph Queries to PostgreSQL
Mapping Graph Queries to PostgreSQLMapping Graph Queries to PostgreSQL
Mapping Graph Queries to PostgreSQLGábor Szárnyas
 
Ruby on-rails-workshop
Ruby on-rails-workshopRuby on-rails-workshop
Ruby on-rails-workshopRyan Abbott
 
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 courseNeo4j
 
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 GiraphIoan Toma
 
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 GiraphLDBC council
 
Multiple graphs in openCypher
Multiple graphs in openCypherMultiple graphs in openCypher
Multiple graphs in openCypheropenCypher
 
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 JavascriptBrian Hogg
 
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 LawleyNETWAYS
 
Understanding Connected Data through Visualization
Understanding Connected Data through VisualizationUnderstanding Connected Data through Visualization
Understanding Connected Data through VisualizationSebastian Müller
 
Training Week: Introduction to Neo4j
Training Week: Introduction to Neo4jTraining Week: Introduction to Neo4j
Training Week: Introduction to Neo4jNeo4j
 
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 StrategiesMongoDB
 
Morpheus - Cypher for Apache Spark
Morpheus - Cypher for Apache SparkMorpheus - Cypher for Apache Spark
Morpheus - Cypher for Apache SparkKnoldus Inc.
 
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
 
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...Steven Francia
 
CouchDB at JAOO Århus 2009
CouchDB at JAOO Århus 2009CouchDB at JAOO Århus 2009
CouchDB at JAOO Århus 2009Jason Davies
 
Graph Analysis over JSON, Larus
Graph Analysis over JSON, LarusGraph Analysis over JSON, Larus
Graph Analysis over JSON, LarusNeo4j
 
Neo4j Introduction (for Techies)
Neo4j Introduction (for Techies)Neo4j Introduction (for Techies)
Neo4j Introduction (for Techies)Patrick Baumgartner
 
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 MongoDBMongoDB
 
Geospatial Enhancements in MongoDB 2.4
Geospatial Enhancements in MongoDB 2.4Geospatial Enhancements in MongoDB 2.4
Geospatial Enhancements in MongoDB 2.4MongoDB
 

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

Recently uploaded

Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1DianaGray10
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemAsko Soukka
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding TeamAdam Moalla
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPathCommunity
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureEric D. Schabell
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7DianaGray10
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxUdaiappa Ramachandran
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarPrecisely
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfJamie (Taka) Wang
 

Recently uploaded (20)

Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystem
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation Developers
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability Adventure
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7
 
201610817 - edge part1
201610817 - edge part1201610817 - edge part1
201610817 - edge part1
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptx
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
20230104 - machine vision
20230104 - machine vision20230104 - machine vision
20230104 - machine vision
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity Webinar
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
 

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