How to import a networkX graph to neo4j?

Viewed 3877

I have a graph created with networkX and I am using neonx to import it to neo4j on localhost. I have a networkX type graph called G. Below is the code:

data1 = json_graph.node_link_data(G)
H = json_graph.node_link_graph(data1)

results = neonx.write_to_neo("http://localhost:7474/db/data/", H, 'LINKS_TO')

The error I get is:

Traceback (most recent call last):
  File "/Users/aman/venv/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2961, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-5-0d401e8987b7>", line 31, in <module>
    results = neonx.write_to_neo("http://localhost:7474/db/data/", H, 'LINKS_TO')
  File "/Users/aman/venv/lib/python3.7/site-packages/neonx/neo.py", line 86, in write_to_neo
    batch_url = all_server_urls['batch']
KeyError: 'batch'
2 Answers

Best way is to dump networkx graph and import it into Neo4j.

Advantages:

  • You don't need to have a working connection with Neo4j server
  • Dump once and load it anywhere anytime. You have saved version/backup.

Dump networkx graph:

nx.write_graphml(g, 'path/to/file.graphml')

Load into Neo4j:

  • Install apoc plugin for Neo4j from here
  • Enable apoc.import.file.enabled=true in conf/neo4j.conf
  • Place your graph dump file.graphml in import/ folder
cypher-shell -a bolt://localhost:7687 "call apoc.import.graphml('file.graphml', {})"

Or in browser:

call apoc.import.graphml('file.graphml', {})

For more details:

Related