cancel
Showing results for 
Search instead for 
Did you mean: 

What is the best way to do batch insert/create/merge by using py2neo?

mading0817
Node Clone

Since I have read a very nice topic from Michael Hunger, I was trying to make it works with py2neo.

Then I tried to set the parametes by sending a Cypher statement as followed:

cypher = ':param batch:[{name:"Alice",age:32},{name:"Bob",age:42}]'
graph.run(cypher)

This will cause a error as followed:

py2neo.database.ClientError: SyntaxError: Invalid input ':': expected <init> (line 1, column 1 (offset: 0))

But if just pasted the cypher onto the neo4j browser, it will just set the parameters successfully.
Can anyone tell me what is wrong with that and is there any other ways to make the insertion more efficient and fast in python?

By the way, the neo4j server reads data from mysql dynamically in real time, so that I can keep both databases' data synchronous.

Thanks!

8 REPLIES 8

benjamin_squire
Graph Voyager

this should work:
':param batch=> [{name:"Alice",age:32},{name:"Bob",age:42}]'

Thanks, but I tried this way and it not works..

I asked this a while back: How does Py2Neo use parameters to make repeat queries faster?

try:

cypher = 'merge (u:User {name:{name1},age:{age1}}) merge (u2:User {name:{name2},age:{age2}})'
graph.run(cypher, {"name1":"Alice","name2":"Bob","age1":32,"age2":42})

Hi, this did work, here is my code:

batch = [{name:"Alice",age:32},{name:"Bob",age:42}]
graph.run(query, rel_batch)

Thanks

technige
Neo4j
Neo4j

Your original syntax uses a client command, which exists only in applications such as the browser and the Cypher shell; these commands are not part of Cypher itself. The final code you posted it indeed the correct way to do this: passing the parameters along with the Cypher query string as a second argument.

I see the difference between browser and Cypher itself. Thanks sooo much!

1634644550
Node

@mading0817
batch = [{name:"Alice",age:32},{name:"Bob",age:42}]
graph.run(query, rel_batch)

Hello, can you explain, what exactly does query and rel_batch mean? Can you give an example?

Hi, Im sorry there is a mistake, it shoule be:

batch = [{name:"Alice",age:32},{name:"Bob",age:42}]

query = """
UNWIND {{batch}} as row
.....
your query-
.....
""".format(batch=batch)

graph.run(query, batch=batch)

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.