Hope someone can help me, trying to find a way to do MySQL’s INSERT – ON DUPLICATE KEY UPDATE in Aerospike but not sure if there’s a native way to do that or if it must be done with user defined functions or checking it in the app logic.
What I’m tryin to do is insert a counter that increments on a certain action for a user_id on a certain category_id, the table on mysql is as follows:
category_id | user_id | counter
And my SQL for the operation is like this:
“INSERT INTO counter_table(category_id, user_id, counter) VALUES (‘xx’,‘yy’,1) ON DUPLICATE KEY UPDATE counter = counter + 1;”
Most key/value databases support this natively. What you’re looking for is called an Increment. With Aerospike this is also called the Add operation, which you can read more about here:
Just send an Add operation with the key being Category ID + User ID and a bin holding the count. If the key doesn’t exist, Aerospike will create it on the first increment. Afterwards, every new increment for that key will just update that bin value by 1 (or whatever integer value you pass).
Following is the snippet of code you can test using java client:
WritePolicy policy = new WritePolicy();
// Write a single value.
Key key = new Key("test", "", "xyz");
//Write a record
Bin bin = new Bin("default", 1);
Record record = client.operate(policy, key, Operation.add(bin), Operation.get());
Thanks, that’s exactly what I need. And from what I understand if I want to be able to retrieve all the counts per user regarding a single category id I just need to create another bin in the same record with the category id an create a secondary index on that bin right?
However I’d recommend doing the bin and index on a user ID because that’s what you would query by. Otherwise you would get back all users that have a category ID which could be a lot of data if you have millions of users (if that’s how your app works).
I checked that, but the increment method only seems to accept the options listed in my previous post, so there wasn’t much to do by using one of the options. But after what rbotzer posted, it is all clear now, the record must exist, the bin can be created with the increment action if it doesn’t exist but it will not create non-existent records.
Increment works on a bin in an existing record. If you didn’t first put() the record in its place, you’d get an error status.
I was trying to do this on a single call with operate using write and then increment, but the write operation on operate also fails if the key doesn’t exist, so I guess what I’m showing above is the only way to do it, please correct me if I’m wrong or if there’s a better way.
The PHP client is consistent with the other clients that are wrapped around the C client, such as Python and Node.js. The .Net client is ‘native’ (i.e. it implements all levels from connection pooling, cluster tending, wire protocol, etc). It has a different interface.
Definitely something we’ll consider in future iterations. For now I’m making the documentation for the bin operations more clear.