cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

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")}```
1 ACCEPTED SOLUTION

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:
3X_4_4_44102cccd33f88011be12c2f5b841d0c7d2d4b9e.png

View solution in original post

10 REPLIES 10

ameyasoft
Graph Maven

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?

Andre,

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

Have a great day!

John

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:
3X_4_4_44102cccd33f88011be12c2f5b841d0c7d2d4b9e.png

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.

tms
Graph Buddy

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.

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!