2
votes

I have created nodes using LOAD CSV method using Cypher. The next part is creating relationships with the nodes. For that I have CSV in the following format

fromStopName,from,route,toStopName,to
Swargate,1,route1_1,Swargate Corner,2
Swargate Corner,2,route1_1,Hirabaug,3
Hirabaug,3,route1_1,Maruti,4
Maruti,4,route1_1,Mandai,5

Now I would like to have "route" name as relationship between nodes. So, I am using the following LOAD CSV command in CYPHER

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:C:\\\\busroutes.csv" AS row
MATCH(f {name:row.fromStopName}),(t {name:row.toStopName}) CREATE f - [:row.route]->t

But looks like, I cannot do that. Instead, if I name relationship statically and then assign property from csv route field, it works.

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:C:\\\\busroutes.csv" AS row
MATCH(f {name:row.fromStopName}),(t {name:row.toStopName}) CREATE f - [:CONNECTS {route: row.route}]->t

I am wondering if this is disabled to enforce good practice of having "pure" verb kind of relationships and avoiding creating multiplicity of same relationship. like "connected by 1_1" "connected by 1_2".

Or I am just not finding the right link or not using correct syntax. Appreciate help!

1

1 Answers

1
votes

Right now you can't as this is structural information.

  • Either use neo4j-import tool for that.
  • Or use one CSV file per type and spell out the rel-type.
  • Or even filter the CSV and do multi-pass:

e.g.

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:C:\\\\busroutes.csv" AS row
with row where row.route = "route1_1"
MATCH(f {name:row.fromStopName}),(t {name:row.toStopName}) 
CREATE (f)-[:route1_1]->(t)

There is also a trick using fake conditionals but you still have to spell them out.