How do I replace a record invoking AerospikeClient#operate method?

I’ve got a record with an ordered list bin, the only access pattern for the set so far was to append values to the ordered list bin via AerospikeClient#operate method. I’ve stumbled upon a requirement to be able to completely replace a record. I’ve tried to pass a WritePolicy instance with RecordExistsAction.REPLACE recordExistsAction specified which resulted in an error:

com.aerospike.client.AerospikeException: Error 4,1,BB9020011AC4202 127.0.0.1 3000: Parameter error. 

The only option for me is to delete a record first and then call an operate method to append values to an ordered list, but I’d like to avoid an extra network call to the Aerospike server as much as possible. Are there any other options available to solve my issue?

I’ve been using Java Aerospike Client.

Thank you in advance. Cheers, Mike.

Do you need to replace the whole record or just reset the list bin to empty?

WritePolicy wPolicy = new WritePolicy(); 
wPolicy.recordExistsAction = RecordExistsAction.REPLACE;
Key key = new Key("test", "users", username); 

Bin bin1 = new Bin("interests",
              Arrays.asList(interests.split(",")));  //interests is string of csv e.g. "flying, hiking, walking"
   
client.put(wPolicy, key, bin1);

The above should work…

thank you all guys!

xorphox, yes, I need to replace the whole record.

pgupta, yes, AerospikeClient#put works well. I’m curios why AerospikeClient#operate fails.

Can you post a short failing code example?

hi pgupta, sorry for my delayed reply.

AerospikeClient client = new AerospikeClient("localhost", 3000);
WritePolicy policy = new WritePolicy();

policy.recordExistsAction = RecordExistsAction.REPLACE;
Key key = new Key("test", "test", 1);

List<Value> bin = Arrays.asList(Value.get(2), Value.get(1), Value.get(3), Value.get(4));

int writeFlags = ListWriteFlags.ADD_UNIQUE | ListWriteFlags.PARTIAL | ListWriteFlags.NO_FAIL;
ListPolicy listPolicy = new ListPolicy(ListOrder.ORDERED, writeFlags);
Operation append = ListOperation.appendItems(listPolicy, "list", bin);
client.operate(policy, key, append);

Record record = client.get(client.readPolicyDefault, key);

If I substitute RecordExistsAction.REPLACE for a default value RecordExistsAction.UPDATE then everything works smoothly.

ListOperations are intended for modifying an existing List Bin - so they are only allowed for UPDATE action. If you want to REPLACE an existing record, then don’t go the List Modification route. Write a list bin directly using client.put().

1 Like

thank you! Actually the “policy” method argument was a source of confusion for me. If it’s not intended to be different than a single value, it would be nice to exclude it from the list of arguments, wouldn’t it?

Anyway, thank you!

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.