2015-02-06 89 views
1

我有一个movies.csv文件,每行有一个特征向量(例如 - id |名称| 0 | 1 | 1 | 0 | 0 | 0 | 1有2个名称和特征的功能id,用于流派分类的7个特征)neo4j中循环数据值

我想要类Movies中的节点m与来自类流派的节点g具有关系[:HAS_GENRE]。为此,我需要遍历所有'|'分离的特点,只有真正的关系,如果值是1

从本质上讲,我想有 -

x = a //where a is the index of the first genre feature 

while (x < lim) //lim is the last index of the feature vector 
{ 
if line[x] is 1: 
    (m{id:toInt(line[0]})-[:HAS_GENRE]->(g{id=line[x]}) 

} 

我该怎么办呢?

+0

我知道匹配和创建构造,只是没有关于循环部分的想法。 – goelakash 2015-02-06 14:27:58

+0

你有一个.csv文件,其中有一个管道分隔的列? – 2015-02-07 01:27:07

+0

所有列都由管道分隔 – goelakash 2015-02-07 09:22:19

回答

0

试试这个

WITH ["Genre1","Genre2",...] as genres 
LOAD CSV FROM "file:movies.pdv" using fieldterminator "|" AS row 
MERGE (m:Movie {id:row[0]}) ON CREATE SET m.title = row[1] 
FOREACH (idx in filter(range(0,size(genres)-1) WHERE row[2+idx]="1")) | 
    MERGE (g:Genre {name:genres[idx]}) 
    CREATE (m)-[:HAS_GENRE]->(g) 
) 
  • 它加载作为一个集合
  • 前两个元素被用来创建影片
  • 然后通过过滤器的潜在指标range(0,size(genres)-1)的文件的每一行在输入行中存在“1”,
  • 然后将得到的索引列表用于查找流派名称或编号
  • 并连接电影与流派
+0

当我在[]中为流派添加“匹配(g:流派)返回g”时,它不起作用。我想要动态检索列表,我该怎么做?我已经有一个(:流派)节点的列表。 – goelakash 2015-02-08 12:26:12