cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

Conditional Load csv

ams19
Node

Hi
I need to creates independent nodes from a csv file, csv is like below-

First, Last, Age
john, Doe, 34
null, Sam,36

My code is like this;

load csv with headers from "file:///demo.csv" as line
with line
CASE 
when not line.first is null
CREATE (s:id{name:line.last})
ELSE
CREATE (s:id{name:line.first})
return count(*)

type or paste code here

Expecting it should create independent nodes with first or last name, but I am encountering this error.

Invalid input 'S': expected 'l/L' (line 4, column 3 (offset: 92))
"CASE"

1 ACCEPTED SOLUTION

ameyasoft
Graph Maven
Try this:

load csv with headers from "file:///demo.csv" as line
with line

FOREACH(ignoreMe IN CASE WHEN line.first IS NOT NULL THEN [1] ELSE [] END |

	MERGE (s:id{name:line.last})
)

FOREACH(ignoreMe IN CASE WHEN line.first IS NULL THEN [1] ELSE [] END |

	MERGE (s:id{name:line.first})
)

View solution in original post

4 REPLIES 4

ameyasoft
Graph Maven
Try this:

load csv with headers from "file:///demo.csv" as line
with line

FOREACH(ignoreMe IN CASE WHEN line.first IS NOT NULL THEN [1] ELSE [] END |

	MERGE (s:id{name:line.last})
)

FOREACH(ignoreMe IN CASE WHEN line.first IS NULL THEN [1] ELSE [] END |

	MERGE (s:id{name:line.first})
)

Thanks it worked,
follow up question,
it took 30 minutes on my desktop to ingest a 5MB csv which had 50k records in it.
I have allocated 12 GB of heal memory.

Is there a way to run it quick or i am doing something wrong ?

There is nothing wrong. You can create an index and try it. Also, I do not recommend using 'id' as name label.

anthapu
Graph Fellow

If you know the values are going to be distinct then you can try this

load csv with headers from "file:///demo.csv" as line
with line
CREATE (s:id{name: coalesce(line.first, line.last)})

If you don't know if it is going to be distinct then you can try

Create index first

CREATE INDEX ON :id(name)

Then run the query

load csv with headers from "file:///demo.csv" as line
with line
MERGE (s:id{name: coalesce(line.first, line.last)})

These should work better.