csv import neo4j。如何引用foreach之外的节点

时间:2015-11-13 12:38:44

标签: neo4j cypher

下面的

是密码导入脚本

LOAD CSV WITH HEADERS FROM 'sample.csv' AS line
CREATE (transaction:Transaction{trxid:TOINT(line.COMBINED)})
MERGE (party:Party{partyname:UPPER(line.OPARTY)})
FOREACH (bnk1 IN (CASE WHEN line.BANK_ID_1 IS NULL THEN [] ELSE [line.BANK_ID_1] END) |
MERGE (bank1:Bank{bankname:bnk1})
//relation
MERGE (transaction)-[:Trough]->(bank1)
)
FOREACH (bnk2 IN (CASE WHEN line.BANK_ID_2 is NULL THEN [] ELSE [line.BANK_ID_2] END) |
MERGE (bank2:Bank{bankname:bnk2})
//relation
)
FOREACH (bnk3 IN (CASE  WHEN line.BANK_ID_3  is NULL THEN  [] ELSE [line.BANK_ID_3] END) |
MERGE (bank3:Bank{bankname:bnk3})
//relation
MERGE (bank2)-[:next]->(bank3)
)
MERGE (party)-[:Initiated]->(transaction)

MERGE (transaction)-[:Trough]->(bank1)
MERGE(bank1)-[:next]->(bank2)
MERGE (bank2)-[:next]->(bank3)

我正在尝试创建bank1和bank2之间的关系.am无法创建,因为bank1和bank2节点是在foreach中创建的。请帮助解决这个问题

sample.csv 结合起来,OPARTY,BANK_ID_1,BANK_ID_2,BANK_ID_3 1,阿伦,蟒,轴 2,雷迪毫升,, ICICI

我想要结果

reddy->2->ml->[]->icici

这里reddy发起了trx 2 thru ml银行,然后空银行,最后到了icici

arun ->1->boa->axis->[]

1 个答案:

答案 0 :(得分:0)

我看到了两种方法。您可以使用WITH传递数组,然后将UNWIND传递给它:

LOAD CSV WITH HEADERS FROM 'sample.csv' AS line
CREATE (transaction:Transaction{trxid:TOINT(line.COMBINED)})
MERGE (party:Party{partyname:UPPER(line.OPARTY)})
WITH (CASE WHEN line.BANK_ID_1 IS NULL THEN [] ELSE [line.BANK_ID_1] END) AS bank_ids_1
UNWIND bank_ids_1 and bank_id_1
MERGE (bank1:Bank{bankname:bank_id_1})

或者您可以多次浏览CSV:

LOAD CSV WITH HEADERS FROM 'sample.csv' AS line
CREATE (transaction:Transaction {trxid:TOINT(line.COMBINED)});

LOAD CSV WITH HEADERS FROM 'sample.csv' AS line
WHERE NOT(line.BANK_ID_1 IS NULL)
MERGE (:Bank{bankname:line.BANK_ID_1});

LOAD CSV WITH HEADERS FROM 'sample.csv' AS line
WHERE NOT(line.BANK_ID_2 IS NULL)
MERGE (:Bank{bankname:line.BANK_ID_2});

LOAD CSV WITH HEADERS FROM 'sample.csv' AS line
WHERE NOT(line.BANK_ID_1 IS NULL) AND NOT(line.BANK_ID_2 IS NULL)
MATCH
  (bank1:Bank{bankname:line.BANK_ID_1}),
  (bank2:Bank{bankname:line.BANK_ID_2})
MERGE(bank1)-[:next]->(bank2);

等......当然,这是一个粗略的例子。

就个人而言,我喜欢第二种选择,因为它具有可理解性。