Search in sources :

Example 61 with Key

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.");
}
Also used : Bin(com.aerospike.client.Bin) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) List(java.util.List) ArrayList(java.util.ArrayList) Key(com.aerospike.client.Key)

Example 62 with Key

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");
}
Also used : HashMap(java.util.HashMap) Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Map(java.util.Map) HashMap(java.util.HashMap) Key(com.aerospike.client.Key)

Example 63 with Key

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));
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 64 with Key

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);
    }
}
Also used : Value(com.aerospike.client.Value) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 65 with Key

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);
    }
}
Also used : HashMap(java.util.HashMap) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Aggregations

Key (com.aerospike.client.Key)204 Record (com.aerospike.client.Record)106 Bin (com.aerospike.client.Bin)104 Test (org.junit.Test)79 AerospikeException (com.aerospike.client.AerospikeException)50 ArrayList (java.util.ArrayList)50 Value (com.aerospike.client.Value)37 HashMap (java.util.HashMap)32 List (java.util.List)31 WritePolicy (com.aerospike.client.policy.WritePolicy)21 Map (java.util.Map)15 Statement (com.aerospike.client.query.Statement)10 BeforeClass (org.junit.BeforeClass)10 Policy (com.aerospike.client.policy.Policy)9 RecordSet (com.aerospike.client.query.RecordSet)9 IndexTask (com.aerospike.client.task.IndexTask)8 IOException (java.io.IOException)6 Collections.singletonList (java.util.Collections.singletonList)6 AerospikeClient (com.aerospike.client.AerospikeClient)5 BatchRead (com.aerospike.client.BatchRead)5