I have a dataset that looks like:
id s01 s02 s03 s04 s05 s06 s07 s08 s09 s10
U01 1 0 0 0 0 0 0 0 0 1
U02 1 0 0 0 0 0 0 0 0 0
U03 1 0 0 0 0 0 0 0 0 0
U04 0 1 0 0 0 0 0 0 0 0
U05 0 1 0 0 0 0 0 0 0 0
U06 0 0 1 0 0 0 0 0 1 0
U07 0 0 1 0 0 0 0 0 0 0
U08 0 0 1 0 0 0 0 0 0 0
U09 0 0 1 1 0 0 0 0 0 0
U10 0 0 0 1 0 0 0 0 0 0
U11 0 0 0 1 1 0 0 0 0 1
U12 0 0 0 0 1 0 0 0 0 0
U13 0 0 0 0 1 1 0 0 0 0
U14 0 0 0 0 0 1 1 0 0 0
U15 0 0 0 0 0 0 1 0 0 0
U16 0 0 0 0 0 0 1 1 0 0
U17 0 0 0 0 0 1 0 1 0 0
U18 0 0 0 0 0 0 0 1 0 0
U19 0 0 0 0 0 0 0 1 1 0
U20 0 1 0 0 0 0 0 0 1 0
I want to import it into Neo4j, the nodes are U01, U02, U03 ... and s01, s02, ...., s10
The nodes have already been created. Now I want to create relationships based on the values (0/1): if it is 1 then there should be a relationship between the corresponding nodes.
Like for 2nd row and 2nd column the value is 1, so that means I have to create a relationship between U01
and S01
and for 2nd row 3rd column the value is 0, so I will skip that value.
So far, I was trying to come up with a hardcoded solution:
LOAD CSV WITH HEADERS FROM file:///new.csv" AS line FIELDTERMINATOR '\t'
WITH line
MATCH (a:Users)
WHERE a.user_id = line.id
WITH line, a
RETURN CASE
WHEN TOINT(line.s01)=1 THEN CREATE (a)-[:watches]->(c:NewsMedia{ID:"s01"})
WHEN TOINT(line.s02)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s02"})
WHEN TOINT(line.s03)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s03"})
WHEN TOINT(line.s04)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s04"})
WHEN TOINT(line.s05)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s05"})
WHEN TOINT(line.s06)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s06"})
WHEN TOINT(line.s07)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s07"})
WHEN TOINT(line.s08)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s08"})
WHEN TOINT(line.s09)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s09"})
WHEN TOINT(line.s10)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s10"})
END
The error is coming in the conditional MERGE part, can we not create relationships in WHEN <condition> THEN MERGE ()-[]->()
1) I think that you can not use MERGE
inside the CASE
.
2) To avoid hardcored solution
you can use UNWIND
and KEYS
:
LOAD CSV WITH HEADERS FROM "file:///new.csv" AS line FIELDTERMINATOR '\t'
MERGE (a:Users {user_id: line.id})
WITH a, line
UNWIND KEYS(line) AS bid
WITH a, bid WHERE "id" <> bid AND TOINTEGER(line[bid]) = 1
MERGE (b:NewsMedia {ID: bid})
MERGE (a)-[r:watches]->(b)
RETURN a, r, b
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.