DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Getting started with Ruby and Neo4j

Getting started with Ruby and Neo4j

Max De Marzi user avatar by
Max De Marzi
·
Jan. 31, 12 · Web Dev Zone · Interview
Like (0)
Save
Tweet
5.75K Views

Join the DZone community and get the full member experience.

Join For Free

getting started with ruby and neo4j is very easy.
follow these steps and you’ll be up and running in no time.

first we install the neography gem:

using bundler:
1	echo "source 'http://rubygems.org'
2	gem 'neography' " > gemfile
3	bundle install 
without bundler:
1	gem install neography

then we’ll add our tasks to a rakefile, download neo4j and start it:

1	echo "require 'neography/tasks'" >> rakefile
2	rake neo4j:install
3	rake neo4j:start

what can you do with a graph database?

how about recommend friends on a social network?

01	require 'rubygems'
02	require 'neography'
03	 
04	@neo = neography::rest.new
05	 
06	def create_person(name)
07	  @neo.create_node("name" => name)
08	end
09	 
10	def make_mutual_friends(node1, node2)
11	  @neo.create_relationship("friends", node1, node2)
12	  @neo.create_relationship("friends", node2, node1)
13	end
14	 
15	def suggestions_for(node)
16	  @neo.traverse(node,
17	                "nodes",
18	                {"order" => "breadth first",
19	                 "uniqueness" => "node global",
20	                 "relationships" => {"type"=> "friends",
21	                                     "direction" => "in"},
22	                 "return filter" => {"language" => "javascript",
23	                                     "body" => "position.length() == 2;"},
24	                 "depth" => 2}).map{|n| n["data"]["name"]}.join(', ')
25	end
26	 
27	johnathan = create_person('johnathan')
28	mark      = create_person('mark')
29	phil      = create_person('phil')
30	mary      = create_person('mary')
31	luke      = create_person('luke')
32	 
33	make_mutual_friends(johnathan, mark)
34	make_mutual_friends(mark, mary)
35	make_mutual_friends(mark, phil)
36	make_mutual_friends(phil, mary)
37	make_mutual_friends(phil, luke)
38	 
39	puts "johnathan should become friends with #{suggestions_for(johnathan)}"
40	 
41	# result
42	# johnathan should become friends with mary, phil

let’s go through each step:

we require our gems and get an instance of neography connecting to the neo4j server:

1	require 'rubygems'
2	require 'neography'
3	 
4	@neo = neography::rest.new

we write a function to make it easy for us to create nodes that have a name property:

1	def create_person(name)
2	  @neo.create_node("name" => name)
3	end

then we create another function to relate two nodes together.
in neo4j all relationships have a direction, so if want two people to be mutual friends we create two relationships one incoming and one outgoing.

1	def make_mutual_friends(node1, node2)
2	  @neo.create_relationship("friends", node1, node2)
3	  @neo.create_relationship("friends", node2, node1)
4	end

now comes the fun part. we want to suggest that you become friends with the friends of your friends.




so using a person as a starting point, we want to go out two friends relationships away and return with those friends of friends.

01	def suggestions_for(node)
02	  @neo.traverse(node,
03	                "nodes",
04	                {"order" => "breadth first",
05	                 "uniqueness" => "node global",
06	                 "relationships" => {"type"=> "friends",
07	                                     "direction" => "in"},
08	                 "return filter" => {"language" => "javascript",
09	                                     "body" => "position.length() == 2;"},
10	                 "depth" => 2}).map{|n| n["data"]["name"]}.join(', ')
11	end

let’s test this out by creating a small set of friends and link them together:

01	johnathan = create_person('johnathan')
02	mark      = create_person('mark')
03	phil      = create_person('phil')
04	mary      = create_person('mary')
05	luke      = create_person('luke')
06	 
07	make_mutual_friends(johnathan, mark)
08	make_mutual_friends(mark, mary)
09	make_mutual_friends(mark, phil)
10	make_mutual_friends(phil, mary)
11	make_mutual_friends(phil, luke)
12	 
13	puts "johnathan should become friends with #{suggestions_for(johnathan)}"
14	 
15	# result
16	# johnathan should become friends with mary, phil

there you have it. pretty simple right?

no? if it looks like a bit much, can we make it easier?

absolutely. neography lets you work at the neo4j rest api level, but also provides an additional layer sprinkled with ruby syntactic sugar.

01	require 'rubygems'
02	require 'neography'
03	 
04	def create_person(name)
05	  neography::node.create("name" => name)
06	end
07	 
08	johnathan = create_person('johnathan')
09	mark      = create_person('mark')
10	phil      = create_person('phil')
11	mary      = create_person('mary')
12	luke      = create_person('luke')
13	 
14	johnathan.both(:friends) << mark
15	mark.both(:friends) << mary
16	mark.both(:friends) << phil
17	phil.both(:friends) << mary
18	phil.both(:friends) << luke
19	 
20	def suggestions_for(node)
21	  node.incoming(:friends).
22	       order("breadth first").
23	       uniqueness("node global").
24	       filter("position.length() == 2;").
25	       depth(2).
26	       map{|n| n.name }.join(', ')
27	end
28	puts "johnathan should become friends with #{suggestions_for(johnathan)}"
29	 
30	# result
31	# johnathan should become friends with mary, phil
take a look at the neography documentation for more.

in this post we saw how to traverse the graph using the traversal framework directly.
in upcoming posts , i’ll show you two more ways to traverse the graph via gremlin and cypher as well as many more things you can do with neo4j .

find me and many other graph aficionados at the neo4j google group .

source: http://maxdemarzi.com/2012/01/04/getting-started-with-ruby-and-neo4j/

Neo4j

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Spring, IoC Containers, and Static Code: Design Principles
  • Applying Domain-Driven Design Principles to Microservice Architectures
  • Making Your Own Express Middleware
  • Caching Across Layers in Software Architecture

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo