Neo4j cypher-导入CSV并基于csv值添加节点之间的关系 [英] Neo4j cypher - Import CSV and add relationships between nodes based on the csv values
问题描述
在cypher中,是否有可能使用导入功能基于csv的值在节点之间创建不同的关系.
Is it possible in cypher to create different relationship between nodes based on the value of csv using the import functionality .
例如: 对于给定的CSV数据
Eg : For a given csv data
product_id user_id action
1 1 VIEW
1 2 PURCHASE
我需要基于以下参数创建产品节点(product_id),用户节点(user_id)以及' user '和 product 节点之间的VIEW或PURCHASE关系csv中的"操作"字段.
I need to create product node(product_id) , user node(user_id) and either VIEW or PURCHASE relationship between 'user' and product node based on the value of 'action' field in csv .
下面是我尝试过的方法.我知道语法是不正确的,但只是为了让我了解我要做什么.
Below are the approaches I tried . I know the syntax is incorrect but just to give an idea what I am trying to do .
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)
可能有简单的方法来做.但我没有在线获得任何参考.谢谢.
There may be simple way to do it . but I am not getting any reference online. Thanks.
推荐答案
您不能具有动态关系类型.但是,有一种解决方法是有条件地填充一个包含1个或0个元素的数组并应用FOREACH
:
You cannot have dynamic relationship types. However there is a workaround by populating conditionally an array with 1 or 0 elements and applying 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 .
这篇关于Neo4j cypher-导入CSV并基于csv值添加节点之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!