0
votes

I have a CSV dataset through which I'm trying to build relationships between two node types(Comment and Person) that already exist in my database.

This is the database information -

enter image description here

This is the csv file of the current relationship comment_hasCreator_person that I'm trying to build -
enter image description here

The problem is - no matter which Cypher query I try, all of them returns the same thing - "no changes, no rows".

Here are the different variations of the query I've tried -

This is the first query -

// comment_hasCreator_person_0_0.csv
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "https://dl.dropbox.com/s/qb4occggixmaz9g/comment_hasCreator_person_0_0.csv" AS line
MATCH (comment:Comment { id: toInt(line.Comment.id)}),(person:Person { id: toInt(line.Person.id)})
CREATE (comment)-[:hasCreator]->(person)

I assumed this might have not worked because my CSV headers were initially named Comment.id and Person.id. So I removed the . and tried out the query, with the same result -

// comment_hasCreator_person_0_0.csv
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "https://dl.dropbox.com/s/qb4occggixmaz9g/comment_hasCreator_person_0_0.csv" AS line
MATCH (comment:Comment { id: toInt(line.Commentid)}),(person:Person { id: toInt(line.Personid)})
CREATE (comment)-[:hasCreator]->(person)

When that didn't work, I followed this answer and tried using MERGE instead of CREATE, even though it shouldn't make a difference because the relationships didn't exist in the first place -

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "https://www.dropbox.com/s/qb4occggixmaz9g/comment_hasCreator_person_0_0.csv?dl=0" AS line
MATCH (comment:Comment { id: toInt(line.Commentid)}),(person:Person { id: toInt(line.Personid)})
MERGE (comment)-[r:hasCreator]->(person)
RETURN comment,r, person 

This query just returned "no rows".

I also tried a variation of the query where I didn't use the toInt() function, but that didn't make any difference.

To ensure the nodes exist, I selected random cell values from the CSV file and used a MATCH clause to ensure the corresponding Comment and Person nodes exist in the database, and I did find all the nodes.

As the last step, I decided to create a relationship manually between the first row values from the CSV file -

MATCH (c:Comment{id:1236950581249}), (p:Person{id:10995116284808})
CREATE (c)-[r:hasCreator]->(p)
RETURN c,r,p

and this worked just fine -

enter image description here

I'm totally clueless as to why the relationships won't get created when I import it from the CSV file. I would appreciate any help.

3

3 Answers

3
votes

You have a problem in yout CSV file. The field terminator character used in it is "|" and not the default ",". You can edit your CSV file and chenge the field terminator character to "," or use the option FIELDTERMINATOR available in the LOAD CSV.

Try editing your query to something like this:

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "https://www.dropbox.com/s/qb4occggixmaz9g/comment_hasCreator_person_0_0.csv?dl=0" AS line
FIELDTERMINATOR '|'
MATCH (comment:Comment { id: toInt(line.Commentid)}),(person:Person { id: toInt(line.Personid)})
MERGE (comment)-[r:hasCreator]->(person)
RETURN comment,r, person 
0
votes

You are missing the field terminator here as it is | in your case, instead of ;.

You can try this out:

USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "filename" AS LINE FIELDTERMINATOR '|' MERGE (comment:Comment { id: toInt(LINE.Commentid)}) MERGE (person:Person { id: toInt(line.Personid)}) MERGE (comment) - [r:has_creator] -> (person) RETURN comment,r,person

0
votes

Another reason for this kind of error may be white spaces in CSV file. If line in CSV looks like:

2a9b40bc-78f0-4e79-9b2b-441108883448, Pink node - 2, 2, pink

then index 1 for results will be: ' Pink node - 2' (notice space at beginning), not: 'Pink node - 2'. Editing csv files or using trim() function would be the solution here:

...
WHERE a.id = trim(line[0]) AND b.id = trim(line[1]) 
...