Nitin Kumar
Nitin Kumar

Reputation: 381

Cypher load csv with python

I have established a graph database using batch import tool for Neo4j. The data I am working on is generated every day so I need to update my graph database daily. Running the batch import is not a solution as it will clean and upload entire data everyday. I am trying to use the load csv cypher function using python to do so however I am not able to.

ex = USING PERIODIC COMMIT LOAD CSV FROM myfile.csv AS row FIELDTERMINATOR ';'
ex += MERGE(:a{b:row[0],d:row[1]})

I am trying to run this command using py2neo's as

graph = py2neo.Graph()
graph.cypher.execute(ex)

I get an error Invalid input 'G' : expected whitespace, comment, "...string..." or a parameter (line 1, column 37 (offset 36))

Using this link I understand that I have to look over the rows and then I can upload data using py2neo. https://codereview.stackexchange.com/questions/75842/optimizing-for-data-import-in-neo4j-using-py2neo

Is there a way to run load csv directly using python over an a csv? Writing look is not a problem however, this adds on to extra computation.

Thanks in advance for answers :)

Upvotes: 2

Views: 7794

Answers (1)

William Lyon
William Lyon

Reputation: 8546

You might have an issue with not adding whitespace at the end of the first line and appending the second line. It's hard to know for sure since I don't see the quotes in your sample code. Try using Python's multiline string syntax to construct your query:

query = '''
USING PERIODIC COMMIT 
LOAD CSV FROM 'file:///path/to/myfile.csv' AS row FIELDTERMINATOR ';'
MERGE (:Person {name: row[1], age: row[2]})
'''

graph.cypher.execute(query)

Also, note that to reference a file on the local file system you should use the file:/// format.

Upvotes: 7

Related Questions