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.
Hey, you've mixed up
aGraph
andG
as function arguments, right?