1

I am fairly new to graph/neo4j/cypher and have my csv imports working well for nodes, but to build the relationships, I want to make it more dynamic by using a case statement to decide my relationship statement, but it errors out with:

Invalid input 'S': expected 'l/L' (line 2, column 3 (offset: 60)) "CASE line.type"

Is it possible to use CASE in this format or is my syntax wrong?

CSV File:

id,type,rack,name_from,port,name_to
1,fDEVICE,R01-01,ID_DEV_001,PX-01-0-F,R01-01-F-U01-1-A-01
2,fDEVICE,R01-01,ID_DEV_001,PX-01-1-F,R01-01-F-U02-1-A-01
3,fDEVICE,R01-01,ID_DEV_001,PX-03-0-F,R01-01-F-U01-1-A-02
4,fDEVICE,R01-01,ID_DEV_001,PX-03-1-F,R01-01-F-U02-1-A-02
5,FRAME,,R02-01-F-U01-1-A-01,,R02-01-F-U13-1-A-01
6,FRAME,,R02-12-F-U01-1-A-01,,R02-12-F-U24-1-A-01
7,FRAME,,R02-01-F-U01-1-A-02,,R02-01-F-U14-1-A-01
8,FRAME,,R02-12-F-U01-1-A-02,,R02-12-F-U23-1-A-01
9,tDEVICE,R03-01,ID_DEV_002,gi0-01,R03-01-F-U01-1-A-01
10,tDEVICE,R03-12,ID_DEV_003,gi0-01,R03-12-F-U02-1-A-01
11,tDEVICE,R03-02,ID_DEV_004,gi0-01,R03-02-F-U01-1-A-01
12,tDEVICE,R03-11,ID_DEV_005,gi0-01,R03-11-F-U02-1-A-01

Cypher:

LOAD CSV WITH HEADERS FROM 'file:///patching.csv' AS line
CASE line.type
    WHEN 'fDEVICE' THEN
        MATCH (deviceport:DevicePort { name: line.port, device: line.name_from }),(port:Port { name: line.name_to})
        CREATE (port)-[:PATCHED]->(deviceport)
    WHEN 'FRAME' THEN
        MATCH (portA:Port { name: line.name_from}),(portB:Port { name: line.name_to})
        CREATE (portA)-[:PATCHED]->(portB)
    WHEN 'tDEVICE' THEN
        MATCH (deviceport:DevicePort { name: line.port, device: line.name_from }),(port:Port { name: line.name_to})
        CREATE (port)<-[:PATCHED]-(deviceport)
END
Andrew
  • 75
  • 1
  • 11
  • 2
    Possible duplicate of [Cypher Neo4J - CASE Expression with MERGE](http://stackoverflow.com/questions/27576427/cypher-neo4j-case-expression-with-merge) – stdob-- Jul 16 '16 at 21:03

1 Answers1

0

I would probably split it up into 3 simple statements, each filtering by line.type.

like this:

LOAD CSV WITH HEADERS FROM 'file:///patching.csv' AS line
WHERE line.type='fDEVICE'
MATCH (deviceport:DevicePort { name: line.port, device: line.name_from }),(port:Port { name: line.name_to})
CREATE (port)-[:PATCHED]->(deviceport);

etc.

There are currently no conditionals for partial statements / subqueries in cypher.

You can work around with a (non-)empty FOREACH loop for each condition but that only supports updating statements, so no MATCH.

Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
  • Thanks @michael-hunger. Am I right in assuming if run in three separate statements, its also loading the same csv three times? – Andrew Jul 17 '16 at 22:33