Search in sources :

Example 51 with Key

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

the class Append method runExample.

/**
 * Append string to an existing string.
 */
@Override
public void runExample(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "appendkey");
    String binName = params.getBinName("appendbin");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    Bin bin = new Bin(binName, "Hello");
    console.info("Initial append will create record.  Initial value is " + bin.value + '.');
    client.append(params.writePolicy, key, bin);
    bin = new Bin(binName, " World");
    console.info("Append \"" + bin.value + "\" to existing record.");
    client.append(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));
    }
    Object received = record.getValue(bin.name);
    String expected = "Hello World";
    if (received.equals(expected)) {
        console.info("Append successful: ns=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, received);
    } else {
        console.error("Append 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 52 with Key

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

the class AsyncBatch method writeRecords.

/**
 * Write records individually.
 */
private void writeRecords() {
    WriteHandler handler = new WriteHandler(size);
    for (int i = 1; i <= size; i++) {
        Key key = sendKeys[i - 1];
        Bin bin = new Bin(binName, valuePrefix + 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(eventLoop, handler, params.writePolicy, key, bin);
    }
}
Also used : Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key)

Example 53 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);
            }
        }

        public void onFailure(AerospikeException e) {
            console.error("Batch read complex failed: " + Util.getErrorMessage(e));
        }
    }, 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 54 with Key

use of com.aerospike.client.Key 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 55 with Key

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

the class Batch method batchReadHeaders.

/**
 * Read record header data in one batch.
 */
private void batchReadHeaders(AerospikeClient client, Parameters params, String keyPrefix, int size) throws Exception {
    // Batch gets into one call.
    Key[] keys = new Key[size];
    for (int i = 0; i < size; i++) {
        keys[i] = new Key(params.namespace, params.set, keyPrefix + (i + 1));
    }
    Record[] records = client.getHeader(null, keys);
    for (int i = 0; i < records.length; i++) {
        Key key = keys[i];
        Record record = records[i];
        Level level = Level.ERROR;
        int generation = 0;
        int expiration = 0;
        if (record != null && (record.generation > 0 || record.expiration > 0)) {
            level = Level.INFO;
            generation = record.generation;
            expiration = record.expiration;
        }
        console.write(level, "Record: ns=%s set=%s key=%s generation=%d expiration=%d", key.namespace, key.setName, key.userKey, generation, expiration);
    }
    if (records.length != size) {
        console.error("Record size mismatch. Expected %d. Received %d.", size, records.length);
    }
}
Also used : Record(com.aerospike.client.Record) Level(com.aerospike.client.Log.Level) 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