Neo4j是一个面向网络的数据库,即一个嵌入式的、基于磁盘的、具备完全的事务特性的Java持久化引擎,但是它将结构化数据存储在网络上而不是表中。Neo4j也可以被看作是一个高性能的图引擎,该引擎具有成熟数据库的所有特性。程序员工作在一个面向对象的、灵活的网络结构下而不是严格、静态的表中——但是他们可以享受到具备完全的事务特性、企业级的数据库的所有好处。
Neo4j因其嵌入式、高性能、轻量级等优势,越来越受到关注。今天将为大家介绍如何开始学习Neo4j和Ruby,当然这是非常简单的开始。按照下面的步骤进行,你将在很短的时间内学会Neo4j。
首先,需要安装neography gem:
使用Bundler工具,代码如下:
echo "source 'http://rubygems.org'
gem 'neography' " > Gemfile
bundle install
不使用Bundler工具,代码如下:
gem install neography
然后,将任务添加到Rakefile里,下载Neo4j并启动:
echo "require 'neography/tasks'" >> Rakefile
rake neo4j:install
rake neo4j:start
图形数据库应用
比如,在社交网站上好友推荐就是个不错的应用,代码如下:
require 'rubygems'
require 'neography'
@neo = Neography::Rest.new
def create_person(name)
@neo.create_node("name" => name)
end
def make_mutual_friends(node1, node2)
@neo.create_relationship("friends", node1, node2)
@neo.create_relationship("friends", node2, node1)
end
def suggestions_for(node)
@neo.traverse(node,
"nodes",
{"order" => "breadth first",
"uniqueness" => "node global",
"relationships" => {"type"=> "friends",
"direction" => "in"},
"return filter" => {"language" => "javascript",
"body" => "position.length() == 2;"},
"depth" => 2}).map{|n| n["data"]["name"]}.join(', ')
end
johnathan = create_person('Johnathan')
mark = create_person('Mark')
phil = create_person('Phil')
mary = create_person('Mary')
luke = create_person('Luke')
make_mutual_friends(johnathan, mark)
make_mutual_friends(mark, mary)
make_mutual_friends(mark, phil)
make_mutual_friends(phil, mary)
make_mutual_friends(phil, luke)
puts "Johnathan should become friends with #{suggestions_for(johnathan)}"
# RESULT
# Johnathan should become friends with Mary, Phil
我们将一步步的介绍以上代码:
首先提取gems,得到Neography连接到Neo4j服务器的一个实例,代码如下:
require 'rubygems'
require 'neography'
@neo = Neography::Rest.new
然后编写函数,方便创建拥有name属性的节点,代码如下:
def create_person(name)
@neo.create_node("name" => name)
end
另外创建一个函数,将两个节点关联起来。
在Neo4j中所有的关系都有方向,因此,在社交网站中如果两个人有共同的好友,就需要创建了两个关系——传入和传出,代码如下:
def make_mutual_friends(node1, node2)
@neo.create_relationship("friends", node1, node2)
@neo.create_relationship("friends", node2, node1)
end
接下来是最有趣的部分,代码如何实现为你推荐好友的好友:
将一个用户作为起点,沿着两个好友关系找到他们的好友并返回结果。
def suggestions_for(node)
@neo.traverse(node,
"nodes",
{"order" => "breadth first",
"uniqueness" => "node global",
"relationships" => {"type"=> "friends",
"direction" => "in"},
"return filter" => {"language" => "javascript",
"body" => "position.length() == 2;"},
"depth" => 2}).map{|n| n["data"]["name"]}.join(', ')
end
创建小规模的好友集合并将它们联系在一起,来进行此次测试:
johnathan = create_person('Johnathan')
mark = create_person('Mark')
phil = create_person('Phil')
mary = create_person('Mary')
luke = create_person('Luke')
make_mutual_friends(johnathan, mark)
make_mutual_friends(mark, mary)
make_mutual_friends(mark, phil)
make_mutual_friends(phil, mary)
make_mutual_friends(phil, luke)
puts "Johnathan should become friends with #{suggestions_for(johnathan)}"
# RESULT
# Johnathan should become friends with Mary, Phil
这很简单吧?neography工作在Neo4j REST API的水平,但也提供了一个额外的层,涉及ruby的语法糖。
require 'rubygems'
require 'neography'
def create_person(name)
Neography::Node.create("name" => name)
end
johnathan = create_person('Johnathan')
mark = create_person('Mark')
phil = create_person('Phil')
mary = create_person('Mary')
luke = create_person('Luke')
johnathan.both(:friends) << mark
mark.both(:friends) << mary
mark.both(:friends) << phil
phil.both(:friends) << mary
phil.both(:friends) << luke
def suggestions_for(node)
node.incoming(:friends).
order("breadth first").
uniqueness("node global").
filter("position.length() == 2;").
depth(2).
map{|n| n.name }.join(', ')
end
puts "Johnathan should become friends with #{suggestions_for(johnathan)}"
# RESULT
# Johnathan should become friends with Mary, Phil