我有一个csv文件,其中包含以下电子邮件数据:
Id,To,CC
0,a@z.com,b@z.com
1,,
2,a@z.com;b@z.com;c@z.com,d@z.com;e@z.com
To
和CC
字段可以包含零个或多个地址,地址分为;
个。我想要做的是创建代表Id
的电子邮件的节点和代表Entity
的{{1}}关系的节点(我最终想要添加[:TO]
属性当它是CC时的关系)。文件中还有很多其他的东西,但这是我坚持的角落。
我正在做以下创建节点,这是有效的(我是Neo4j的新手,所以我不知道它是否优雅):
CarobonCopied
但我无法弄清楚的是如何建立关系。我想做一些像嵌套USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS
FROM 'file:///short_FieldCleanedEmails.csv' AS line
WITH line,
SPLIT(line.To, ';') AS tos,
SPLIT(line.CC, ';') AS ccs
// loop over to's
FOREACH (ignoreMe IN CASE WHEN trim(line.From) <> ""
THEN [1] ELSE [] END | FOREACH (addr in tos |
MERGE (entity_to:Entity {NameAddress: addr})))
// loop over cc's
FOREACH (ignoreMe IN CASE WHEN trim(line.CC) <> ""
THEN [1] ELSE [] END | FOREACH (addr in ccs |
MERGE (entity_cc:Entity {NameAddress: addr})))
;
调用的事情,但这不起作用。
我正在尝试这个:
FOREACH
并且它不起作用,因为我猜想嵌套USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS
FROM 'file:///short_FieldCleanedEmails.csv' AS line
WITH line,
SPLIT(line.To, ';') AS tos,
SPLIT(line.CC, ';') AS ccs
MATCH (email:Email {id: TOINT(line.Id)})
// loop over to's
FOREACH (ignoreMe IN CASE WHEN trim(line.From) <> ""
THEN [1] ELSE [] END | FOREACH (addr in tos |
MATCH (entity_to:Entity {NameAddress: addr}),
CREATE (email)-[:TO]->(entity_to)))
// loop over cc's
FOREACH (ignoreMe IN CASE WHEN trim(line.CC) <> ""
THEN [1] ELSE [] END | FOREACH (addr in ccs |
MATCH (entity_cc:Entity {NameAddress: addr}),
CREATE (email)-[t:TO]->(entity_cc)))
;
语句是违法的。(我之前创建了Email节点,在这里我还没有完成添加FOREACH
属性的步骤到CC的CarbonCopy
。)
有什么建议吗?
答案 0 :(得分:1)
使用filter()
代替外部FOREACH
。
FOREACH (addr IN FILTER(addr IN tos WHERE addr <> "")|
MERGE (entity_to:Entity {NameAddress: addr} )
MERGE (email) - [:TO] -> (entity_to))
或者使用UNWIND
并过滤表示法,它更清洁一些(特别是如果您习惯于列出理解):
UNWIND [x IN tos WHERE x <> ""] AS addr
MERGE (entity_to:Entity {NameAddress: addr} )
MERGE (email) - [:TO] -> (entity_to)
您还希望MERGE
entity_to
节点,因为如果查询已经存在,则在创建关系时会抛出错误。