Creating nodes and relationships conditionally while loading nodes from csv in Neo4j

I have a dataset in csv format. One of the fields is a type, such as an enumeration. Based on this type, I need to create different types of nodes and relationships when loading data using csv load. You can call a line in csv for a super type that has an attribute defining its subtype.

I cannot figure out how this can be done in cypher. Is my only option to split one csv file per csv file for each type and run different cyphers?

+3


source to share


1 answer


This document on conditional statements helps.

Here's a simple example of loading by column value:

Data: a, b Ford, Chevy car, Mazda truck, GMC car, F150 truck, 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're done, you will have nodes created from different types.

+4


source







All Articles