2016-02-08 31 views
1

我想要使用d3.js或alchemy.js獲得可視化 - 但鍊金術,特別是需要數據源在GraphJSON中。從Neo4j到GraphJSON與Ruby

我一直在玩Max De Marzi(使用neography),Michael Hunger(cy2neo,js),Neo4j和Neo4j.rb的教程和示例 - 但我似乎無法一路獲得。主要是因爲我不知道自己在做什麼 - 但這就是我想學習的方式。

我試圖實現將是沿着線: https://bl.ocks.org/mbostock/3750558 或默認的可視化瀏覽:http://graphalchemist.github.io/Alchemy/#/docs

你可以看到GraphJSON格式應該是什麼樣子的發現它這個頁面還於: http://graphalchemist.github.io/Alchemy/#/docs

如果我運行下面...

get '/followers' do 
    Neo4j::Session.open(:server_db, "http://localhost:7474") 
    query = Neo4j::Session.query('MATCH (a--(b)--(c) RETURN a,b,c LIMIT 30') 
    puts "--------------" 
    puts query_to_graph_json(query) 
    query_to_graph_json(query) 
end 

# This is supposed to grab nodes and edges, but it never gets edges. 
# It's originally from a conversation at the neo4j.rb site 

    def query_to_graph_json(query) 
    nodes = {} 
    edges = {} 

    add_datum = Proc.new do |datum| 
     case datum 
     when Neo4j::ActiveNode, Neo4j::Server::CypherNode 
     nodes[datum.neo_id] = { 
     id: datum.neo_id, 
     properties: datum.props #was attributes, but kept saying that wasn't a method 
     } 
     when Neo4j::ActiveRel, Neo4j::Server::CypherRelationship 
     edges[[datum.start_node.neo_id, datum.end_node.neo_id]] = { 
     source: datum.start_node.neo_id, 
     target: datum.end_node.neo_id, 
     type: datum.rel_type, 
     properties: datum.props 
     } 
     else 
     raise "Invalid value found: #{datum.inspect}" 
     end 
    end 

    query.each do |row| 
     row.to_a.each do |datum| 
     if datum.is_a?(Array) 
      datum.each {|d| add_datum.call(d) } 
     else 
      add_datum.call(datum) 
     end 
     end 
    end 

    { 
     nodes: nodes.values, 
     edges: edges.values 
    }.to_json 
    end 

我去拿......

{ 
    "nodes": [ 
    { 
     "id": 597, 
     "properties": { 
     "name": "John", 
     "type": "Person" 
     } 
    }, 
    { 
     "id": 127, 
     "properties": { 
     "name": "Chris", 
     "type": "Person" 
     } 
    }, 
    { 
     "id": 129, 
     "properties": { 
     "name": "Suzie", 
     "type": "Person" 
     } 
    }, 
], 
    "edges": [ 

    ] 
} 

問題是我需要邊緣。

如果我跑......

get '/followers' do 
    content_type :json 
    neo = Neography::Rest.new("http://localhost:7474") 
    cypher = "MATCH (a)--(b)--(c) RETURN ID(a),a.name,ID(b),b.name,ID(c),c.name LIMIT 30" 
    puts neo.execute_query(cypher).to_json 
end 

我會得到的路徑表。但它沒有按照我需要的格式進行格式化 - 而且我也不知道如何從這種格式轉換爲GraphJSON格式。

{ 
    "columns": [ 
    "ID(a)", 
    "a.name", 
    "ID(b)", 
    "b.name", 
    "ID(c)", 
    "c.name" 
    ], 
    "data": [ 
    [ 
     597, 
     "John", 
     127, 
     "Chris", 
     129, 
     "Suzie" 
    ], 
    [ 
     597, 
     "John", 
     6, 
     "Adam", 
     595, 
     "Pee-Wee" 
    ] 
    ] 
} 

回答

1

我想你遇到的一個問題是,你不是匹配兩個節點和一個關係,而是匹配三個節點和兩個關係。這是你的MATCH

MATCH (a)--(b)--(c) 

它應該是這樣的:

MATCH (a)-[b]-(c) 

MATCH子句[]可以排除,你可以做一個原始--(或--><--)代表的關係。

雖然您可能想要查詢某個特定方向。如果雙向查詢,則會在切換的開始和結束節點兩次獲得相同的關係。

使用neo4j-core(我偏向維護者朝爲一體;)

nodes = [] 
rels = [] 
session.query('(source)-[rel]->(target)').pluck(:source, :rel, :target).each do |source, rel, target| 
    nodes << source 
    nodes << target 
    rels << rel 
end 

{ 
    nodes: nodes, 
    edges: rels 
}.to_json 

還要注意的是,如果你不指定任何標籤的查詢可能是緩慢的,這取決於節點的數量)。取決於你需要什麼;)

1

這暗號查詢應該返回邊緣陣列按例子格式:

MATCH (a)-[r]-(b) 
WITH collect(
    { 
     source: id(a), 
     target: id(b), 
     caption: type(r) 
    } 
) AS edges 
RETURN edges 

運行此對一些樣本數據,結果是這樣的:

[ 
      { 
      "source": 9456, 
      "target": 9454, 
      "caption": "LIKES" 
      }, 
      { 
      "source": 9456, 
      "target": 9454, 
      "caption": "LIKES" 
      }, 
      { 
      "source": 9456, 
      "target": 9455, 
      "caption": "LIKES" 
      }, 
      { 
      "source": 9454, 
      "target": 9456, 
      "caption": "LIKES" 
      } 
]