chain relationships

38 views
Skip to first unread message

cdu8...@gmail.com

unread,
Apr 24, 2015, 8:26:51 PM4/24/15
to ne...@googlegroups.com
Hi all,

I've got .csv file with servers and scripts.

Serveur Script
AALTO Script1
AALTO Script2
AALTO Script3
AALTO Script4
ABBOTT Script5
ABBOTT Script6
ABBOTT Script7
ABBOUD Script8
ABBOUD Script9
ABBOUD Script10
ABBOUD Script11
ABBOUD Script12

And i'd like to create following relationships :

(AALTO)-[:Execution]->(Script1)-[:Puis]->(Script2)-[:Puis]->(Script3)-[:Puis]->(Script4)

(ABBOTT)-[:Execution]->(Script5)-[:Puis]->(Script6)-[:Puis]->(Script7)

(ABBOUD)-[:Execution]->(Script8)-[:Puis]->(Script9)-[:Puis]->(Script10)-[:Puis]->(Script11)-[:Puis]->(Script12)

Thanks a lot for your help.

Fred

Michael Hunger

unread,
Apr 24, 2015, 9:09:18 PM4/24/15
to ne...@googlegroups.com
This should work

it aggregates the scripts into a collection
and then uses and index range to access the individual scripts

LOAD CSV WITH HEADERS FROM ... AS row
WITH row.Serveur as server, collect(row.Script) as scripts
MERGE (s:Server {id:server})
WITH s,scripts,size(scripts) as size
FOREACH (idx in range(0,size-2) |
  MERGE (s1:Script {name:scripts[idx]})
  MERGE (s2:Script {name:scripts[idx+1]})
  CREATE (s2)-[:AFTER]->(s1)
)
MATCH (script:Script {name: scripts[0]})
CREATE (s)-[:EXECUTE]->(script)


--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

cdu8...@gmail.com

unread,
Apr 25, 2015, 6:12:57 AM4/25/15
to ne...@googlegroups.com
Hi Michael,

Thanks a lot, it works fine.

//Chargement du fichier
LOAD CSV WITH HEADERS FROM 'file:c:/Users/Fred/Desktop/scripts-test.csv' AS row FIELDTERMINATOR ';'

WITH row.Serveur as server, collect(row.Script) as scripts
MERGE (s:Server {id:server})
WITH s,scripts,size(scripts) as size
FOREACH (idx in range(0,size-2) |
  MERGE (s1:Script {name:scripts[idx]})
  MERGE (s2:Script {name:scripts[idx+1]})
  CREATE (s1)-[:AFTER]->(s2)
)
WITH scripts, s

MATCH (script:Script {name: scripts[0]})
CREATE (s)-[:EXECUTE]->(script)

I just added a WITH instruction as i had the following error message :
WITH is required between FOREACH and MATCH (line 10, column 1 (offset: 372))

"MATCH (script:Script {name: scripts[0]})"


And it works perfectly :



Fred
Reply all
Reply to author
Forward
0 new messages