from Neo4j to GraphJSON with Ruby -
i'm trying visualizations using d3.js or alchemy.js--but alchemy, in particular, requires datasource in graphjson.
i've been playing around tutorials , examples of max de marzi (using neography), michael hunger (cy2neo, js), neo4j, , neo4j.rb -- cannot seem way there. because don't know i'm doing--but how i'm trying learn.
what i'm trying achieve along lines of: https://bl.ocks.org/mbostock/3750558 or default visualization here: http://graphalchemist.github.io/alchemy/#/docs
and can see graphjson formatting should finding on page also: http://graphalchemist.github.io/alchemy/#/docs
if run following...
get '/followers' 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 # supposed grab nodes , edges, never gets edges. # it's conversation @ neo4j.rb site def query_to_graph_json(query) nodes = {} edges = {} add_datum = proc.new |datum| case datum when neo4j::activenode, neo4j::server::cyphernode nodes[datum.neo_id] = { id: datum.neo_id, properties: datum.props #was attributes, kept saying wasn't 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 |row| row.to_a.each |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 i'll get...
{ "nodes": [ { "id": 597, "properties": { "name": "john", "type": "person" } }, { "id": 127, "properties": { "name": "chris", "type": "person" } }, { "id": 129, "properties": { "name": "suzie", "type": "person" } }, ], "edges": [ ] } the problem being need edges.
if run...
get '/followers' 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 i'll table of paths. it's not formatted in way need--and have no idea how might format 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" ] ] }
i think 1 problem you're having that, instead of matching 2 nodes , 1 relationship, you're matching 3 nodes , 2 relationships. here's match:
match (a)--(b)--(c) it should like:
match (a)-[b]-(c) in match clause [] can excluded , can raw -- (or --> or <--) represents relationship.
you want querying 1 specific direction though. if query bidirectionally you'll same relationship twice start , end nodes switched.
using neo4j-core (which biased towards 1 of maintainers ;)
nodes = [] rels = [] session.query('(source)-[rel]->(target)').pluck(:source, :rel, :target).each |source, rel, target| nodes << source nodes << target rels << rel end { nodes: nodes, edges: rels }.to_json also note if don't specify labels query might slow, depending on number of nodes). depends on need ;)
Comments
Post a Comment