简体   繁体   English

在将CSV导入Neo4j时创建条件关系

[英]Creating conditional relationship while importing CSV into Neo4j

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 我想将其导入Neo4j,节点为U01,U02,U03 ...和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. 现在,我想基于值(0/1)创建关系:如果为1,则对应的节点之间应该存在关系。

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. 就像第二行和第二列的值是1一样,这意味着我必须在U01S01之间创建关系,而对于第二行第三列的值是0,所以我将跳过该值。

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 ()-[]->() 错误出现在条件MERGE部分中, WHEN <condition> THEN MERGE ()-[]->() ,是否可以创建关系?

1) I think that you can not use MERGE inside the CASE . 1)我认为您不能在CASE使用MERGE

2) To avoid hardcored solution you can use UNWIND and KEYS : 2)为了避免hardcored solution您可以使用UNWINDKEYS

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM