4
votes

I'm new to Neo4j and have been going over some example and have had modest success importing data from csv files (from a relational database). I've managed to create nodes for Persons easily:

USING PERIODIC COMMIT                                                                                                 
LOAD CSV WITH HEADERS FROM "file:/home/xxx/Development/Database/exports/persons.csv" AS row               
MERGE (:Person {id: toInt(row.id)}); 

I have a second csv file containing a row per relationship between 2 Persons and I'm trying to use the following Cypher query to create the relationships with no success (No data returned, and nothing was changed):

USING PERIODIC COMMIT                                                                                                 
LOAD CSV WITH HEADERS FROM "file:/home/xxxx/Development/Database/exports/person_relationship.csv" AS row                   
MATCH (f:Person {id: toInt(row.from_person_id)}), (t:Person {id: toInt(row.to_person_id)})                            
CREATE (f)-[:RELATED_TO]->(t);

Like I say, Persons are created fine but no amount of wangling or examples can lead me to the correct grammar to create the relationship RELATED_TO.

Any help appreciated.

1
I'm also new to neo4j, and have recently used almost exactly the same code you have to do almost the same thing. All I can say is your code looks correct to my newbie eyes. No doubt you've already checked this, but are you sure the from_person_id and to_person_id match the ids in person.csv?alan
yep, checked the column names over a few times... thanksraven-king
Just to note, I've also tried the statement to create the relationships in the web ui (replacing the row place holders with the actual values from the row, the relationship is created as expected... must be something to do with pulling the data from the csv file?raven-king
Your second statement looks correct. Can you share a few lines an the header of your file?Michael Hunger
Also make sure to create a constraint to speed it up: CREATE CONSTRAINT ON (p:Person) ASSERT p.id IS UNIQUE;Michael Hunger

1 Answers

3
votes

Your second statement looks correct. Can you share a few lines an the header of your file?

You can also check what cypher uses by trying this:

LOAD CSV WITH HEADERS FROM "file:/home/xxxx/Development/Database/exports/person_relationship.csv" AS row                   
RETURN row,  toInt(row.from_person_id), toInt(row.to_person_id)
LIMIT 5;

Also make sure to create a constraint to speed it up:

CREATE CONSTRAINT ON (p:Person) ASSERT p.id IS UNIQUE;