Csv triples to neo4j graph


(pckrt) #1

I have a csv file having 5 columns. I want to model a graph in the following way:
First column-> node
Second column -> label of the node from first column
Fourth column-> node
Fifth column -> label of the node from fourth column
Third column -> relation between node from first column and fourth column.

how can I write a cypher query to do this?


(Bratanic Tomaz) #2

You need APOC in order to import dynamic labels.

I will assume that the third column contains the relationship type.

LOAD CSV FROM "file:///your_file.csv" as row

CALL apoc.merge.node([row[1], {id:row[0]}, {})
YIELD node as startNode
CALL apoc.merge.node([row[4], {id:row[3]}, {})
YIELD node as endtNode
CALL apoc.create.relationship(startNode, row[2], {}, {}, endNode) yield rel
RETURN distinct 'done'