Wiggle
Wiggle

Reputation: 281

Having trouble creating relationships from csv import in neo4j

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.

Upvotes: 1

Views: 2236

Answers (2)

Phil Parnell
Phil Parnell

Reputation: 11

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)

Upvotes: 1

Jim Biard
Jim Biard

Reputation: 2272

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

Upvotes: 6

Related Questions