1
votes

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
What have you tried to achieve your wanted results? What has your research concerning your problem shown? Can you provide code of your tries? How do I ask a good question, How much research effort is expected, and How to create a Minimal, Complete, and Verifiable example might be helpful to improve your question. - peacetype
@peacetype you cant help me that's fine, but why the downvoting? because it was too difficult for you? - Sankar
I presume the reason for downvoting was that the question is sloppily formatted and proposes a problem that is difficult to reproduce. Work on those issues and users are typically happy to revert the downvotes. For example, you can copy some rows from the CSV file (instead of sharing a screenshot), work on the grammar of the and in general, follow the guidelines for creating a good question as linked by @peacetype. - Gabor Szarnyas
Thanks @peacetype and Gabor I have updated what you have asked for, sorry I am new to this so still learning how to create a good question. - Sankar
I improved the question a bit more, please check. - Gabor Szarnyas

1 Answers

0
votes

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