cypher中每行的拆分操作

时间:2015-11-03 20:05:51

标签: neo4j cypher

我有这个查询 -

LOAD CSV from "file:/C:/1m/movies1.csv" AS rows 
FIELDTERMINATOR ';' unwind rows as row
foreach (r in row|
SPLIT(r[3],"|") as genre
merge (c:Genres { name: genre }) 
merge (m:Movies { MovieID: row[1] }) 
MERGE (m)-[:HAS_GENRE]->(c))

行是这样的 -

1;Toy Story (1995);Animation|Children's|Comedy
2;Jumanji (1995);Adventure|Children's|Fantasy
3;Grumpier Old Men (1995);Comedy|Romance
4;Waiting to Exhale (1995);Comedy|Drama

所以每行的第三个元素都有多个值,我想用'|'来展开分隔符。但我得到错误 -

Invalid input 'P': expected 't/T' or 'e/E' (line 4, column 2 (offset: 108))
"SPLIT(r[3],"|") as genre"

如何拆分每行中的第三项并将它们合并到一个节点?

1 个答案:

答案 0 :(得分:5)

一般来说,FOREACH的东西可以用UNWIND完成。

LOAD CSV from "file:/C:/1m/movies1.csv" AS rows FIELDTERMINATOR ';'
MERGE (movie:Movie {MovieID:rows[0]})
SET movie.name = rows[1]
WITH movie, SPLIT(rows[2], "|") AS genres
UNWIND genres AS g
MERGE (genre:Genre {name:g})
MERGE (movie)-[:HAS_GENRE]->(genre)
RETURN movie, genre;

收率:

enter image description here

您还指的是rows[3],它是null。 csv文件只有3列,其中第三列是rows[2]访问的。