Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
05-16-2020 06:16 AM
Hello
I hope you all are doing well. I'm using for the moment py2neo however I'm not dead set on it. I'm trying to "speed up" my process, I have realized that py2neo is fairly slow so I'm trying my luck with some code magic...
From these benchmarks it appears that unwind is the fastest way to do stuff with db so here's my try...:
example = {}
example["name"] = "someName"
example["type"] = "Pet"
example["atr"] = "lel"
petArray = 100 * [example]
print("made my array of dicts", petArray)
tx.run('unwind $mapEntry as mItem create (n:mItem["type"] {name:mItem["name"], art:mItem["atr"]})', mapEntry=petArray)
tx.commit()
Now I'm getting the >
py2neo.database.ClientError: SyntaxError: Invalid input '[': expected an identifier character, whitespace, NodeLabel, a property map, ')' or a relationship pattern (line 1, column 42 (offset: 41))
"unwind $mapEntry as mItem create (n:mItem["type"] {name:mItem["name"], art:mItem["atr"]})"
Would any1 be so kind to help me out with it ?
Solved! Go to Solution.
05-16-2020 06:38 AM
No problem, it's always a pleasure:)
This is a solution with APOC plugin:)
tx.run(''' UNWIND $mapEntry AS mItem
CALL apoc.create.node([mItem["type"]], {name:mItem["name"], art:mItem["atr"]})
YIELD node
RETURN node'''
, mapEntry=petArray)
tx.commit()
05-16-2020 06:22 AM
Hello,
I think it's not possible to edit the label as property:)
tx.run('UNWIND $mapEntry AS mItem CREATE (n:Pet {name:mItem['name'], art:mItem['atr']})", mapEntry=petArray)
This is the link from the doc: https://neo4j.com/docs/cypher-manual/4.0/syntax/parameters/
05-16-2020 06:29 AM
But there is a solution if you need to do it dynamically:)
Doc: https://neo4j-contrib.github.io/neo4j-apoc-procedures/3.5/nodes-relationships/data-creation/
CALL apoc.create.node(['Label'], {key:value,…})
05-16-2020 06:30 AM
Hey
Thank you for both replies!
I've tweaked my code for the moment to reflect this, as I though I will have to split my maps per "type" and batch individual type 1 by 1... I'll read on the CALL apoc part next!
So far I tweaked the code to be this >
tx.run(''' unwind $mapEntry as mItem
CREATE (n:Pet)
SET n = mItem
RETURN n'''
, mapEntry=petArray)
tx.commit()
As one line code breaks my brain :- )
05-16-2020 06:38 AM
No problem, it's always a pleasure:)
This is a solution with APOC plugin:)
tx.run(''' UNWIND $mapEntry AS mItem
CALL apoc.create.node([mItem["type"]], {name:mItem["name"], art:mItem["atr"]})
YIELD node
RETURN node'''
, mapEntry=petArray)
tx.commit()
05-16-2020 06:46 AM
Oh my gash thats amazing! Now I gotta read on apoc :- )Thank you!
Wrapping up as the problem has been solved.
Regards
Dariusz
05-16-2020 06:47 AM
One last thing, if you can't do it with Cypher, it's maybe possible with APOC:)
Thank you:)
Regards,
Cobra
All the sessions of the conference are now available online