How to create dynamic relations from csv with apoc?


(Mirto Busico) #1

Hi all,
I have a csv file containing:

"FromNode","RelationType","ToNode"
"Cybersecurity knowledge","CONNECTED","Vulnerabilities"
"Cybersecurity knowledge","RELATED","Mitigations"

Trying to load from csv I used this (wrong) statement:

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)

I found that the apoc procedure

"apoc.create.relationship(person1,'KNOWS',{key:value,...}, person2) create relationship with dynamic rel-type"

should do thework; but I was unable to find the correct syntax.

How can I create these differently labelled relationships from csv?

UPDATE:
Tryed this

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
CALL apoc.create.relationship(f, row.RelationType,{}, s)

(taken from https://stackoverflow.com/questions/48606320/can-i-use-this-csv-to-load-a-neo4j-graph-with-cypher )

But obtained the error:

SyntaxError: Query cannot conclude with CALL (must be RETURN or an update clause) (line 5, column 1 (offset: 135)) 

(Andrew Bowman) #2

Unless the CALL is the only thing in the query, you need to explicitly YIELD variables from the call (using call apoc.help('create.rel') shows us this procedure yields arel` variable), and as in the message you can't end the query with a CALL, it needs to be a writing clause or a return. If you need the equivalent of a no-op then you can remove a non-existent property from a node or relationship:

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
CALL apoc.create.relationship(f, row.RelationType,{}, s) YIELD rel
REMOVE rel.noOp

(Mirto Busico) #3

Thanks a lot: it worked.

Please, can you explain

If you need the equivalent of a no-op then you can remove a non-existent property from a node or relationship

I don't understand when and how

REMOVE rel.noOp

works