user_mda user_mda - 6 months ago 8x
SQL Question

Importing MYSQL database to NeO4j

I have a mysql database on a remote server which I am trying to migrate into Neo4j database. For this I dumped the individual tables into csv files and am now planning to use the LOAD CSV functionality to create graphs from the tables.

How does loading each table preserve the relationship between tables?
In other words, how can I generate a graph for the entire database and not just a single table?

  1. Load each table as a CSV
  2. Create indexes on your relationship field (Neo4j only does single property indexes)
  3. Use MATCH() to locate related records between the tables
  4. Use MERGE(a)-[:RELATIONSHIP]->(b) to create the relationship between the tables.

Run "all at once", this'll create a large transaction, won't go to completion, and most likely will crash with a heap error. Getting around that issue will require loading the CSV first, then creating the relationships in batches of 10K-100K transaction blocks.

One way to accomplish that goal is:

MATCH (a:LabelA)
MATCH (b:LabelB {id:}) WHERE NOT (a)-[:RELATIONSHIP]->(b)
WITH a, b LIMIT 50000

What this does is find :LabelB records that don't have a relationship with the :LabelA records and then creates that relationship for the first 50,000 records it finds. Running this repeatedly will eventually create all the relationships you want.