Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
08-09-2020 02:22 PM
Hello all,
I am using the following query to load a CSV file into a neo4j graph. After executing the query, it returns no nodes or relationship types. What am I missing? Attached please find a screenshot for the result.
</>
LOAD CSV WITH HEADERS FROM "file:///relations.csv" AS row
MATCH (f:Node), (s:Node)
WHERE f.Name = row.FromNode
AND s.Name = row.ToNode
CREATE (f)-[:TOSTRING(row.RelationType)]->(s)
</>
08-09-2020 05:39 PM
Hi,
I think what is happening is that the nodes are not being created. I am generally conservative since I am also still learning and will break the import into two steps first the nodes and then the relationships.
LOAD CSV WITH HEADERS FROM "file:///relations.csv" AS row
Merge (f:Node{Name:row.FromNode})
Merge(s:Node{Name:row.ToNode)
LOAD CSV WITH HEADERS FROM "file:///relations.csv" AS row
MATCH (f:Node{Name:row.FromNode})
MATCH (s:Node{Name:row.ToNode)
Merge (f)-[:TOSTRING(row.RelationType)]->(s)
Andy
08-09-2020 11:38 PM
Hello @baderk.cs
You cannot set the RELATION TYPE with a paramater in Cypher, you must use APOC:
LOAD CSV WITH HEADERS FROM "file:///relations.csv" AS row
MATCH (f:Node {Name: row.FromNode})
MATCH (s:Node {Name: row.ToNode})
CALL apoc.create.relationship(f, row.RelationType, {}, s) YIELD rel
RETURN 1
Regards,
Cobra
08-10-2020 06:38 AM
Hi,
This is my tiny csv data.
FromNode,ToNode,RelationType
A,B,1
A,C,2
D,E,Type3
Then I merged Andy and Cobra's answers.
This Cypher worked with Neo4j 4.1.1.
By the way, toString is required?
LOAD CSV WITH HEADERS FROM 'file:///relations.csv' AS row
MERGE (f:Node {Name: row.FromNode})
MERGE (s:Node {Name: row.ToNode});
CREATE INDEX node_name FOR (n:NODE) ON (n.Name);
LOAD CSV WITH HEADERS FROM 'file:///relations.csv' AS row
MATCH (f:NODE {Name: row.FromNode})
MATCH (s:NODE {Name: row.ToNode})
CALL apoc.CREATE.RELATIONSHIP(f, toString(row.RelationType), {}, s) YIELD REL
RETURN REL;
All the sessions of the conference are now available online