cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Using unwind and python dict to populate database

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 ?

1 ACCEPTED SOLUTION

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()

View solution in original post

6 REPLIES 6

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/

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,…​})

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 :- )

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()

Oh my gash thats amazing! Now I gotta read on apoc :- )Thank you!

Wrapping up as the problem has been solved.

Regards
Dariusz

One last thing, if you can't do it with Cypher, it's maybe possible with APOC:)

Thank you:)

Regards,
Cobra