This little project defines a function that can be used to construct a Cypher query which when executed against a Neo4j database server will store the graph to the server.
- A Graph is an abstract mathematical model composed of Nodes connected through Edges that can be used to describe complex systems composed of a set of parts (corresponding to nodes) and their connections (corresponding to edges).
- Examples of graphs are road networks (junctions connected via roads), electronic circuit networks (components and their connections) and others
- Networkx is an excellent Python module for manipulating such Graph objects of any kind.
- Neo4j is a graph database. It uses the Graph as a data model to store such objects to a data store.
- Cypher is Neo4j's query language. It is a domain specific language that can be used to manipulate graph objects.
Given a graph (G) write a function that creates the query to store the graph with all of its nodes, edges and attributes.
- By traversing all nodes and edges and creating the corresponding parts of the Cypher query.
- graph2Cypher requires the random, Networkx modules.
- The graph2Cypher_demo.py requires networkx, matplotlib
- The graph2Cypher function assumes that its (only) parameter IS A DIRECTED GRAPH.
- Simply going through all nodes and edges and dumping their attributes is not practical for all graphs because the node-id used by Networkx might not be usable by Neo4j directly. The typical example is a graph whose Networkx node-ids are integers.
- For this reason and just for the needs of constructing the Cypher query, the graph's nodes get relabeled on the fly.
- Furthermore, certain assumptions are made on attribute names. Each node's id is identified by the ID node attribute, while edges are getting the type ":LINKED_TO" by default.
-
Obviously, the function can be used in stand-alone mode to create the query that can then be sent to the neo4j database through something like the Python REST interface or the Neo4j-shell.
-
In the case of the Neo4j-shell, assuming that you have it to your system path, you can simply do the following:
python graph2Cypher_demo.py>aGraph.cypher #This creates the text file with the Cypher query neo4j-shell -file aGraph.cypher #This will execute the query within aGraph.cypher and store the graph to the database.
Hello Michael
I guess i can but i did not think that the extent of the contribution justified a repo of its own (perhaps if i gather more related Networkx + Neo4j code it could become a repo of its own)
Regarding the random tags:
The short answer: Yes you are right, this could be done too.
The long answer: Networkx does not really care what the user decides to attach as a "Node". This is perfectly valid:
So, if you have full control over someThing, then you could put a repr function in place that derives some sort of unique identifier from the object which could then be used directly even in a Cypher query, escaped, exactly as you suggest.
If you don't have full control over it, you can possibly derive (and add the repr) OR do the trick with generating identifiers.
I did not want to restrict the use of the function too much. If you remove the "ID" part, the node's id would not participate at all.
But thanks anyway, this can be added to the next iteration along with: