Search in sources :

Example 6 with Key

use of com.aerospike.client.Key in project aerospike-client-java by aerospike.

the class ListMap method testMapComplex.

/**
	 * Write/Read HashMap<Object,Object> directly instead of relying on java serializer.
	 */
private void testMapComplex(AerospikeClient client, Parameters params) throws Exception {
    console.info("Read/Write HashMap<Object,Object>");
    Key key = new Key(params.namespace, params.set, "mapkey2");
    client.delete(params.writePolicy, key);
    byte[] blob = new byte[] { 3, 52, 125 };
    List<Integer> list = new ArrayList<Integer>();
    list.add(100034);
    list.add(12384955);
    list.add(3);
    list.add(512);
    HashMap<Object, Object> map = new HashMap<Object, Object>();
    map.put("key1", "string1");
    map.put("key2", 2);
    map.put("key3", blob);
    // map.put("key4", Value.getAsList(list)) works too
    map.put("key4", list);
    map.put("key5", true);
    map.put("key6", false);
    Bin bin = new Bin(params.getBinName("mapbin2"), map);
    client.put(params.writePolicy, key, bin);
    Record record = client.get(params.policy, key, bin.name);
    Map<?, ?> receivedMap = (Map<?, ?>) record.getValue(bin.name);
    validateSize(6, receivedMap.size());
    validate("string1", receivedMap.get("key1"));
    // Server convert numbers to long, so must expect long.
    validate(2L, receivedMap.get("key2"));
    validate(blob, (byte[]) receivedMap.get("key3"));
    List<?> receivedInner = (List<?>) receivedMap.get("key4");
    validateSize(4, receivedInner.size());
    validate(100034L, receivedInner.get(0));
    validate(12384955L, receivedInner.get(1));
    validate(3L, receivedInner.get(2));
    validate(512L, receivedInner.get(3));
    validate(true, receivedMap.get("key5"));
    validate(false, receivedMap.get("key6"));
    console.info("Read/Write HashMap<Object,Object> successful");
}
Also used : HashMap(java.util.HashMap) Bin(com.aerospike.client.Bin) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) Key(com.aerospike.client.Key)

Example 7 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 8 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 9 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 : ArrayList(java.util.ArrayList) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 10 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)185 Bin (com.aerospike.client.Bin)97 Record (com.aerospike.client.Record)96 Test (org.junit.Test)71 ArrayList (java.util.ArrayList)40 AerospikeException (com.aerospike.client.AerospikeException)34 Value (com.aerospike.client.Value)30 HashMap (java.util.HashMap)30 List (java.util.List)24 WritePolicy (com.aerospike.client.policy.WritePolicy)21 Map (java.util.Map)15 BeforeClass (org.junit.BeforeClass)10 Policy (com.aerospike.client.policy.Policy)9 Statement (com.aerospike.client.query.Statement)9 IndexTask (com.aerospike.client.task.IndexTask)8 RecordSet (com.aerospike.client.query.RecordSet)7 BatchRead (com.aerospike.client.BatchRead)5 MapPolicy (com.aerospike.client.cdt.MapPolicy)5 LargeList (com.aerospike.client.large.LargeList)5 RecordSequenceListener (com.aerospike.client.listener.RecordSequenceListener)5