1
votes

I have used a command like this to successfully create named nodes from csv:

load csv with headers from "file:/Users/lwyglend/Developer/flavourGroups.csv" as
flavourGroup                
create (fg {name: flavourGroup.flavourGroup}) 
set fg:flavourGroup 
return fg

However I am not having any luck using load from csv to create relationships with a similar command:

load csv with headers from "file:/Users/lwyglend/Developer/flavoursByGroup.csv" as
relationship 
match (flavour {name: relationship.flavour}), 
        (flavourGroup {name: relationship.flavourGroup}) 
create flavour-[:BELONGS_TO]->flavourGroup

From a headed csv file that looks a bit like this:

flavour, flavourGroup
fish, marine
caviar, marine 

There are no errors, the command seems to execute, but no relationships are actually created.

If I do a simple match on name: fish and name: marine and then construct the belongs to relationship between the fish and marine pre-existing nodes with cypher, the relationship sets up fine.

Is there a problem with importing from csv? Is my code wrong somehow? I have played around with a few different things but as a total newb to neo4j would appreciate any advice you have.

2
Did you ever import the flavours, not only the flavorGroups? Seems they are missing, so you won't get any matches for your relationship import.Peter Neubauer
Yes, I imported both flavours and flavourGroups successfully from two separate csv files. Sorry that wasn't clear. Named nodes from either group can be matched successfully, and a relationship added between a node from either group, it is only creating all the relationships from the example csv with headers that I cannot get to work. I don't want to do it by hand-written cypher, as hundreds of relationships to add.Wiggle

2 Answers

6
votes

Wiggle,

I don't know for sure if this is your problem, but I discovered that if you have spaces after your commas in your CSV file (as you show in your example), they appear to be included as part of the field names and field contents. When I made a CSV file like the one you showed and tried to load it, I found that it failed. When I took out the spaces, I found that it succeeded.

As a test, try this query:

LOAD FROM CSV WITH HEADERS FROM "file:/Users/lwyglend/Developer/flavoursByGroup.csv" AS line
RETURN line.flavourGroup

then try this query:

LOAD FROM CSV WITH HEADERS FROM "file:/Users/lwyglend/Developer/flavoursByGroup.csv" AS line
RETURN line.` flavourGroup`

Grace and peace,

Jim

1
votes

I'm a bit late in answering your question, but I don't think the spaces alone are the culprit. In your example cypher there is no association to the actual nodes in your database, only the csv alias named "relationship".

Try something along this line instead:

load csv with headers from "file:/Users/lwyglend/Developer/flavoursByGroup.csv" as relationship match (f:flavour), (fg:flavourGroup) where f.name = relationship.flavour and fg.name = relationship.flavourGroup create (f)-[:BELONGS_TO]->(fg)