stoffer
stoffer

Reputation: 2397

Create nodes and relations conditionally when loading nodes from csv in Neo4j

I have a data set in csv format. One of the fields is a type, like an enumeration. Based on this type I need to create different types nodes and relations when loading the data using csv load. You could call a row in the csv for a super type having an attribute defining its subtype.

I'm not really able to figure out how this can be done in cypher. Is my only option to split the one csv file into a csv file per type and run different cyphers ?

Upvotes: 4

Views: 2270

Answers (1)

Dave Fauth
Dave Fauth

Reputation: 878

This document on conditional statements helps.

Here is a simple example for loading based on a column value:

Data: a,b Ford,car Chevy,truck Mazda,car GMC,truck F150,truck

Cypher code:

load csv with headers from "file:/testfile.csv" as row
FOREACH(ignoreMe IN CASE WHEN trim(row.b) = "truck" THEN [1] ELSE [] END | MERGE (p:Truck {vehicleType: row.a}))
FOREACH(ignoreMe IN CASE WHEN trim(row.b) = "car" THEN [1] ELSE [] END | MERGE (p:Car {vehicleType: row.a}))

When you are done, you will have nodes created of different types.

Upvotes: 5

Related Questions