Search in sources :

Example 31 with Key

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

the class UserDefinedFunction method writeUsingUdf.

private void writeUsingUdf(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "udfkey1");
    Bin bin = new Bin(params.getBinName("udfbin1"), "string value");
    client.execute(params.writePolicy, key, "record_example", "writeBin", Value.get(bin.name), bin.value);
    Record record = client.get(params.policy, key, bin.name);
    String expected = bin.value.toString();
    String received = record.getString(bin.name);
    if (received != null && received.equals(expected)) {
        console.info("Data matched: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, received);
    } else {
        console.error("Data 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 32 with Key

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

the class UserDefinedFunction method writeIfNotExists.

private void writeIfNotExists(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "udfkey3");
    String binName = "udfbin3";
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    // Write record only if not already exists. This should succeed.
    client.execute(params.writePolicy, key, "record_example", "writeUnique", Value.get(binName), Value.get("first"));
    // Verify record written.
    Record record = client.get(params.policy, key, binName);
    String expected = "first";
    String received = record.getString(binName);
    if (received != null && received.equals(expected)) {
        console.info("Record written: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, binName, received);
    } else {
        console.error("Data mismatch: Expected %s. Received %s.", expected, received);
    }
    // Write record second time. This should fail.
    console.info("Attempt second write.");
    client.execute(params.writePolicy, key, "record_example", "writeUnique", Value.get(binName), Value.get("second"));
    // Verify record not written.
    record = client.get(params.policy, key, binName);
    received = record.getString(binName);
    if (received != null && received.equals(expected)) {
        console.info("Success. Record remained unchanged: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, binName, received);
    } else {
        console.error("Data mismatch: Expected %s. Received %s.", expected, received);
    }
}
Also used : Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 33 with Key

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

the class UserDefinedFunction method appendListUsingUdf.

private void appendListUsingUdf(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "udfkey5");
    String binName = params.getBinName("udfbin5");
    String value = "appended value";
    client.execute(params.writePolicy, key, "record_example", "appendListBin", Value.get(binName), Value.get(value));
    Record record = client.get(params.policy, key, binName);
    if (record != null) {
        Object received = record.getValue(binName);
        if (received != null && received instanceof List<?>) {
            List<?> list = (List<?>) received;
            if (list.size() == 5) {
                Object obj = list.get(4);
                if (obj.equals(value)) {
                    console.info("UDF data matched: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, binName, received);
                    return;
                }
            }
        }
        console.error("UDF data mismatch");
        console.error("Expected: " + value);
        console.error("Received: " + received);
    } else {
        console.error("Failed to find record: " + key.userKey);
    }
}
Also used : Record(com.aerospike.client.Record) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.aerospike.client.Key)

Example 34 with Key

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

the class Replace method runReplaceExample.

public void runReplaceExample(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "replacekey");
    Bin bin1 = new Bin("bin1", "value1");
    Bin bin2 = new Bin("bin2", "value2");
    Bin bin3 = new Bin("bin3", "value3");
    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);
    console.info("Replace with: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin3.name, bin3.value);
    WritePolicy policy = new WritePolicy();
    policy.recordExistsAction = RecordExistsAction.REPLACE;
    client.put(policy, key, bin3);
    console.info("Get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey);
    Record record = client.get(params.policy, key);
    if (record == null) {
        throw new Exception(String.format("Failed to get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey));
    }
    if (record.getValue(bin1.name) == null) {
        console.info(bin1.name + " was deleted as expected.");
    } else {
        console.error(bin1.name + " found when it should have been deleted.");
    }
    if (record.getValue(bin2.name) == null) {
        console.info(bin2.name + " was deleted as expected.");
    } else {
        console.error(bin2.name + " found when it should have been deleted.");
    }
    validateBin(key, bin3, record);
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) AerospikeException(com.aerospike.client.AerospikeException) WritePolicy(com.aerospike.client.policy.WritePolicy)

Example 35 with Key

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

the class Replace method runReplaceOnlyExample.

public void runReplaceOnlyExample(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "replaceonlykey");
    Bin bin = new Bin("bin", "value");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    console.info("Replace record requiring that it exists: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey);
    try {
        WritePolicy policy = new WritePolicy();
        policy.recordExistsAction = RecordExistsAction.REPLACE_ONLY;
        client.put(policy, key, bin);
        console.error("Failure. This command should have resulted in an error.");
    } catch (AerospikeException ae) {
        if (ae.getResultCode() == ResultCode.KEY_NOT_FOUND_ERROR) {
            console.info("Success. Key not found error returned as expected.");
        } else {
            throw ae;
        }
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy)

Aggregations

Key (com.aerospike.client.Key)201 Record (com.aerospike.client.Record)104 Bin (com.aerospike.client.Bin)103 Test (org.junit.Test)79 ArrayList (java.util.ArrayList)49 AerospikeException (com.aerospike.client.AerospikeException)47 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)8 IndexTask (com.aerospike.client.task.IndexTask)8 Collections.singletonList (java.util.Collections.singletonList)6 AerospikeClient (com.aerospike.client.AerospikeClient)5 BatchRead (com.aerospike.client.BatchRead)5 MapPolicy (com.aerospike.client.cdt.MapPolicy)5