Loading CSV file results in no records

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)

</>

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

Hello @baderk.cs :slight_smile:

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

1 Like

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;