Load CSV with Node Label and Properties

I've been fighting with this very simple task all day. I have read countless help files, tips, manual entries, watched videos -- about to pull out my hair.

I have a very simple csv (countries.csv) with the following columns: [ name, alpha2, alpha3, region, subRegion ]

I want my node to have the name of the country as the label. For example: (Australia). I want the other columns to be properties of that label.

Whenever I load the csv I either get nodes that don't have a label (blank) or use the alpha2 (AU) as the label.

Please help figure out a simple, repeatable method to load csv files so I know how to label them with one column and use the other columns as properties.

Thanks,

John

Here is a sample of my last run:

load csv with headers from "file:///countries.csv" as row  create (c:Country) set c = row {.name, alpha2:row.alpha2, alpha3:row.alpha3, region:coalesce(row.region, "Unknown"), subRegion:coalesce(row.subRegion,"Unknown")}```

Please post at least one row of your csv file with headers so that I can offer a solution.

Thank-you so much!!!

name alpha2 alpha3 region subRegion
Afghanistan AF AFG Asia Southern Asia
Åland Islands AX ALA Europe Northern Europe
Albania AL ALB Europe Southern Europe

I went back to the basics and read more about CREATE. I thought I figured it out with the code below but the nodes came back blank again. I do see all of the properties when I click on one but I expected each node to have the name of the country on it.

load csv with headers from "file:///countries.csv" as row
CREATE (c:Country {name:row.name, alpha2:row.alpha2, alpaha3:row.alpha3, region:coalesce(row.region, "Unknown"), subRegion:coalesce(row.subRegion,"Unknown")})

I was struggling with my project too, decided to use the Pyingest script. Now it works like a charm and really fast.

Just to be sure, the CSV you're using is within the import folder set on your neo4j config file?

1 Like
Thanks for sharing the data. Looks like your .csv file is 'tab' separated and not comma separated.
Here is the Cypher:
load csv with headers from "file:///countries.csv" as row 
FIELDTERMINATOR '\t'
MERGE (c:Country {name:row.name, alpha2:row.alpha2, alpaha3:row.alpha3, region:coalesce(row.region, "Unknown"), subRegion:coalesce(row.subRegion,"Unknown")})

Result:
Screen Shot 2021-08-15 at 11.54.27 PM

3 Likes

Andre,

Thanks for the head's up about Pyingest. I will check it out.

Have a great day!

John

1 Like

Something as simple as that! Wow! Thanks. I wrestled with this over the weekend. I appreciate your help! Thanks for all you do in the Neoj4 community.

1 Like

FWIW, I find it MUCH easier to debug cypher in the Neo4J browser (http://your.domain:7474).

For a problem like this, I'd use a text editor (VSCode on Linux, Notepad++ on windows, whatever) to extract the header and no more than two or three records from the desired input file. Give it a name like "tiny_countries.csv".

Then paste your code into the browser and use file:///tiny_countries.csv. The Neo4J browser is pretty good at prompting you (with squiggles) for things that confuse it and for syntax errors and typos.

Once you get it working on a tiny file, you'll almost certainly want to prefix your cypher with:

USING PERIODIC COMMIT 100 ...

This will break the ingest into smaller transactions (100 records in the above) so that you avoid memory issues during the ingest. I prefer the Neo4J practice of using all caps for the Cypher elements so that they stand out from names and such that I create.

If you haven't already, I suggest adding indices and constraints before doing an ingest like this. It makes an ENORMOUS difference in ingest performance.

2 Likes

Yes, it's a good suggestion to check with smaller data file to fix all the errors. I like it!!

Once again, thanks so much! I learn so much from all of you!

1 Like