Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
08-14-2021 01:37 PM
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")}```
Solved! Go to Solution.
08-15-2021 11:58 PM
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:
08-14-2021 10:40 PM
Please post at least one row of your csv file with headers so that I can offer a solution.
08-15-2021 05:26 AM
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 |
08-15-2021 06:29 AM
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")})
08-15-2021 05:26 PM
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?
08-16-2021 05:07 AM
Andre,
Thanks for the head's up about Pyingest. I will check it out.
Have a great day!
John
08-15-2021 11:58 PM
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:
08-16-2021 05:09 AM
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.
08-20-2021 02:38 PM
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.
08-20-2021 02:53 PM
Yes, it's a good suggestion to check with smaller data file to fix all the errors. I like it!!
08-21-2021 05:30 AM
Once again, thanks so much! I learn so much from all of you!
All the sessions of the conference are now available online