See this sample code. Delete all keys (see call to removeByKeyRange, start = null, end=null) in the operate command before inserting the new map. It will be atomic.
package com.aerospike;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
import com.aerospike.client.AerospikeClient;
import com.aerospike.client.Record;
import com.aerospike.client.Bin;
import com.aerospike.client.Key;
import com.aerospike.client.Value;
import com.aerospike.client.cdt.MapOperation;
import com.aerospike.client.cdt.MapPolicy;
import com.aerospike.client.cdt.MapOrder;
import com.aerospike.client.cdt.MapWriteMode;
import com.aerospike.client.cdt.MapReturnType;
import com.aerospike.client.policy.RecordExistsAction;
import com.aerospike.client.policy.WritePolicy;
public class MapBasedCounter {
public static void putItemsKOrdered(AerospikeClient client, Key key, Map<Value,Value> m) {
MapPolicy mPolicy = new MapPolicy(MapOrder.KEY_ORDERED, MapWriteMode.UPDATE);
Map<Value, Value> m1empty = new HashMap<Value, Value>();
client.operate(null, key,
MapOperation.removeByKeyRange("myMap", null, null, MapReturnType.COUNT),
MapOperation.putItems(mPolicy, "myMap", m)
);
}
public static void main(String[] args) {
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
Key key1 = new Key("test", "s1", 1);
WritePolicy policy = new WritePolicy();
policy.recordExistsAction = RecordExistsAction.UPDATE;
client.delete(policy, key1);
Map<Value, Value> m1 = new HashMap<Value, Value>();
m1.put(Value.get("cv1"), Value.get(11));
m1.put(Value.get("cv2"), Value.get(12));
m1.put(Value.get("cv3"), Value.get(13));
m1.put(Value.get("cv4"), Value.get(14));
m1.put(Value.get("cv5"), Value.get(15));
putItemsKOrdered(client, key1,m1);
System.out.println("\nRecords inserted:");
System.out.println("\nKey1,KEY_ORDERED:"+ client.operate(null, key1,
MapOperation.getByIndexRange("myMap", 0, MapReturnType.VALUE)));
Map<Value, Value> m1a = new HashMap<Value, Value>();
m1a.put(Value.get("v1"), Value.get(11));
m1a.put(Value.get("v2"), Value.get(12));
m1a.put(Value.get("v3"), Value.get(13));
m1a.put(Value.get("v4"), Value.get(14));
putItemsKOrdered(client, key1,m1a);
System.out.println("\nRecords inserted:");
System.out.println("\nKey1,KEY_ORDERED:"+ client.operate(null, key1,
MapOperation.getByIndexRange("myMap", 0, MapReturnType.VALUE)));
client.close();
}
}
Output:
Records inserted:
Key1,KEY_ORDERED:(gen:1),(exp:289330207),(bins:(myMap:[11, 12, 13, 14, 15]))
Records inserted:
Key1,KEY_ORDERED:(gen:2),(exp:289330207),(bins:(myMap:[11, 12, 13, 14]))