Neo4j cypher – 导入CSV并根据csv值添加节点之间的关系

在cypher中是否有可能使用导入功能基于csv的值在节点之间创建不同的关系.

例如:
对于给定的csv数据

product_id  user_id  action
1           1        VIEW
1           2        PURCHASE

我需要根据csv中’action’字段的值创建产品节点(product_id),用户节点(user_id)以及’user’和产品节点之间的VIEW或PURCHASE关系.

以下是我尝试过的方法.我知道语法不正确但只是想知道我想要做什么.

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file://pathto/product-event.csv" AS csvLine 
FIELDTERMINATOR ','
MERGE ( prod:Product{pid: toInt(csvLine.product_id)} )
MERGE ( usr:User { cid: toInt(csvLine.userid)  })
WITH prod,usr , CASE  
    WHEN csvLine.action ='VIEW' THEN  'VIEW'
    WHEN csvLine.action ='PURCHASE' THEN  'PURCHASE'
    ELSE 'VIEW' END AS action
MERGE (usr)-[:action]->(prod)



USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///pathto/product-event.csv" AS csvLine 
FIELDTERMINATOR ','
MERGE ( prod:Product {pid: toInt(csvLine.productid) } )
MERGE ( usr:User { cid: toInt(csvLine.userid) }
    )
ON CREATE SET 
    prod.created=timestamp()
WHERE csvLine.action = 'VIEW'
MERGE (cust)-[:VIEW]->(prod)
WHERE csvLine.action = 'PURCHASE'
MERGE (usr)-[:PURCHASE]->(prod)

可能有简单的方法来做到这一点.但我没有在网上得到任何参考.谢谢.

您不能拥有动态关系类型.但是,通过有条件地填充具有1或0个元素的数组并应用FOREACH,有一种解决方法:

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file://pathto/product-event.csv" AS csvLine 
FIELDTERMINATOR ','
MERGE ( prod:Product{pid: toInt(csvLine.product_id)} )
MERGE ( usr:User { cid: toInt(csvLine.userid)  })
FOREACH(ignoreMe IN CASE WHEN csvLine.action='VIEW' THEN [1] ELSE [] END | 
    MERGE (usr)-[:VIEW]->(prod) )
FOREACH(ignoreMe IN CASE WHEN csvLine.action='PURCHASE' THEN [1] ELSE [] END | 
    MERGE (usr)-[:PURCHASE]->(prod) )

另见http://www.markhneedham.com/blog/2014/08/22/neo4j-load-csv-handling-empty-columns.

相关文章
相关标签/搜索