chain relationships if condition

34 views
Skip to first unread message

cdu8...@gmail.com

unread,
May 15, 2015, 5:49:23 AM5/15/15
to ne...@googlegroups.com
Hi all,

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

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

And i'd like to create following relationships :

Lundi-[:Process]->(AALTO)-[:Execution]->(Script1)-[:Puis]->(Script2)-[:Puis]->(Script4)
Lundi-[:Process]->(ABBOTT)-[:Execution]->(Script6)
Lundi-[:Process]->(ABBOUD)-[:Execution]->(Script8)-[:Puis]->(Script9)-[:Puis]->(Script10)

And so on for Mardi and Samedi

Thanks a lot for your help.

Fred

Michael Hunger

unread,
May 15, 2015, 2:54:47 PM5/15/15
to ne...@googlegroups.com
I'd probably do a multi-pass, one per day

load csv with headers from "" as row
where row.Lundi = "x"
match (d:Day {name:"Lundi"})
with d,row.Serveur as serverName, collect(row.Script) as scripts
match (s:Server {name:serverName})
merge (d)-[:PROCESS]->(s)
with s,head(scripts) as first, scripts
match (scr1:Script {name: first})
create (s)-[:EXECUTION]->(scr1)
unwind range(0,size(scripts)-2) as idx
MATCH (a:Script {name:scripts[idx])
MATCH (b:Script {name:scripts[idx+1])
CREATE (a)-[:PUIS]->(b);


1. instead of MATCH you can also use merge, make sure you have the indexes / constraints created
2. instead of UNWIND you can also use FOREACH (but then you have to use MERGE for a,b not MERGE)

Michael

--
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,
May 16, 2015, 5:21:43 PM5/16/15
to ne...@googlegroups.com
Hi Michael,

Thanks for your answer, i tried to followyour solution but without success.

LOAD CSV WITH HEADERS FROM 'file:c:/Users/Fred/Desktop/cgp2.csv' AS row FIELDTERMINATOR ';'

match (d:Day {name:"Lundi"})
where row.Lundi = "x"

with d,row.Serveur as serverName, collect(row.Script) as scripts
match (s:Serveur {name:serverName})

merge (d)-[:PROCESS]->(s)
with s,head(scripts) as first, scripts, size(scripts) as size

match (scr1:Script {name: first})
create (s)-[:EXECUTION]->(scr1)
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)

When i do a match n return n it returns nothing.

Thanks for your help.

Best regards,

Fred
cgp2.csv

Michael Hunger

unread,
May 16, 2015, 5:44:02 PM5/16/15
to ne...@googlegroups.com
you need to have the nodes for days, servers, scripts etc. before otherwise use merge

Am 16.05.2015 um 23:21 schrieb cdu8...@gmail.com:

Hi Michael,

Thanks for your answer, i tried to followyour solution but without success.

LOAD CSV WITH HEADERS FROM 'file:c:/Users/Fred/Desktop/cgp2.csv' AS row FIELDTERMINATOR ';'
merge (d:Day {name:"Lundi"})

where row.Lundi = "x"
with d,row.Serveur as serverName, collect(row.Script) as scripts
merge (s:Serveur {name:serverName})

merge (d)-[:PROCESS]->(s)
with s, scripts, size(scripts) as size
merge (scr1:Script {name: scripts[0]})

create (s)-[:EXECUTION]->(scr1)
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

--
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.
<cgp2.csv>

cdu8...@gmail.com

unread,
May 18, 2015, 4:20:14 PM5/18/15
to ne...@googlegroups.com
Hi Michael,

It should be nearly good but it isn't.


LOAD CSV WITH HEADERS FROM 'file:c:/Users/Fred/Desktop/cgp2.csv' AS row FIELDTERMINATOR ';'
where row.Lundi = "x"
match (d:Day {name:"Lundi"})

with d,row.Serveur as serverName, collect(row.Script) as scripts
match (s:Server {name:serverName})

merge (d)-[:PROCESS]->(s)
with s,head(scripts) as first, scripts
match (scr1:Script {name: first})

create (s)-[:EXECUTION]->(scr1)
unwind range(0,size(scripts)-2) as idx
MATCH (a:Script {name:scripts[idx]})
MATCH (b:Script {name:scripts[idx+1]})
CREATE (a)-[:PUIS]->(b)

I've got the following error :
Invalid input 'h': expected 'i/I' (line 2, column 2 (offset: 93))
"where row.Lundi = "x""


Thanks a lot for your help.

Best Regards,

Fred

Michael Hunger

unread,
May 18, 2015, 5:20:27 PM5/18/15
to ne...@googlegroups.com
Sorry, forgot a WITH row

LOAD CSV WITH HEADERS FROM 'file:c:/Users/Fred/Desktop/cgp2.csv' AS row FIELDTERMINATOR ';'
WITH row
where row.Lundi = "x"
...

cdu8...@gmail.com

unread,
May 18, 2015, 5:32:04 PM5/18/15
to ne...@googlegroups.com
Michael,

Thanks and sorry for that again, i'm lost with that :

WITH is required between CREATE and UNWIND (line 14, column 1 (offset: 386))
"unwind range(0,size(scripts)-2) as idx"

Best Regards,

Fred

Michael Hunger

unread,
May 18, 2015, 6:17:20 PM5/18/15
to ne...@googlegroups.com
LOAD CSV WITH HEADERS FROM 'file:c:/Users/Fred/Desktop/cgp2.csv' AS row FIELDTERMINATOR ';'
with row
where row.Lundi = "x"

match (d:Day {name:"Lundi"})
with d,row.Serveur as serverName, collect(row.Script) as scripts
match (s:Server {name:serverName})
merge (d)-[:PROCESS]->(s)
with s,head(scripts) as first, scripts
match (scr1:Script {name: first})
create (s)-[:EXECUTION]->(scr1)
with scripts
unwind range(0,size(scripts)-2) as idx
MATCH (a:Script {name:scripts[idx]})
MATCH (b:Script {name:scripts[idx+1]})
CREATE (a)-[:PUIS]->(b)

cdu8...@gmail.com

unread,
May 18, 2015, 7:23:46 PM5/18/15
to ne...@googlegroups.com
No error this time, but it doesn't work since when i do a match n return n, it returns 0 rows.

Michael Hunger

unread,
May 18, 2015, 7:26:40 PM5/18/15
to ne...@googlegroups.com
try this:

LOAD CSV WITH HEADERS FROM 'file:c:/Users/Fred/Desktop/cgp2.csv' AS row FIELDTERMINATOR ';'
return row limit 5

and look at the data. Are there spaces around you'r 'x' ?

cdu8...@gmail.com

unread,
May 22, 2015, 5:13:28 AM5/22/15
to ne...@googlegroups.com
Hi Michael,

Here is the result int attachment.
It looks fine.

Best regards,

Fred
screenshot.jpg
Reply all
Reply to author
Forward
0 new messages