1
votes

I am populating Neo4j by csv import (py2neo batch import). I want to create relationship by using the existing nodes generated from csv files. In my current csv file, the "sheet_name" column lists the names of existing csv files and "row_name" column shows nodes generated from related csv file.

for row in reader:
            if row:
                name = strip(row[0])
                created_by = strip(row[1])
                barcode = strip(row[3])
                sheet_name = strip(row[4])
                row_name = strip(row[5])

                query = """
                    merge (document:Document {name:{a}})
                    merge (sheet_name:Sheet_Name {sheet_name:{b}})
                    merge (to_deliver:To_Deliver {row_name:{c}}) #This node was created before from other csv file
                    merge (delivered_Delivered {row_name:{c}}) #This node was created before from other csv file
                    CASE WHEN sheet_name.value = 'Pending' THEN MERGE (document)-[:STATUS_IS]->(to_deliver)
                    CASE WHEN sheet_name.value = 'Completed' THEN MERGE (document)-[:STATUS_IS]->(delivered)
                   """
                batch.append(query, {"a": name, "b": created_by, "c": barcode, 
                                  "d": sheet_name, "e": row_name})
                i += 1
                j += 1
            batch.process()

I want to create the relationship based on

--if the value of "sheet_name" is Pending, then document status is "to_deliver"-- and;

--if the value of "sheet_name" is Completed, then document status is "delivered"--

"to_deliver" and "delivered" nodes were created before from other csv files. When i run the code, I get an error like Invalid input 'A': expected 'r/R' Is CASE..THEN appropriate for conditional statements? Thanks

1

1 Answers

1
votes

Doing conditional write operations in Cypher is a bit tricky. You can use FOREACH in combination with a CASE WHEN to iterate over a 1 element array and do the action if the condition is true.

This concept is very well explained at http://www.markhneedham.com/blog/2014/06/17/neo4j-load-csv-handling-conditionals/.