Search in sources :

Example 26 with Key

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

the class AsyncBatch method batchReadComplex.

/**
	 * Read records with varying namespaces, bin names and read types in one batch.
	 * This requires Aerospike Server version >= 3.6.0
	 */
private void batchReadComplex() throws Exception {
    // Batch gets into one call.
    // Batch allows multiple namespaces in one call, but example test environment may only have one namespace.
    String[] bins = new String[] { binName };
    List<BatchRead> records = new ArrayList<BatchRead>();
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 1), bins));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 2), true));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 3), true));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 4), false));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 5), true));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 6), true));
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 7), bins));
    // This record should be found, but the requested bin will not be found.
    records.add(new BatchRead(new Key(params.namespace, params.set, keyPrefix + 8), new String[] { "binnotfound" }));
    // This record should not be found.
    records.add(new BatchRead(new Key(params.namespace, params.set, "keynotfound"), bins));
    // Execute batch.
    client.get(eventLoop, new BatchListListener() {

        public void onSuccess(List<BatchRead> records) {
            // Show results.
            int found = 0;
            for (BatchRead record : records) {
                Key key = record.key;
                Record rec = record.record;
                if (rec != null) {
                    found++;
                    console.info("Record: ns=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, binName, rec.getValue(binName));
                } else {
                    console.info("Record not found: ns=%s set=%s key=%s bin=%s", key.namespace, key.setName, key.userKey, binName);
                }
            }
            if (found != 8) {
                console.error("Records found mismatch. Expected %d. Received %d.", 8, found);
            }
            taskComplete();
        }

        public void onFailure(AerospikeException e) {
            console.error("Batch read complex failed: " + Util.getErrorMessage(e));
            taskComplete();
        }
    }, null, records);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) BatchListListener(com.aerospike.client.listener.BatchListListener) ArrayList(java.util.ArrayList) BatchRead(com.aerospike.client.BatchRead) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key)

Example 27 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 28 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 29 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 30 with Key

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

the class QuerySum method writeRecords.

private void writeRecords(AerospikeClient client, Parameters params, String keyPrefix, String binName, int size) throws Exception {
    for (int i = 1; i <= size; i++) {
        Key key = new Key(params.namespace, params.set, keyPrefix + i);
        Bin bin = new Bin(binName, i);
        console.info("Put: ns=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, bin.value);
        client.put(params.writePolicy, key, bin);
    }
}
Also used : Bin(com.aerospike.client.Bin) 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