从Ruby的Neo4j到GraphJSON [英] from Neo4j to GraphJSON with Ruby

查看:107
本文介绍了从Ruby的Neo4j到GraphJSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用d3.js或alchemy.js进行可视化-但特别是alchemy要求数据源在GraphJSON中.

I'm trying to get visualizations using d3.js or alchemy.js--but alchemy, in particular, requires the datasource to be in GraphJSON.

我一直在研究Max De Marzi(使用neography),Michael Hunger(cy2neo,js),Neo4j和Neo4j.rb的教程和示例-但我似乎无法一路走到那里.主要是因为我不知道自己在做什么-但这就是我试图学习的方式.

I've been playing around with the tutorials and examples of Max De Marzi (using neography), Michael Hunger (cy2neo, js), Neo4j, and Neo4j.rb -- but I cannot seem to get all the way there. Mostly because I don't know what I'm doing--but this is how I'm trying to learn.

我要实现的目标是: https://bl.ocks.org/mbostock/3750558 或此处的默认可视化效果: http://graphalchemist.github.io/Alchemy/#/docs

What I'm trying to achieve would be along the lines of: https://bl.ocks.org/mbostock/3750558 or the default visualization here: http://graphalchemist.github.io/Alchemy/#/docs

您还可以通过在此页面上找到GraphJSON格式来查看它的外观: http: //graphalchemist.github.io/Alchemy/#/docs

And you can see what GraphJSON formatting should look like by finding it on this page also: http://graphalchemist.github.io/Alchemy/#/docs

如果我运行以下命令...

If I run the following...

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格式.

I'll get a table of paths. But it's not formatted in the way I need--and I have no idea how it might get from this format to the GraphJSON format.

{
  "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"
    ]
  ]
}

推荐答案

我认为您遇到的一个问题是,您要匹配三个节点和两个关系,而不是匹配两个节点和一个关系.这是您的MATCH:

I think that one problem that you're having is that, instead of matching two nodes and one relationship, you're matching three nodes and two relationships. Here's your MATCH:

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

应为:

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

MATCH子句中,可以将[]排除在外,而您可以只做代表关系的原始--(或--><--).

In a MATCH clause the [] can be excluded and you can just do a raw -- (or --> or <--) which represents the relationship.

不过,您可能想查询一个特定的方向.如果双向查询,则在切换起始节点和结束节点时将获得两次相同的关系.

You probably want to be querying for one specific direction though. If you query bidirectionally you'll get the same relationship twice with the start and end nodes switched.

使用neo4j-core(我偏向于维护者之一;)

Using neo4j-core (which I biased towards as one of the maintainers ;)

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

还请注意,如果您未指定任何标签,则查询可能会变慢,具体取决于节点数).取决于您的需求;)

Also note that if you don't specify any labels your query might be slow, depending on the number of nodes). Depends on what you need ;)

这篇关于从Ruby的Neo4j到GraphJSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆