Ruby 和 Neo4j 入門

jopen 12年前發布 | 1K 次閱讀

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</pre>

我們來簡單的介紹一下上面的源碼:

我們需要引入 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

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!