0
votes

I need to upload a CSV file that has functions which need to be executed in Neo4j to form relationship among nodes that are already present. I am using a Python script to upload the file. I am using the following code to achieve this -

result_relationships = db.run( "LOAD CSV WITH HEADERS FROM 'file:///New_Relationships.csv' AS Relation" " return *")

This is not creating any relationship in Neo4j. My CSV file looks similar to the following picture - enter image description here

I also tried reading the CSV line by line and executing the cypher query one by one. Following is the code -

with open('New_Relationships.csv', 'rt') as f:
reader = csv.reader(f)
for row in reader:
    result = db.run(row)

This also did not help.

1
This really isn't the right way to process CSV files, especially if each "function" (by which you're actually referring to a Cypher clause) is the same on each row. Instead, use 2 columns, the num for a, the num for b, then craft your query to match on both and create the relationship. If you DO have different cypher clauses per row, which you can't simplify down to a different means of import, then you may want to look at APOC Procedures, specifically apoc.cypher.doIt(), which will execute cypher snippets.InverseFalcon
Your CSV file is not an importable file - that's not what the CSV Import feature works with. For a file like that, you'd have to write custom code to read/parse that file and figure out a way to execute everything correctly. The Neo4j docs have plenty of information on loading data from CSV - I'd suggest starting there, to see how to have raw data in your csv, and cypher logic that acts upon that data.David Makogon
@DavidMakogon I have gone through the docs and various other articles. This is what I got. Can you please share something that can be of help to me?TeeKay

1 Answers

1
votes

This is normal, your cypher script do nothing with the CSV data. You just return all the data of the CSV, so you simply read the file.

I don't know the structure of your file, but youo do something like this :

LOAD CSV WITH HEADERS FROM 'file:///New_Relationships.csv' AS relation
MATCH (start:Node {id:relation.start})
MATCH (end:Node {id:relation.end})
CREATE (start)-[:MY_REL]->(end);

I recommend you to read the LOAD CSV documentation, and to take a look to the examples : https://neo4j.com/docs/developer-manual/current/cypher/clauses/load-csv/