use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class ListMap method testListStrings.
/**
* Write/Read ArrayList<String> directly instead of relying on java serializer.
*/
private void testListStrings(AerospikeClient client, Parameters params) throws Exception {
console.info("Read/Write ArrayList<String>");
Key key = new Key(params.namespace, params.set, "listkey1");
client.delete(params.writePolicy, key);
ArrayList<String> list = new ArrayList<String>();
list.add("string1");
list.add("string2");
list.add("string3");
Bin bin = new Bin(params.getBinName("listbin1"), list);
client.put(params.writePolicy, key, bin);
Record record = client.get(params.policy, key, bin.name);
List<?> receivedList = (List<?>) record.getValue(bin.name);
validateSize(3, receivedList.size());
validate("string1", receivedList.get(0));
validate("string2", receivedList.get(1));
validate("string3", receivedList.get(2));
console.info("Read/Write ArrayList<String> successful.");
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class ListMap method testMapStrings.
/**
* Write/Read HashMap<String,String> directly instead of relying on java serializer.
*/
private void testMapStrings(AerospikeClient client, Parameters params) throws Exception {
console.info("Read/Write HashMap<String,String>");
Key key = new Key(params.namespace, params.set, "mapkey1");
client.delete(params.writePolicy, key);
HashMap<String, String> map = new HashMap<String, String>();
map.put("key1", "string1");
map.put("key2", "string2");
map.put("key3", "string3");
Bin bin = new Bin(params.getBinName("mapbin1"), map);
client.put(params.writePolicy, key, bin);
Record record = client.get(params.policy, key, bin.name);
Map<?, ?> receivedMap = (Map<?, ?>) record.getValue(bin.name);
validateSize(3, receivedMap.size());
validate("string1", receivedMap.get("key1"));
validate("string2", receivedMap.get("key2"));
validate("string3", receivedMap.get("key3"));
console.info("Read/Write HashMap<String,String> successful");
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class Operate method runExample.
/**
* Demonstrate multiple operations on a single record in one call.
*/
@Override
public void runExample(AerospikeClient client, Parameters params) throws Exception {
// Write initial record.
Key key = new Key(params.namespace, params.set, "opkey");
Bin bin1 = new Bin("optintbin", 7);
Bin bin2 = new Bin("optstringbin", "string value");
console.info("Put: namespace=%s set=%s key=%s bin1=%s value1=%s bin2=%s value2=%s", key.namespace, key.setName, key.userKey, bin1.name, bin1.value, bin2.name, bin2.value);
client.put(params.writePolicy, key, bin1, bin2);
// Add integer, write new string and read record.
Bin bin3 = new Bin(bin1.name, 4);
Bin bin4 = new Bin(bin2.name, "new string");
console.info("Add: " + bin3.value);
console.info("Write: " + bin4.value);
console.info("Read:");
Record record = client.operate(params.writePolicy, key, Operation.add(bin3), Operation.put(bin4), Operation.get());
if (record == null) {
throw new Exception(String.format("Failed to get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey));
}
validateBin(key, record, bin3.name, 11, record.getInt(bin3.name));
validateBin(key, record, bin4.name, bin4.value.toString(), record.getValue(bin4.name));
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class OperateList method runSimpleExample.
/**
* Simple example of list functionality.
*/
public void runSimpleExample(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "listkey");
String binName = params.getBinName("listbin");
// Delete record if it already exists.
client.delete(params.writePolicy, key);
List<Value> inputList = new ArrayList<Value>();
inputList.add(Value.get(55));
inputList.add(Value.get(77));
// Write values to empty list.
Record record = client.operate(params.writePolicy, key, ListOperation.appendItems(binName, inputList));
console.info("Record: " + record);
// Pop value from end of list and also return new size of list.
record = client.operate(params.writePolicy, key, ListOperation.pop(binName, -1), ListOperation.size(binName));
console.info("Record: " + record);
// There should be one result for each list operation on the same list bin.
// In this case, there are two list operations (pop and size), so there
// should be two results.
List<?> list = record.getList(binName);
for (Object value : list) {
console.info("Received: " + value);
}
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class OperateMap method runSimpleExample.
/**
* Simple example of map operate functionality.
*/
public void runSimpleExample(AerospikeClient client, Parameters params) throws Exception {
Key key = new Key(params.namespace, params.set, "mapkey");
String binName = params.getBinName("mapbin");
// Delete record if it already exists.
client.delete(params.writePolicy, key);
Map<Value, Value> inputMap = new HashMap<Value, Value>();
inputMap.put(Value.get(1), Value.get(55));
inputMap.put(Value.get(2), Value.get(33));
// Write values to empty map.
Record record = client.operate(params.writePolicy, key, MapOperation.putItems(MapPolicy.Default, binName, inputMap));
console.info("Record: " + record);
// Pop value from map and also return new size of map.
record = client.operate(params.writePolicy, key, MapOperation.removeByKey(binName, Value.get(1), MapReturnType.VALUE), MapOperation.size(binName));
console.info("Record: " + record);
// There should be one result for each map operation on the same map bin.
// In this case, there are two map operations (pop and size), so there
// should be two results.
List<?> list = record.getList(binName);
for (Object value : list) {
console.info("Received: " + value);
}
}
Aggregations