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

时间:2018-01-17 10:28:46

标签: csv neo4j cypher

我有一个看起来像的数据集:

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

我想将它导入Neo4j,节点是U01,U02,U03 ......和s01,s02,....,s10

节点已经创建。现在我想基于值(0/1)创建关系:如果它是1那么相应节点之间应该存在关系。

与第2行和第2列类似,值为1,这意味着我必须在U01S01之间建立关系,对于第2行第3列,值为0,所以我将跳过那个值。

到目前为止,我试图想出一个硬编码的解决方案:

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

错误发生在条件MERGE部分,我们不能在WHEN <condition> THEN MERGE ()-[]->()

中创建关系

1 个答案:

答案 0 :(得分:0)

1)我认为您无法在MERGE内使用CASE

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