cancel
Showing results for 
Search instead for 
Did you mean: 

Csv triples to neo4j graph

pckrt
Node

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?

1 ACCEPTED SOLUTION

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'

View solution in original post

1 REPLY 1

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'
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.