Search in sources :

Example 61 with Bin

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

the class OperateMap method runNestedListCreateExample.

public void runNestedListCreateExample(AerospikeClient client, Parameters params) {
    Key key = new Key(params.namespace, params.set, "mapkey3");
    String binName = params.getBinName("mapbin");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    List<Value> l1 = new ArrayList<Value>();
    l1.add(Value.get(7));
    l1.add(Value.get(9));
    l1.add(Value.get(5));
    Map<Value, Value> inputMap = new HashMap<Value, Value>();
    inputMap.put(Value.get("key1"), Value.get(l1));
    // Create maps.
    client.put(params.writePolicy, key, new Bin(binName, inputMap));
    // Create ordered list at map's "key2" only if list does not exist.
    // Append 2,1 to ordered list.
    CTX ctx = CTX.mapKey(Value.get("key2"));
    Record record = client.operate(params.writePolicy, key, ListOperation.create(binName, ListOrder.ORDERED, false, ctx), ListOperation.append(binName, Value.get(2), ctx), ListOperation.append(binName, Value.get(1), ctx), Operation.get(binName));
    record = client.get(params.policy, key);
    console.info("Record: " + record);
}
Also used : HashMap(java.util.HashMap) Bin(com.aerospike.client.Bin) CTX(com.aerospike.client.cdt.CTX) Value(com.aerospike.client.Value) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 62 with Bin

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

the class Prepend method runExample.

/**
 * Prepend string to an existing string.
 */
@Override
public void runExample(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "prependkey");
    String binName = params.getBinName("prependbin");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    Bin bin = new Bin(binName, "World");
    console.info("Initial prepend will create record.  Initial value is " + bin.value + '.');
    client.prepend(params.writePolicy, key, bin);
    bin = new Bin(binName, "Hello ");
    console.info("Prepend \"" + bin.value + "\" to existing record.");
    client.prepend(params.writePolicy, key, bin);
    Record record = client.get(params.policy, key, bin.name);
    if (record == null) {
        throw new Exception(String.format("Failed to get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey));
    }
    // The value received from the server is an unsigned byte stream.
    // Convert to an integer before comparing with expected.
    Object received = record.getValue(bin.name);
    String expected = "Hello World";
    if (received.equals(expected)) {
        console.info("Prepend successful: ns=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, received);
    } else {
        console.error("Prepend mismatch: Expected %s. Received %s.", expected, received);
    }
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 63 with Bin

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

the class AsyncPutGet method runExample.

/**
 * Asynchronously write and read a bin using alternate methods.
 */
@Override
public void runExample(AerospikeClient client, EventLoop eventLoop) {
    Key key = new Key(params.namespace, params.set, "putgetkey");
    Bin bin = new Bin(params.getBinName("putgetbin"), "value");
    runPutGetInline(client, eventLoop, key, bin);
    runPutGetWithRetry(client, eventLoop, key, bin);
}
Also used : Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key)

Example 64 with Bin

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

the class BatchOperate method writeRecords.

private void writeRecords(AerospikeClient client, Parameters params) {
    for (int i = 1; i <= RecordCount; i++) {
        Key key = new Key(params.namespace, params.set, KeyPrefix + i);
        Bin bin1 = new Bin(BinName1, i);
        Bin bin2 = new Bin(BinName2, i + 10);
        List<Integer> list = new ArrayList<Integer>();
        for (int j = 0; j < i; j++) {
            list.add(j * i);
        }
        Bin bin3 = new Bin(BinName3, list);
        console.info("Put: ns=%s set=%s key=%s val1=%s val2=%s val3=%s", key.namespace, key.setName, key.userKey, bin1.value, bin2.value, list.toString());
        client.put(params.writePolicy, key, bin1, bin2, bin3);
    }
}
Also used : Bin(com.aerospike.client.Bin) ArrayList(java.util.ArrayList) Key(com.aerospike.client.Key)

Example 65 with Bin

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

the class Expire method expireExample.

/**
 * Write and twice read an expiration record.
 */
private void expireExample(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "expirekey ");
    Bin bin = new Bin(params.getBinName("expirebin"), "expirevalue");
    console.info("Put: namespace=%s set=%s key=%s bin=%s value=%s expiration=2", key.namespace, key.setName, key.userKey, bin.name, bin.value);
    // Specify that record expires 2 seconds after it's written.
    WritePolicy writePolicy = new WritePolicy();
    writePolicy.expiration = 2;
    client.put(writePolicy, key, bin);
    // Read the record before it expires, showing it is there.
    console.info("Get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey);
    Record record = client.get(params.policy, key, bin.name);
    if (record == null) {
        throw new Exception(String.format("Failed to get record: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey));
    }
    Object received = record.getValue(bin.name);
    String expected = bin.value.toString();
    if (received.equals(expected)) {
        console.info("Get record successful: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, received);
    } else {
        throw new Exception(String.format("Expire record mismatch: Expected %s. Received %s.", expected, received));
    }
    // Read the record after it expires, showing it's gone.
    console.info("Sleeping for 3 seconds ...");
    Thread.sleep(3 * 1000);
    record = client.get(params.policy, key, bin.name);
    if (record == null) {
        console.info("Expiry of record successful. Record not found.");
    } else {
        console.error("Found record when it should have expired.");
    }
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy)

Aggregations

Bin (com.aerospike.client.Bin)151 Key (com.aerospike.client.Key)129 Record (com.aerospike.client.Record)70 Test (org.junit.Test)54 AerospikeException (com.aerospike.client.AerospikeException)34 ArrayList (java.util.ArrayList)31 WritePolicy (com.aerospike.client.policy.WritePolicy)27 HashMap (java.util.HashMap)22 Value (com.aerospike.client.Value)20 List (java.util.List)15 BeforeClass (org.junit.BeforeClass)14 Map (java.util.Map)13 Policy (com.aerospike.client.policy.Policy)12 IndexTask (com.aerospike.client.task.IndexTask)11 WriteListener (com.aerospike.client.listener.WriteListener)5 RegisterTask (com.aerospike.client.task.RegisterTask)5 AbstractMap (java.util.AbstractMap)4 TreeMap (java.util.TreeMap)4 CTX (com.aerospike.client.cdt.CTX)3 BitPolicy (com.aerospike.client.operation.BitPolicy)3