Ruby 和 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 并获取一个连接到 Neo4j 服务器的 Neography 的实例:
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
怎样,很简单吧?
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
更多的信息请阅读 Neography Documentation