Cypher: Bulk update of a property on many nodes

804 views
Skip to first unread message

Luanne Coutinho

unread,
Feb 26, 2013, 11:12:55 AM2/26/13
to ne...@googlegroups.com
Hi,

I have a bunch of item nodes and each node has a property called "quantity". I have a map with data {itemId, quantity}
Is there a good way to just run one query and update the quantity for each specific item? Or do I have to do this item by item?

Using 1.8.1.

Thanks
Luanne

Peter Neubauer

unread,
Feb 26, 2013, 5:29:26 PM2/26/13
to Neo4j User
Luanne,
well, maybe the easiest way is to generate Cypher SET statements from this and execute them via the neo4j-shell?

/peter


Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

Neo4j 1.8 GA - http://www.dzone.com/links/neo4j_18_release_fluent_graph_literacy.html



Luanne

--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Luanne Coutinho

unread,
Feb 26, 2013, 10:34:36 PM2/26/13
to ne...@googlegroups.com
Hi Peter,

This is data I receive from another service, and I've got to update it in our Neo4j db as part of a data sync process.
I could definitely generate multiple Cyphers and have them executed- was just wondering if it could be done using one cypher. The number of items to be updated in the batch will be 5000+, so that's a lot of queries to execute in turn.

Regards
Luanne

Peter Neubauer

unread,
Feb 27, 2013, 1:38:50 AM2/27/13
to Neo4j User
Luanne,
I am thinking that if you use the neo4j shell, you can actually do BEGIN/COMMIT thus having maybe just one transaction. With parameters (Michaels is adding that right now) in that script, even the Cypher exectuion shoudl not be costly. See https://github.com/neo4j/neo4j/pull/560, should be in shortly.

What do the say, Michael?

/peter


Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

Neo4j 1.8 GA - http://www.dzone.com/links/neo4j_18_release_fluent_graph_literacy.html


Luanne Coutinho

unread,
Feb 27, 2013, 6:02:14 AM2/27/13
to ne...@googlegroups.com
Dumb question--how do I do this from my web application? I am using embedded, running in Tomcat. The flow is: there's an activeMQ consumer that receives a message containing this data. We've got to pick it up and update the graph.
I'm missing something?

Thanks
Luanne

Peter Neubauer

unread,
Feb 27, 2013, 6:08:22 AM2/27/13
to Neo4j User
In one message, you get a Map with all the to-be-updated properties?


Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

Neo4j 1.8 GA - http://www.dzone.com/links/neo4j_18_release_fluent_graph_literacy.html


Luanne Coutinho

unread,
Feb 27, 2013, 6:29:02 AM2/27/13
to ne...@googlegroups.com
Yes. Key=id of the entity (already indexed in our graph), value=property value to be set

Peter Neubauer

unread,
Feb 27, 2013, 7:33:33 AM2/27/13
to Neo4j User
Maybe generate a bunch of queries, like

start n = node:entities(id={key}) set n.property={value}

key and value as parameters, then loop over all the entries, and just modify the parameters for each with the same statement, open a transaction around all of them in embedded Java? Cypher does not handle TX on its own, that is a concern of the surrounding system.


/peter


Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

Neo4j 1.8 GA - http://www.dzone.com/links/neo4j_18_release_fluent_graph_literacy.html


Luanne Coutinho

unread,
Feb 27, 2013, 7:37:57 AM2/27/13
to ne...@googlegroups.com
Yeah, that's what I ended up doing...thought I'd ask here too in case you guys had any magic :-)

Thanks Peter.

Peter Neubauer

unread,
Feb 27, 2013, 7:46:48 AM2/27/13
to Neo4j User
Probably Michael or Wes can do some crazy stuff with the collection support in cypher, but this would be my Joe Corporate approach first ;)

/peter

Michael Hunger

unread,
Feb 27, 2013, 1:49:18 PM2/27/13
to ne...@googlegroups.com
How many are those?

You can do crazy stuff with

Pass two params, a list of id's to start
And a list of tuples id,value

And then
Start n=node({ids})
Set n.foo=last(head(filter(t in {tuples} : id(n)=head(t)))

Dunno if it is faster ;)

Mostly more complicated



Sent from mobile device

Am 27.02.2013 um 13:46 schrieb Peter Neubauer <peter.n...@neotechnology.com>:

Probably Michael or Wes can do some crazy stuff with the collection support in cypher, but this would be my Joe Corporate approach first ;)

/peter

--

Luanne Coutinho

unread,
Mar 1, 2013, 6:53:47 AM3/1/13
to ne...@googlegroups.com
That looks really interesting- I'll amuse myself by deciphering it this weekend :-)
Thanks Michael!
Reply all
Reply to author
Forward
0 new messages