Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

Max De Marzi, is a seasoned web developer. He started building websites in 1996 and has worked with Ruby on Rails since 2006. The web forced Max to wear many hats and master a wide range of technologies. He can be a system admin, database developer, graphic designer, back-end engineer and data scientist in the course of one afternoon. Max is a graph database enthusiast. He built the Neography Ruby Gem, a rest api wrapper to the Neo4j Graph Database. He is addicted to learning new things, loves a challenge and finding pragmatic solutions. Max is very easy to work with, focuses under pressure and has the patience of a rock. Max is a DZone MVB and is not an employee of DZone and has posted 22 posts at DZone. You can read more from them at their website. View Full User Profile

Getting started with Ruby and Neo4j

01.31.2012
Email
Views: 2244
  • submit to reddit

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/

Published at DZone with permission of Max De Marzi, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)