Neo4j cypher - import CSV and add links between nodes based on csv values

Is it possible in cypher to create different relationships between nodes based on the csv value using the import function.

For example: For csv data

product_id  user_id  action
1           1        VIEW
1           2        PURCHASE

I need to create product node (product_id), user node (user_id) and either a VIEW or PURCHASE relationship between user and product node based on the value of the action field in csv.

Below are the approaches I tried. I know that the syntax is incorrect, but just to give an idea of ​​what I'm 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 an easy way to do this. but I do not get any links on the Internet. Thank.

+4
2

. , 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.

+7

, , :

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)  }) 
MERGE (usr)-[:action {type: csvLine.action}]->(prod) )
+1

Source: https://habr.com/ru/post/1569530/


All Articles