使用多次调用 apoc.do.when
Use many call apoc.do.when
我想创建电影节点并从 csv 创建电影和流派之间的关系
CSV 文件:
id|标题|发行|动作|冒险|动画|儿童|喜剧|犯罪|纪录片|剧情|奇幻|film-Noir|恐怖|音乐|悬疑|爱情|sci-Fi|惊悚|war|西方
1|玩具总动员 (1995)|1995 年 1 月 1 日|0|0|1|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0
2|黄金眼 (1995)|1995 年 1 月 1 日|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0
3|四个房间 (1995)|1995 年 1 月 1 日|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0
我的密码查询
LOAD CSV WITH HEADERS FROM 'https://www.dropbox.com/s/yho5i7kqocpkh9r/u.item.csv?dl=1' AS line FIELDTERMINATOR '|'
CREATE (n:Movie {id: toInteger(line.id),title: line.title, release: date(line.date)})
WITH line,n
CALL apoc.do.when(toInteger(line.action) = 1, 'MATCH(g:Genre) WHERE g.id = 1 MERGE ($n)-[IS]->(g)','', {n:n})
CALL apoc.do.when(toInteger(line.adventure) = 1, 'MATCH(g:Genre) WHERE g.id = 2 MERGE ($n)-[IS]->(g)','', {n:n})
....
对于每个流派(动作、冒险等),如果电影的流派等于 1,我会在流派节点和电影节点之间创建一个关系
你能帮帮我吗?
我建议另一种方法:
LOAD CSV WITH HEADERS FROM 'https://www.dropbox.com/s/yho5i7kqocpkh9r/u.item.csv?dl=1' AS line FIELDTERMINATOR '|'
// get all the genre names belonging to a movie
WITH line,[key IN keys(line) WHERE key <> "id" AND line[key] = "1" | key] AS genres
// MERGE the movie using the id, SET the other properties
MERGE (movie:Movie {id: toInteger(line.id)})
ON CREATE SET movie.title = line.title,
movie.release = date(line.date)
// loop through the genre names and MERGE the :Genre nodes when needed
FOREACH (genre IN genres |
MERGE (g:Genre {type:genre})
MERGE (movie)-[:IS]->(g)
)
对于前两行,它创建了这个:
我想创建电影节点并从 csv 创建电影和流派之间的关系
CSV 文件: id|标题|发行|动作|冒险|动画|儿童|喜剧|犯罪|纪录片|剧情|奇幻|film-Noir|恐怖|音乐|悬疑|爱情|sci-Fi|惊悚|war|西方 1|玩具总动员 (1995)|1995 年 1 月 1 日|0|0|1|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0 2|黄金眼 (1995)|1995 年 1 月 1 日|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0 3|四个房间 (1995)|1995 年 1 月 1 日|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0
我的密码查询
LOAD CSV WITH HEADERS FROM 'https://www.dropbox.com/s/yho5i7kqocpkh9r/u.item.csv?dl=1' AS line FIELDTERMINATOR '|'
CREATE (n:Movie {id: toInteger(line.id),title: line.title, release: date(line.date)})
WITH line,n
CALL apoc.do.when(toInteger(line.action) = 1, 'MATCH(g:Genre) WHERE g.id = 1 MERGE ($n)-[IS]->(g)','', {n:n})
CALL apoc.do.when(toInteger(line.adventure) = 1, 'MATCH(g:Genre) WHERE g.id = 2 MERGE ($n)-[IS]->(g)','', {n:n})
....
对于每个流派(动作、冒险等),如果电影的流派等于 1,我会在流派节点和电影节点之间创建一个关系
你能帮帮我吗?
我建议另一种方法:
LOAD CSV WITH HEADERS FROM 'https://www.dropbox.com/s/yho5i7kqocpkh9r/u.item.csv?dl=1' AS line FIELDTERMINATOR '|'
// get all the genre names belonging to a movie
WITH line,[key IN keys(line) WHERE key <> "id" AND line[key] = "1" | key] AS genres
// MERGE the movie using the id, SET the other properties
MERGE (movie:Movie {id: toInteger(line.id)})
ON CREATE SET movie.title = line.title,
movie.release = date(line.date)
// loop through the genre names and MERGE the :Genre nodes when needed
FOREACH (genre IN genres |
MERGE (g:Genre {type:genre})
MERGE (movie)-[:IS]->(g)
)
对于前两行,它创建了这个: